From 2f52ba67e1f367e3366fd2ea696fea9c3e7161ca Mon Sep 17 00:00:00 2001 From: Wen Bo Li <50884368+wenovus@users.noreply.github.com> Date: Fri, 14 Apr 2023 13:59:57 -0700 Subject: [PATCH] Revert enum number generation behaviour to pre-v0.19.0 (#814) * Refactor gogen by moving around functions/tests a bit * Revert enum number generation behaviour to pre-v0.19.0 --- gogen/goenums.go | 6 +++--- gogen/testdata/structs/enum-module.formatted-txt | 16 ++++++++-------- .../enum-module.long-enum-names.formatted-txt | 16 ++++++++-------- ...siding-module-typedef-enum-name.formatted-txt | 16 ++++++++-------- ...ypedef-enum-name.wrapper-unions.formatted-txt | 16 ++++++++-------- ...siding-module-typedef-enum-name.formatted-txt | 16 ++++++++-------- .../enum-module.wrapper-unions.formatted-txt | 16 ++++++++-------- testdata/modules/enum-module.yang | 2 +- ygen/genir.go | 8 +++++--- ygot/types.go | 12 +++++++++++- 10 files changed, 68 insertions(+), 56 deletions(-) diff --git a/gogen/goenums.go b/gogen/goenums.go index 87048c553..3392b5895 100644 --- a/gogen/goenums.go +++ b/gogen/goenums.go @@ -59,9 +59,9 @@ func genGoEnumeratedTypes(enums map[string]*ygen.EnumeratedYANGType) (map[string switch e.Kind { case ygen.IdentityType, ygen.SimpleEnumerationType, ygen.DerivedEnumerationType, ygen.UnionEnumerationType, ygen.DerivedUnionEnumerationType: - for i, v := range e.ValToYANGDetails { - values[int64(i)+1] = safeGoEnumeratedValueName(v.Name) - origValues[int64(i)+1] = v + for _, v := range e.ValToYANGDetails { + values[int64(v.Value)+1] = safeGoEnumeratedValueName(v.Name) + origValues[int64(v.Value)+1] = v } default: return nil, fmt.Errorf("unknown enumerated type %v", e.Kind) diff --git a/gogen/testdata/structs/enum-module.formatted-txt b/gogen/testdata/structs/enum-module.formatted-txt index bc6380180..2f8ec09e5 100644 --- a/gogen/testdata/structs/enum-module.formatted-txt +++ b/gogen/testdata/structs/enum-module.formatted-txt @@ -407,13 +407,13 @@ const ( // Child_InlineEnum_UNSET corresponds to the value UNSET of Child_InlineEnum Child_InlineEnum_UNSET E_Child_InlineEnum = 0 // Child_InlineEnum_ADENINE corresponds to the value ADENINE of Child_InlineEnum - Child_InlineEnum_ADENINE E_Child_InlineEnum = 1 + Child_InlineEnum_ADENINE E_Child_InlineEnum = 11 // Child_InlineEnum_THYMINE corresponds to the value THYMINE of Child_InlineEnum - Child_InlineEnum_THYMINE E_Child_InlineEnum = 2 + Child_InlineEnum_THYMINE E_Child_InlineEnum = 12 // Child_InlineEnum_CYTOSINE corresponds to the value CYTOSINE of Child_InlineEnum - Child_InlineEnum_CYTOSINE E_Child_InlineEnum = 3 + Child_InlineEnum_CYTOSINE E_Child_InlineEnum = 13 // Child_InlineEnum_GUANINE corresponds to the value GUANINE of Child_InlineEnum - Child_InlineEnum_GUANINE E_Child_InlineEnum = 4 + Child_InlineEnum_GUANINE E_Child_InlineEnum = 14 ) // E_EnumModule_Cl is a derived int64 type which is used to represent @@ -541,10 +541,10 @@ const ( // valid YANG identifier. var ΛEnum = map[string]map[int64]ygot.EnumDefinition{ "E_Child_InlineEnum": { - 1: {Name: "ADENINE"}, - 2: {Name: "THYMINE"}, - 3: {Name: "CYTOSINE"}, - 4: {Name: "GUANINE"}, + 11: {Name: "ADENINE"}, + 12: {Name: "THYMINE"}, + 13: {Name: "CYTOSINE"}, + 14: {Name: "GUANINE"}, }, "E_EnumModule_Cl": { 1: {Name: "X"}, diff --git a/gogen/testdata/structs/enum-module.long-enum-names.formatted-txt b/gogen/testdata/structs/enum-module.long-enum-names.formatted-txt index de80f25cb..11d2a34a1 100644 --- a/gogen/testdata/structs/enum-module.long-enum-names.formatted-txt +++ b/gogen/testdata/structs/enum-module.long-enum-names.formatted-txt @@ -253,13 +253,13 @@ const ( // EnumModule_Child_InlineEnum_UNSET corresponds to the value UNSET of EnumModule_Child_InlineEnum EnumModule_Child_InlineEnum_UNSET E_EnumModule_Child_InlineEnum = 0 // EnumModule_Child_InlineEnum_ADENINE corresponds to the value ADENINE of EnumModule_Child_InlineEnum - EnumModule_Child_InlineEnum_ADENINE E_EnumModule_Child_InlineEnum = 1 + EnumModule_Child_InlineEnum_ADENINE E_EnumModule_Child_InlineEnum = 11 // EnumModule_Child_InlineEnum_THYMINE corresponds to the value THYMINE of EnumModule_Child_InlineEnum - EnumModule_Child_InlineEnum_THYMINE E_EnumModule_Child_InlineEnum = 2 + EnumModule_Child_InlineEnum_THYMINE E_EnumModule_Child_InlineEnum = 12 // EnumModule_Child_InlineEnum_CYTOSINE corresponds to the value CYTOSINE of EnumModule_Child_InlineEnum - EnumModule_Child_InlineEnum_CYTOSINE E_EnumModule_Child_InlineEnum = 3 + EnumModule_Child_InlineEnum_CYTOSINE E_EnumModule_Child_InlineEnum = 13 // EnumModule_Child_InlineEnum_GUANINE corresponds to the value GUANINE of EnumModule_Child_InlineEnum - EnumModule_Child_InlineEnum_GUANINE E_EnumModule_Child_InlineEnum = 4 + EnumModule_Child_InlineEnum_GUANINE E_EnumModule_Child_InlineEnum = 14 ) // E_EnumModule_EnumModule_Cl is a derived int64 type which is used to represent @@ -387,10 +387,10 @@ const ( // valid YANG identifier. var ΛEnum = map[string]map[int64]ygot.EnumDefinition{ "E_EnumModule_Child_InlineEnum": { - 1: {Name: "ADENINE"}, - 2: {Name: "THYMINE"}, - 3: {Name: "CYTOSINE"}, - 4: {Name: "GUANINE"}, + 11: {Name: "ADENINE"}, + 12: {Name: "THYMINE"}, + 13: {Name: "CYTOSINE"}, + 14: {Name: "GUANINE"}, }, "E_EnumModule_EnumModule_Cl": { 1: {Name: "X"}, diff --git a/gogen/testdata/structs/enum-module.long-enum-names.residing-module-typedef-enum-name.formatted-txt b/gogen/testdata/structs/enum-module.long-enum-names.residing-module-typedef-enum-name.formatted-txt index 434a00c1c..bb54fd4ac 100644 --- a/gogen/testdata/structs/enum-module.long-enum-names.residing-module-typedef-enum-name.formatted-txt +++ b/gogen/testdata/structs/enum-module.long-enum-names.residing-module-typedef-enum-name.formatted-txt @@ -315,13 +315,13 @@ const ( // EnumModule_Child_InlineEnum_UNSET corresponds to the value UNSET of EnumModule_Child_InlineEnum EnumModule_Child_InlineEnum_UNSET E_EnumModule_Child_InlineEnum = 0 // EnumModule_Child_InlineEnum_ADENINE corresponds to the value ADENINE of EnumModule_Child_InlineEnum - EnumModule_Child_InlineEnum_ADENINE E_EnumModule_Child_InlineEnum = 1 + EnumModule_Child_InlineEnum_ADENINE E_EnumModule_Child_InlineEnum = 11 // EnumModule_Child_InlineEnum_THYMINE corresponds to the value THYMINE of EnumModule_Child_InlineEnum - EnumModule_Child_InlineEnum_THYMINE E_EnumModule_Child_InlineEnum = 2 + EnumModule_Child_InlineEnum_THYMINE E_EnumModule_Child_InlineEnum = 12 // EnumModule_Child_InlineEnum_CYTOSINE corresponds to the value CYTOSINE of EnumModule_Child_InlineEnum - EnumModule_Child_InlineEnum_CYTOSINE E_EnumModule_Child_InlineEnum = 3 + EnumModule_Child_InlineEnum_CYTOSINE E_EnumModule_Child_InlineEnum = 13 // EnumModule_Child_InlineEnum_GUANINE corresponds to the value GUANINE of EnumModule_Child_InlineEnum - EnumModule_Child_InlineEnum_GUANINE E_EnumModule_Child_InlineEnum = 4 + EnumModule_Child_InlineEnum_GUANINE E_EnumModule_Child_InlineEnum = 14 ) // E_EnumModule_EnumModule_Cl is a derived int64 type which is used to represent @@ -428,10 +428,10 @@ var ΛEnum = map[string]map[int64]ygot.EnumDefinition{ 3: {Name: "C"}, }, "E_EnumModule_Child_InlineEnum": { - 1: {Name: "ADENINE"}, - 2: {Name: "THYMINE"}, - 3: {Name: "CYTOSINE"}, - 4: {Name: "GUANINE"}, + 11: {Name: "ADENINE"}, + 12: {Name: "THYMINE"}, + 13: {Name: "CYTOSINE"}, + 14: {Name: "GUANINE"}, }, "E_EnumModule_EnumModule_Cl": { 1: {Name: "X"}, diff --git a/gogen/testdata/structs/enum-module.long-enum-names.residing-module-typedef-enum-name.wrapper-unions.formatted-txt b/gogen/testdata/structs/enum-module.long-enum-names.residing-module-typedef-enum-name.wrapper-unions.formatted-txt index 0a5063a18..1af0048fb 100644 --- a/gogen/testdata/structs/enum-module.long-enum-names.residing-module-typedef-enum-name.wrapper-unions.formatted-txt +++ b/gogen/testdata/structs/enum-module.long-enum-names.residing-module-typedef-enum-name.wrapper-unions.formatted-txt @@ -296,13 +296,13 @@ const ( // EnumModule_Child_InlineEnum_UNSET corresponds to the value UNSET of EnumModule_Child_InlineEnum EnumModule_Child_InlineEnum_UNSET E_EnumModule_Child_InlineEnum = 0 // EnumModule_Child_InlineEnum_ADENINE corresponds to the value ADENINE of EnumModule_Child_InlineEnum - EnumModule_Child_InlineEnum_ADENINE E_EnumModule_Child_InlineEnum = 1 + EnumModule_Child_InlineEnum_ADENINE E_EnumModule_Child_InlineEnum = 11 // EnumModule_Child_InlineEnum_THYMINE corresponds to the value THYMINE of EnumModule_Child_InlineEnum - EnumModule_Child_InlineEnum_THYMINE E_EnumModule_Child_InlineEnum = 2 + EnumModule_Child_InlineEnum_THYMINE E_EnumModule_Child_InlineEnum = 12 // EnumModule_Child_InlineEnum_CYTOSINE corresponds to the value CYTOSINE of EnumModule_Child_InlineEnum - EnumModule_Child_InlineEnum_CYTOSINE E_EnumModule_Child_InlineEnum = 3 + EnumModule_Child_InlineEnum_CYTOSINE E_EnumModule_Child_InlineEnum = 13 // EnumModule_Child_InlineEnum_GUANINE corresponds to the value GUANINE of EnumModule_Child_InlineEnum - EnumModule_Child_InlineEnum_GUANINE E_EnumModule_Child_InlineEnum = 4 + EnumModule_Child_InlineEnum_GUANINE E_EnumModule_Child_InlineEnum = 14 ) // E_EnumModule_EnumModule_Cl is a derived int64 type which is used to represent @@ -409,10 +409,10 @@ var ΛEnum = map[string]map[int64]ygot.EnumDefinition{ 3: {Name: "C"}, }, "E_EnumModule_Child_InlineEnum": { - 1: {Name: "ADENINE"}, - 2: {Name: "THYMINE"}, - 3: {Name: "CYTOSINE"}, - 4: {Name: "GUANINE"}, + 11: {Name: "ADENINE"}, + 12: {Name: "THYMINE"}, + 13: {Name: "CYTOSINE"}, + 14: {Name: "GUANINE"}, }, "E_EnumModule_EnumModule_Cl": { 1: {Name: "X"}, diff --git a/gogen/testdata/structs/enum-module.residing-module-typedef-enum-name.formatted-txt b/gogen/testdata/structs/enum-module.residing-module-typedef-enum-name.formatted-txt index 8a0b4bd25..eae32101c 100644 --- a/gogen/testdata/structs/enum-module.residing-module-typedef-enum-name.formatted-txt +++ b/gogen/testdata/structs/enum-module.residing-module-typedef-enum-name.formatted-txt @@ -315,13 +315,13 @@ const ( // Child_InlineEnum_UNSET corresponds to the value UNSET of Child_InlineEnum Child_InlineEnum_UNSET E_Child_InlineEnum = 0 // Child_InlineEnum_ADENINE corresponds to the value ADENINE of Child_InlineEnum - Child_InlineEnum_ADENINE E_Child_InlineEnum = 1 + Child_InlineEnum_ADENINE E_Child_InlineEnum = 11 // Child_InlineEnum_THYMINE corresponds to the value THYMINE of Child_InlineEnum - Child_InlineEnum_THYMINE E_Child_InlineEnum = 2 + Child_InlineEnum_THYMINE E_Child_InlineEnum = 12 // Child_InlineEnum_CYTOSINE corresponds to the value CYTOSINE of Child_InlineEnum - Child_InlineEnum_CYTOSINE E_Child_InlineEnum = 3 + Child_InlineEnum_CYTOSINE E_Child_InlineEnum = 13 // Child_InlineEnum_GUANINE corresponds to the value GUANINE of Child_InlineEnum - Child_InlineEnum_GUANINE E_Child_InlineEnum = 4 + Child_InlineEnum_GUANINE E_Child_InlineEnum = 14 ) // E_EnumModule_Cl is a derived int64 type which is used to represent @@ -428,10 +428,10 @@ var ΛEnum = map[string]map[int64]ygot.EnumDefinition{ 3: {Name: "C"}, }, "E_Child_InlineEnum": { - 1: {Name: "ADENINE"}, - 2: {Name: "THYMINE"}, - 3: {Name: "CYTOSINE"}, - 4: {Name: "GUANINE"}, + 11: {Name: "ADENINE"}, + 12: {Name: "THYMINE"}, + 13: {Name: "CYTOSINE"}, + 14: {Name: "GUANINE"}, }, "E_EnumModule_Cl": { 1: {Name: "X"}, diff --git a/gogen/testdata/structs/enum-module.wrapper-unions.formatted-txt b/gogen/testdata/structs/enum-module.wrapper-unions.formatted-txt index 3c333c931..0033022e2 100644 --- a/gogen/testdata/structs/enum-module.wrapper-unions.formatted-txt +++ b/gogen/testdata/structs/enum-module.wrapper-unions.formatted-txt @@ -346,13 +346,13 @@ const ( // Child_InlineEnum_UNSET corresponds to the value UNSET of Child_InlineEnum Child_InlineEnum_UNSET E_Child_InlineEnum = 0 // Child_InlineEnum_ADENINE corresponds to the value ADENINE of Child_InlineEnum - Child_InlineEnum_ADENINE E_Child_InlineEnum = 1 + Child_InlineEnum_ADENINE E_Child_InlineEnum = 11 // Child_InlineEnum_THYMINE corresponds to the value THYMINE of Child_InlineEnum - Child_InlineEnum_THYMINE E_Child_InlineEnum = 2 + Child_InlineEnum_THYMINE E_Child_InlineEnum = 12 // Child_InlineEnum_CYTOSINE corresponds to the value CYTOSINE of Child_InlineEnum - Child_InlineEnum_CYTOSINE E_Child_InlineEnum = 3 + Child_InlineEnum_CYTOSINE E_Child_InlineEnum = 13 // Child_InlineEnum_GUANINE corresponds to the value GUANINE of Child_InlineEnum - Child_InlineEnum_GUANINE E_Child_InlineEnum = 4 + Child_InlineEnum_GUANINE E_Child_InlineEnum = 14 ) // E_EnumModule_Cl is a derived int64 type which is used to represent @@ -480,10 +480,10 @@ const ( // valid YANG identifier. var ΛEnum = map[string]map[int64]ygot.EnumDefinition{ "E_Child_InlineEnum": { - 1: {Name: "ADENINE"}, - 2: {Name: "THYMINE"}, - 3: {Name: "CYTOSINE"}, - 4: {Name: "GUANINE"}, + 11: {Name: "ADENINE"}, + 12: {Name: "THYMINE"}, + 13: {Name: "CYTOSINE"}, + 14: {Name: "GUANINE"}, }, "E_EnumModule_Cl": { 1: {Name: "X"}, diff --git a/testdata/modules/enum-module.yang b/testdata/modules/enum-module.yang index c1f691b81..bc7c3b13a 100644 --- a/testdata/modules/enum-module.yang +++ b/testdata/modules/enum-module.yang @@ -43,7 +43,7 @@ module enum-module { leaf inline-enum { type enumeration { - enum ADENINE; + enum ADENINE { value 10; } enum THYMINE; enum CYTOSINE; enum GUANINE; diff --git a/ygen/genir.go b/ygen/genir.go index 1f69508eb..bd920d0b0 100644 --- a/ygen/genir.go +++ b/ygen/genir.go @@ -143,10 +143,11 @@ func GenerateIR(yangFiles, includePaths []string, langMapper LangMapper, opts IR } sort.Strings(valNames) - for _, v := range valNames { + for i, v := range valNames { et.ValToYANGDetails = append(et.ValToYANGDetails, ygot.EnumDefinition{ Name: v, DefiningModule: genutil.ParentModuleName(valLookup[v]), + Value: i, }) } default: @@ -154,13 +155,14 @@ func GenerateIR(yangFiles, includePaths []string, langMapper LangMapper, opts IR // 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 - for v := range enum.entry.Type.Enum.ValueMap() { + 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: enum.entry.Type.Enum.ValueMap()[int64(v)], + Name: valueMap[int64(v)], Value: v, }) } diff --git a/ygot/types.go b/ygot/types.go index e36da03c1..0c96d4370 100644 --- a/ygot/types.go +++ b/ygot/types.go @@ -141,7 +141,17 @@ type EnumDefinition struct { // DefiningModule specifies the module within which the enumeration was // defined. Only populated for identity values. DefiningModule string - // Value specifies the value of an "enumeration" type. + // Value is an optionally-populated field that specifies the value of + // an enumerated type. + // + // TODO: Consider removing this field and using a custom type in the + // ygen package since only the IR generation populates this field. + // + // When populated, the following values are recommended: + // For enumerations, this value is determined by goyang. + // For identityrefs, this value is determined by the lexicographical + // ordering of the identityref name, starting with 0 to be consistent + // with goyang's enumeration numbering. Value int }