openconfig / ygot

A YANG-centric Go toolkit - Go/Protobuf Code Generation; Validation; Marshaling/Unmarshaling
Apache License 2.0
286 stars 107 forks source link

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

Open ruchakulkarni11 opened 1 year ago

ruchakulkarni11 commented 1 year ago

I am using openconfig/ygot\@v0.25.4/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?

wenovus commented 1 year ago

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.

ruchakulkarni11 commented 1 year ago

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?

wenovus commented 1 year ago

Some quick tips:

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 :-) ?