Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to retrieve numeric value of enums in cpp generated code #944

Closed
georgeyanev opened this issue Jul 30, 2019 · 3 comments
Closed

No way to retrieve numeric value of enums in cpp generated code #944

georgeyanev opened this issue Jul 30, 2019 · 3 comments
Assignees

Comments

@georgeyanev
Copy link

georgeyanev commented Jul 30, 2019

Expected Behavior

I expect to be able to retrieve the numeric value of an enum in cpp generated code.

Current Behavior

This seems not possible.

Steps to Reproduce

This yang is passed in a bundle to the generator:

module enum-test {
  namespace "ns0:ns0";
  prefix "ns0";

  typedef loglevel-type {
    type enumeration {
      enum "INFO" {
        value 7;
      }
      enum "DEBUG" {
        value 8;
      }
    }
  }

  container main {
    leaf name {
     type string;
    }

    leaf loglevel {
      type loglevel-type;
    }
  }
}

This is an excerpt of the cpp generated code for the yang above:

class Main : public ydk::Entity
{
    public:
       /*
        Not interesting part is removed
       */

        ydk::YLeaf name; //type: string
        ydk::YLeaf loglevel; //type: LoglevelType   // Isn't this supposed to be of type LoglevelType?
}; // Main

class LoglevelType : public ydk::Enum
{
    public:
        static const ydk::Enum::YLeaf INFO;
        static const ydk::Enum::YLeaf DEBUG;
};

The loglevel field of the main class is of type ydk:YLeaf where the numeric value is not present.
Is there a way to retrieve it?

System Information

YDK-0.8.4
Ubuntu 18.04.2 LTS on a x86_64 machine

@ygorelik ygorelik self-assigned this Jul 30, 2019
ygorelik pushed a commit to ygorelik/ydk-gen that referenced this issue Aug 15, 2019
@ygorelik
Copy link
Collaborator

Added enum_value member to YLeaf class, which carries int value of the enum member. Expended unit tests for access/check of int value of enum leaf.

@ygorelik
Copy link
Collaborator

ygorelik commented Sep 4, 2019

Failed to get enum_value when reading enumeration data from device.

@ygorelik ygorelik reopened this Sep 4, 2019
ygorelik pushed a commit to ygorelik/ydk-gen that referenced this issue Sep 4, 2019
@ygorelik
Copy link
Collaborator

ygorelik commented Sep 5, 2019

Developed partial solution for the issue by providing a static function inside generated ydk::Enum inherited class, which translates enum name to enum_value. Here how it looks like in the bundle code:

class LoglevelType : public ydk::Enum
{
    public:
        static const ydk::Enum::YLeaf INFO;
        static const ydk::Enum::YLeaf DEBUG;

        static int get_enum_value(const std::string name) {
            if (name == "INFO") return 7;
            if (name == "DEBUG") return 8;
            return -1;
        }
};

It would be complete, if the set_value() generated function would set the leaf enum_value automatically. Unfortunately I could not figure out how to do that for the common use, because the enum class could be declared in another C++ file and have different namespace. In current implementation the user should add one line of code to get/set enum_value in a leaf, like this:

  std::cout << "Main LogLevel value: " << sReadPtr->loglevel.value << std::endl;
  sReadPtr->loglevel.enum_value = LoglevelType::get_enum_value(sReadPtr->loglevel.value);
  std::cout << "Main LogLevel enum_value: " << sReadPtr->loglevel.enum_value << std::endl;

@ygorelik ygorelik closed this as completed Sep 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants