Skip to content

Commit

Permalink
Merge pull request #23 from ythadhani/enum_vals_gen_code
Browse files Browse the repository at this point in the history
fix code generation to contain enum vals from YANG
  • Loading branch information
ythadhani authored Apr 14, 2023
2 parents ca8e702 + 975c72e commit 49777d1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
6 changes: 3 additions & 3 deletions ygen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,9 @@ func genGoEnumeratedTypes(enums map[string]*EnumeratedYANGType) (map[string]*goE

switch e.Kind {
case IdentityType, SimpleEnumerationType, DerivedEnumerationType, UnionEnumerationType, 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
6 changes: 4 additions & 2 deletions ygen/genir.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,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:
Expand All @@ -175,7 +176,8 @@ func GenerateIR(yangFiles, includePaths []string, langMapper LangMapper, opts IR
sort.Ints(values)
for _, v := range values {
et.ValToYANGDetails = append(et.ValToYANGDetails, ygot.EnumDefinition{
Name: enum.entry.Type.Enum.ValueMap()[int64(v)],
Name: enum.entry.Type.Enum.ValueMap()[int64(v)],
Value: v,
})
}
}
Expand Down
24 changes: 19 additions & 5 deletions ygen/genir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,11 @@ func TestGenerateIR(t *testing.T) {
Kind: SimpleEnumerationType,
TypeName: "enumeration",
ValToYANGDetails: []ygot.EnumDefinition{{
Name: "ONE",
Name: "ONE",
Value: 0,
}, {
Name: "TWO",
Name: "TWO",
Value: 1,
}},
},
},
Expand Down Expand Up @@ -610,9 +612,11 @@ func TestGenerateIR(t *testing.T) {
Kind: SimpleEnumerationType,
TypeName: "enumeration",
ValToYANGDetails: []ygot.EnumDefinition{{
Name: "ONE",
Name: "ONE",
Value: 0,
}, {
Name: "TWO",
Name: "TWO",
Value: 1,
}},
},
},
Expand Down Expand Up @@ -1129,7 +1133,7 @@ func TestGenerateIR(t *testing.T) {
Name: "Simple_Parent_Child_Config_Three",
Kind: 1,
TypeName: "enumeration",
ValToYANGDetails: []ygot.EnumDefinition{{Name: "ONE"}, {Name: "TWO"}},
ValToYANGDetails: []ygot.EnumDefinition{{Name: "ONE", Value: 0}, {Name: "TWO", Value: 1}},
},
},
ModelData: []*gpb.ModelData{{Name: "openconfig-remote"}, {Name: "openconfig-simple"}, {Name: "openconfig-simple-augment2"}, {Name: "openconfig-simple-grouping"}},
Expand Down Expand Up @@ -1695,10 +1699,12 @@ func TestGenerateIR(t *testing.T) {
{
Name: "NORMAL",
DefiningModule: "",
Value: 0,
},
{
Name: "SUPER",
DefiningModule: "",
Value: 1,
},
},
},
Expand Down Expand Up @@ -1726,10 +1732,12 @@ func TestGenerateIR(t *testing.T) {
{
Name: "SATURDAY",
DefiningModule: "",
Value: 0,
},
{
Name: "SUNDAY",
DefiningModule: "",
Value: 1,
},
},
},
Expand All @@ -1749,14 +1757,17 @@ func TestGenerateIR(t *testing.T) {
{
Name: "ONE",
DefiningModule: "",
Value: 0,
},
{
Name: "TWO",
DefiningModule: "",
Value: 1,
},
{
Name: "THREE",
DefiningModule: "",
Value: 2,
},
},
},
Expand All @@ -1776,14 +1787,17 @@ func TestGenerateIR(t *testing.T) {
{
Name: "UN",
DefiningModule: "",
Value: 0,
},
{
Name: "DEUX",
DefiningModule: "",
Value: 1,
},
{
Name: "TROIS",
DefiningModule: "",
Value: 2,
},
},
},
Expand Down
6 changes: 6 additions & 0 deletions ygen/gogen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1914,14 +1914,17 @@ func TestGenGoEnumeratedTypes(t *testing.T) {
{
Name: "VALUE_A",
DefiningModule: "",
Value: 0,
},
{
Name: "VALUE_B",
DefiningModule: "",
Value: 1,
},
{
Name: "VALUE_C",
DefiningModule: "",
Value: 2,
},
},
},
Expand All @@ -1939,14 +1942,17 @@ func TestGenGoEnumeratedTypes(t *testing.T) {
1: {
Name: "VALUE_A",
DefiningModule: "",
Value: 0,
},
2: {
Name: "VALUE_B",
DefiningModule: "",
Value: 1,
},
3: {
Name: "VALUE_C",
DefiningModule: "",
Value: 2,
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions ygot/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ 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 int
}

// Annotation defines an interface that is implemented by optional metadata
Expand Down

0 comments on commit 49777d1

Please sign in to comment.