Skip to content

Commit 69db7ad

Browse files
wenovusrobshakir
andauthored
Add MappedType.UnionTypeInfos (#670)
* Add code to generate IR (not used). `buildDirectoryDefinitions` and `findEnumSet` have now been converted to be callable by `GenerateIR`. Later PRs will convert ypathgen, Go, and proto generation to use the IR instead of the Directory map. * Fix underscore generation * Simplify IR a bit * Rename goGenState to GoLangMapper. This name is exported since it will be accessed by generator.go after the refactoring is done. The genFakeRoot parameter is also removed since this property can be checked through other means, as is done in https://github.com/openconfig/ygot/tree/ygen-ir-langmapper * Update IR to Support Go Generation Various updates to the IR for better supporting Go generation. * Fix staticcheck * Convert `GenerateGoCode` to use IR for Code Generation. * Delete some no-longer used code * Make newGoLangMapper exported * Fix comment in ygen/codegen.go Co-authored-by: Rob Shakir <[email protected]> * Improve style * gofmt * Update comment in ygen/directory.go Co-authored-by: Rob Shakir <[email protected]> * Add comments that LangMapper should be idempotent * Add comment * Add presence container IR handling (merge conflict) * Add new Fields to IR to support path API generation * Remove useless line in ypathgen * Generate Path API using IR. * Remove unused code * Remove YANGNodeDetails.Kind and add clarifying comments * Remove usage of YANGNodeDetails.Kind * textual -> text * rename RootModule -> RootElementModule * fix name * Improve doc comments on BelongingModule * Remove useless line in ypathgen (#656) * Change name to LeafrefTargetPath * [backwards-incompatible] Delete enum flags for `proto_generator` These flags make IR generation more complex (maybe a day or so of work), so I want to remove them now a bit early ahead of v1 release to avoid the throwaway work. * More accurate enum naming and classification according to namesake type. Currently for unions it uses the outer-most union for the name and the type classification, but it should rather use the namesake union instead. * Rename protoGenState to ProtoLangMapper * Change `IR.Enums` map's key to be a unique key instead of the enum's name. * Add `MappedType.UnionTypeInfos` This will be used to store the key to the global enum map in the `IR`. * Update ygen/genstate.go Co-authored-by: Rob Shakir <[email protected]> Co-authored-by: Rob Shakir <[email protected]>
1 parent 8e1465d commit 69db7ad

File tree

2 files changed

+80
-13
lines changed

2 files changed

+80
-13
lines changed

ygen/genstate.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ type MappedType struct {
3939
// NativeType is the candidate language-specific type name which is to
4040
// be used for the mapped entity.
4141
NativeType string
42-
// EnumeratedYANGTypeName stores a globally-unique name that can be
43-
// used to key into IR.EnumeratedYANGTypes containing all of the
44-
// enumeration definitions. This value should be populated when
42+
// EnumeratedYANGTypeKey stores a globally-unique key that can be
43+
// used to key into IR's EnumeratedYANGTypes map containing all of the
44+
// enumeration definitions. This value should only be populated when
4545
// IsEnumeratedValue is true.
46-
//EnumeratedYANGTypeName string
46+
//EnumeratedYANGTypeKey string
4747
// UnionTypes is a map, keyed by the generated type name, of the types
4848
// specified as valid for a union. The value of the map indicates the
4949
// order of the type, since order is important for unions in YANG.
@@ -52,6 +52,9 @@ type MappedType struct {
5252
// the generated code from the structs maintains only type validation,
5353
// this is not currently a limitation.
5454
UnionTypes map[string]int
55+
// UnionTypeInfos stores more information about each union subtype that
56+
// can be populated if needed during the code generation pass.
57+
UnionTypeInfos map[string]MappedUnionSubtype
5558
// IsEnumeratedValue specifies whether the NativeType that is returned
5659
// is a generated enumerated value. Such entities are reflected as
5760
// derived types with constant values, and are hence not represented
@@ -67,6 +70,16 @@ type MappedType struct {
6770
DefaultValue *string
6871
}
6972

73+
// MappedUnionSubtype stores information associated with a union subtype within
74+
// a MappedType.
75+
type MappedUnionSubtype struct {
76+
// EnumeratedYANGTypeKey stores a globally-unique key that can be
77+
// used to key into IR's EnumeratedYANGTypes map containing all of the
78+
// enumeration definitions. This value should only be populated when
79+
// the union subtype is an enumerated type.
80+
//EnumeratedYANGTypeKey string
81+
}
82+
7083
// IsYgenDefinedGoType returns true if the native type of a MappedType is a Go
7184
// type that's defined by ygen's generated code.
7285
func IsYgenDefinedGoType(t *MappedType) bool {

ygen/goelements_test.go

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ func TestUnionSubTypes(t *testing.T) {
5858
},
5959
want: []string{"string"},
6060
wantMtypes: map[int]*MappedType{
61-
0: {"string", nil, false, goZeroValues["string"], nil},
61+
0: {
62+
NativeType: "string",
63+
UnionTypes: nil,
64+
IsEnumeratedValue: false,
65+
ZeroValue: goZeroValues["string"],
66+
DefaultValue: nil,
67+
},
6268
},
6369
}, {
6470
name: "union of int8, string",
@@ -75,8 +81,20 @@ func TestUnionSubTypes(t *testing.T) {
7581
},
7682
want: []string{"int8", "string"},
7783
wantMtypes: map[int]*MappedType{
78-
0: {"int8", nil, false, goZeroValues["int8"], nil},
79-
1: {"string", nil, false, goZeroValues["string"], nil},
84+
0: {
85+
NativeType: "int8",
86+
UnionTypes: nil,
87+
IsEnumeratedValue: false,
88+
ZeroValue: goZeroValues["int8"],
89+
DefaultValue: nil,
90+
},
91+
1: {
92+
NativeType: "string",
93+
UnionTypes: nil,
94+
IsEnumeratedValue: false,
95+
ZeroValue: goZeroValues["string"],
96+
DefaultValue: nil,
97+
},
8098
},
8199
}, {
82100
name: "union of unions",
@@ -105,10 +123,34 @@ func TestUnionSubTypes(t *testing.T) {
105123
},
106124
want: []string{"string", "int32", "uint64", "int16"},
107125
wantMtypes: map[int]*MappedType{
108-
0: {"string", nil, false, goZeroValues["string"], nil},
109-
1: {"int32", nil, false, goZeroValues["int32"], nil},
110-
2: {"uint64", nil, false, goZeroValues["uint64"], nil},
111-
3: {"int16", nil, false, goZeroValues["int16"], nil},
126+
0: {
127+
NativeType: "string",
128+
UnionTypes: nil,
129+
IsEnumeratedValue: false,
130+
ZeroValue: goZeroValues["string"],
131+
DefaultValue: nil,
132+
},
133+
1: {
134+
NativeType: "int32",
135+
UnionTypes: nil,
136+
IsEnumeratedValue: false,
137+
ZeroValue: goZeroValues["int32"],
138+
DefaultValue: nil,
139+
},
140+
2: {
141+
NativeType: "uint64",
142+
UnionTypes: nil,
143+
IsEnumeratedValue: false,
144+
ZeroValue: goZeroValues["uint64"],
145+
DefaultValue: nil,
146+
},
147+
3: {
148+
NativeType: "int16",
149+
UnionTypes: nil,
150+
IsEnumeratedValue: false,
151+
ZeroValue: goZeroValues["int16"],
152+
DefaultValue: nil,
153+
},
112154
},
113155
}, {
114156
name: "erroneous union without context",
@@ -342,8 +384,20 @@ func TestUnionSubTypes(t *testing.T) {
342384
},
343385
want: []string{"E_Basemod_Id", "E_Basemod2_Id2"},
344386
wantMtypes: map[int]*MappedType{
345-
0: {"E_Basemod_Id", nil, true, "0", nil},
346-
1: {"E_Basemod2_Id2", nil, true, "0", nil},
387+
0: {
388+
NativeType: "E_Basemod_Id",
389+
UnionTypes: nil,
390+
IsEnumeratedValue: true,
391+
ZeroValue: "0",
392+
DefaultValue: nil,
393+
},
394+
1: {
395+
NativeType: "E_Basemod2_Id2",
396+
UnionTypes: nil,
397+
IsEnumeratedValue: true,
398+
ZeroValue: "0",
399+
DefaultValue: nil,
400+
},
347401
},
348402
}, {
349403
name: "union of single identityref",

0 commit comments

Comments
 (0)