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

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

Open
ruchakulkarni11 opened this issue Nov 22, 2023 · 3 comments

Comments

@ruchakulkarni11
Copy link

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?

@wenovus
Copy link
Collaborator

wenovus commented Nov 22, 2023

Currently the Go-generated code does not respect the YANG enum value and we do not support it. This has been brought up before (#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
Copy link
Author

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
Copy link
Collaborator

wenovus commented Nov 23, 2023

Some quick tips:

  • Here is where the enum definition in the IR (intermediate representation) is created

    ygot/ygen/genir.go

    Lines 153 to 169 in 4dcc65e

    default:
    // The remaining enumerated types are all represented as an Enum type within the
    // Goyang entry construct. The values are accessed in a map keyed by an int64
    // and with a value of the name of the enumerated value - retrieved via ValueMap().
    var values []int
    valueMap := enum.entry.Type.Enum.ValueMap()
    for v := range valueMap {
    values = append(values, int(v))
    }
    sort.Ints(values)
    for _, v := range values {
    et.ValToYANGDetails = append(et.ValToYANGDetails, ygot.EnumDefinition{
    Name: valueMap[int64(v)],
    Value: v,
    })
    }
    }
  • Here is where the values are assigned:

    ygot/gogen/goenums.go

    Lines 61 to 65 in 4dcc65e

    case ygen.IdentityType, ygen.SimpleEnumerationType, ygen.DerivedEnumerationType, ygen.UnionEnumerationType, ygen.DerivedUnionEnumerationType:
    for _, v := range e.ValToYANGDetails {
    values[int64(v.Value)+1] = safeGoEnumeratedValueName(v.Name)
    origValues[int64(v.Value)+1] = v
    }

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants