Skip to content

Commit

Permalink
Revert enum number generation behaviour to pre-v0.19.0 (#814)
Browse files Browse the repository at this point in the history
* Refactor gogen by moving around functions/tests a bit

* Revert enum number generation behaviour to pre-v0.19.0
  • Loading branch information
wenovus authored Apr 14, 2023
1 parent bde3c08 commit 2f52ba6
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 56 deletions.
6 changes: 3 additions & 3 deletions gogen/goenums.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions gogen/testdata/structs/enum-module.formatted-txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"},
Expand Down
16 changes: 8 additions & 8 deletions gogen/testdata/structs/enum-module.long-enum-names.formatted-txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"},
Expand Down
16 changes: 8 additions & 8 deletions gogen/testdata/structs/enum-module.wrapper-unions.formatted-txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"},
Expand Down
2 changes: 1 addition & 1 deletion testdata/modules/enum-module.yang
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module enum-module {

leaf inline-enum {
type enumeration {
enum ADENINE;
enum ADENINE { value 10; }
enum THYMINE;
enum CYTOSINE;
enum GUANINE;
Expand Down
8 changes: 5 additions & 3 deletions ygen/genir.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,26 @@ 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:
// 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
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,
})
}
Expand Down
12 changes: 11 additions & 1 deletion ygot/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 2f52ba6

Please sign in to comment.