ygot icon indicating copy to clipboard operation
ygot copied to clipboard

Can enum preserve their defined values while converting yang to gostruct using openconfig/ygot\@v0.25.4/generator/generator.go ?

Open ruchakulkarni11 opened this issue 1 year ago • 3 comments

I am using openconfig/[email protected]/generator/generator.go to convert yang to gostruct.

But the enum values are not converted as it is.

Example: typedef enum_example { type enumeration { enum five { value 5; } enum eight {value 8;} } }

Generated Go Code const ( EnumExample_UNSET E_EnumExample = 0 EnumExample_five E_EnumExample = 1 EnumExample_eight E_EnumExample = 2 )

The Go code that I expect: const ( EnumExample_UNSET E_EnumExample = 0 EnumExample_five E_EnumExample = 5 EnumExample_eight E_EnumExample = 8 )

Is there any way I can generate go enums with the values same as defined in yang file?

ruchakulkarni11 avatar Nov 22 '23 20:11 ruchakulkarni11

Currently the Go-generated code does not respect the YANG enum value and we do not support it. This has been brought up before (https://github.com/openconfig/ygot/issues/286) and the solution would be a flag-protected feature. We currently don't have plans to add this ourselves since JSON (RFC7951) and gNMI both use strings instead of this number for configuration and telemetry, rather than the enum value. However we would welcome any contributions.

There is a concern I can see right now if any implementation is done -- if any of the enum values uses 0, then the UNSET value must be a different value, and then any uninitialized enum field would not be 0. This would not be good Go style and choosing a value may cause a conflict later on if more values are added. One solution is to add 1 to every positive generated value and subtract 1 for every negative generated value from the YANG value.

wenovus avatar Nov 22 '23 23:11 wenovus

Thank you @wenovus for the quick reply!

If you can share more details on an idea of flag-protected feature, that will be really helpful. I would like to know more about where can we have this feature and how can we use it?

ruchakulkarni11 avatar Nov 23 '23 02:11 ruchakulkarni11

Some quick tips:

  • Here is where the enum definition in the IR (intermediate representation) is created https://github.com/openconfig/ygot/blob/4dcc65ef1230778675d989ce9a4d194512974f17/ygen/genir.go#L153-L169
  • Here is where the values are assigned: https://github.com/openconfig/ygot/blob/4dcc65ef1230778675d989ce9a4d194512974f17/gogen/goenums.go#L61-L65

See if you can figure out how the values are currently populated, and then tweak it to meet your needs, taking into account the design consideration above.

I'm going to be out for a week, I can follow-up afterwards.

I would like to know more about where can we have this feature and how can we use it?

I'm not quite sure what you mean here, I'm assuming you're the feature requester, so you would know how you can use it :-) ?

wenovus avatar Nov 23 '23 04:11 wenovus