Skip to content
Permalink
Browse files

Export remaining MappedType fields (#311)

* Export remaining MappedType fields
  • Loading branch information...
wenovus committed Sep 6, 2019
1 parent 88a454d commit fd57a444e8fb32a8e8d1f9e5184b59ae04c406b4
Showing with 112 additions and 112 deletions.
  1. +1 −1 ygen/genstate.go
  2. +34 −34 ygen/goelements.go
  3. +42 −42 ygen/goelements_test.go
  4. +9 −9 ygen/gogen.go
  5. +2 −2 ygen/gogen_test.go
  6. +8 −8 ygen/protoelements.go
  7. +8 −8 ygen/protoelements_test.go
  8. +4 −4 ygen/protogen.go
  9. +4 −4 ygen/protogen_test.go
@@ -592,7 +592,7 @@ func (s *genState) enumeratedTypedefTypeName(args resolveTypeArgs, prefix string

return &MappedType{
NativeType: fmt.Sprintf("%s%s", prefix, tn),
isEnumeratedValue: true,
IsEnumeratedValue: true,
}, nil
}
}
@@ -75,7 +75,7 @@ var (
)

// MappedType is used to store the Go type that a leaf entity in YANG is
// mapped to. The NativeType is always populated for any leaf. unionTypes is populated
// mapped to. The NativeType is always populated for any leaf. UnionTypes is populated
// when the type may have subtypes (i.e., is a union). enumValues is populated
// when the type is an enumerated type.
//
@@ -86,27 +86,27 @@ var (
type MappedType struct {
// NativeType is the type which is to be used for the mapped entity.
NativeType string
// unionTypes is a map, keyed by the Go type, of the types specified
// UnionTypes is a map, keyed by the Go type, of the types specified
// as valid for a union. The value of the map indicates the order
// of the type, since order is important for unions in YANG. Where
// two types are mapped to the same Go type (e.g., string) then
// only the order of the first is maintained. Since the generated
// code from the structs maintains only type validation, this
// is not currently a limitation.
unionTypes map[string]int
// isEnumeratedValue specifies whether the NativeType that is returned
UnionTypes map[string]int
// IsEnumeratedValue specifies whether the NativeType that is returned
// is a generated enumerated value. Such entities are reflected as
// derived types with constant values, and are hence not represented
// as pointers in the output code.
isEnumeratedValue bool
// zeroValue stores the value that should be used for the type if
IsEnumeratedValue bool
// ZeroValue stores the value that should be used for the type if
// it is unset. This is used only in contexts where the nil pointer
// cannot be used, such as leaf getters.
zeroValue string
// defaultValue stores the default value for the type if is specified.
ZeroValue string
// DefaultValue stores the default value for the type if is specified.
// It is represented as a string pointer to ensure that default values
// of the empty string can be distinguished from unset defaults.
defaultValue *string
DefaultValue *string
}

// resolveTypeArgs is a structure used as an input argument to the yangTypeToGoType
@@ -205,9 +205,9 @@ func (s *genState) yangTypeToGoType(args resolveTypeArgs, compressOCPaths bool)
// mtype is set to non-nil when this was a valid enumeration
// within a typedef. We explicitly set the zero and default values
// here.
mtype.zeroValue = "0"
mtype.ZeroValue = "0"
if defVal != nil {
mtype.defaultValue = enumDefaultValue(mtype.NativeType, *defVal, goEnumPrefix)
mtype.DefaultValue = enumDefaultValue(mtype.NativeType, *defVal, goEnumPrefix)
}

return mtype, nil
@@ -216,31 +216,31 @@ func (s *genState) yangTypeToGoType(args resolveTypeArgs, compressOCPaths bool)
// Perform the actual mapping of the type to the Go type.
switch args.yangType.Kind {
case yang.Yint8:
return &MappedType{NativeType: "int8", zeroValue: goZeroValues["int8"], defaultValue: defVal}, nil
return &MappedType{NativeType: "int8", ZeroValue: goZeroValues["int8"], DefaultValue: defVal}, nil
case yang.Yint16:
return &MappedType{NativeType: "int16", zeroValue: goZeroValues["int16"], defaultValue: defVal}, nil
return &MappedType{NativeType: "int16", ZeroValue: goZeroValues["int16"], DefaultValue: defVal}, nil
case yang.Yint32:
return &MappedType{NativeType: "int32", zeroValue: goZeroValues["int32"], defaultValue: defVal}, nil
return &MappedType{NativeType: "int32", ZeroValue: goZeroValues["int32"], DefaultValue: defVal}, nil
case yang.Yint64:
return &MappedType{NativeType: "int64", zeroValue: goZeroValues["int64"], defaultValue: defVal}, nil
return &MappedType{NativeType: "int64", ZeroValue: goZeroValues["int64"], DefaultValue: defVal}, nil
case yang.Yuint8:
return &MappedType{NativeType: "uint8", zeroValue: goZeroValues["uint8"], defaultValue: defVal}, nil
return &MappedType{NativeType: "uint8", ZeroValue: goZeroValues["uint8"], DefaultValue: defVal}, nil
case yang.Yuint16:
return &MappedType{NativeType: "uint16", zeroValue: goZeroValues["uint16"], defaultValue: defVal}, nil
return &MappedType{NativeType: "uint16", ZeroValue: goZeroValues["uint16"], DefaultValue: defVal}, nil
case yang.Yuint32:
return &MappedType{NativeType: "uint32", zeroValue: goZeroValues["uint32"], defaultValue: defVal}, nil
return &MappedType{NativeType: "uint32", ZeroValue: goZeroValues["uint32"], DefaultValue: defVal}, nil
case yang.Yuint64:
return &MappedType{NativeType: "uint64", zeroValue: goZeroValues["uint64"], defaultValue: defVal}, nil
return &MappedType{NativeType: "uint64", ZeroValue: goZeroValues["uint64"], DefaultValue: defVal}, nil
case yang.Ybool:
return &MappedType{NativeType: "bool", zeroValue: goZeroValues["bool"], defaultValue: defVal}, nil
return &MappedType{NativeType: "bool", ZeroValue: goZeroValues["bool"], DefaultValue: defVal}, nil
case yang.Yempty:
// Empty is a YANG type that either exists or doesn't, therefore
// map it to a boolean to indicate its presence or not. The empty
// type name uses a specific name in the generated code, such that
// it can be identified for marshalling.
return &MappedType{NativeType: ygot.EmptyTypeName, zeroValue: goZeroValues[ygot.EmptyTypeName]}, nil
return &MappedType{NativeType: ygot.EmptyTypeName, ZeroValue: goZeroValues[ygot.EmptyTypeName]}, nil
case yang.Ystring:
return &MappedType{NativeType: "string", zeroValue: goZeroValues["string"], defaultValue: defVal}, nil
return &MappedType{NativeType: "string", ZeroValue: goZeroValues["string"], DefaultValue: defVal}, nil
case yang.Yunion:
// A YANG Union is a leaf that can take multiple values - its subtypes need
// to be extracted.
@@ -258,9 +258,9 @@ func (s *genState) yangTypeToGoType(args resolveTypeArgs, compressOCPaths bool)
}
return &MappedType{
NativeType: fmt.Sprintf("E_%s", n),
isEnumeratedValue: true,
zeroValue: "0",
defaultValue: defVal,
IsEnumeratedValue: true,
ZeroValue: "0",
DefaultValue: defVal,
}, nil
case yang.Yidentityref:
// Identityref leaves are mapped according to the base identity that they
@@ -275,12 +275,12 @@ func (s *genState) yangTypeToGoType(args resolveTypeArgs, compressOCPaths bool)
}
return &MappedType{
NativeType: fmt.Sprintf("E_%s", n),
isEnumeratedValue: true,
zeroValue: "0",
defaultValue: defVal,
IsEnumeratedValue: true,
ZeroValue: "0",
DefaultValue: defVal,
}, nil
case yang.Ydecimal64:
return &MappedType{NativeType: "float64", zeroValue: goZeroValues["float64"]}, nil
return &MappedType{NativeType: "float64", ZeroValue: goZeroValues["float64"]}, nil
case yang.Yleafref:
// This is a leafref, so we check what the type of the leaf that it
// references is by looking it up in the schematree.
@@ -297,13 +297,13 @@ func (s *genState) yangTypeToGoType(args resolveTypeArgs, compressOCPaths bool)
// Map binary fields to the Binary type defined in the output code,
// this is used to ensure that we can distinguish a binary field from
// a leaf-list of uint8s which is not possible if mapping to []byte.
return &MappedType{NativeType: ygot.BinaryTypeName, zeroValue: goZeroValues[ygot.BinaryTypeName], defaultValue: defVal}, nil
return &MappedType{NativeType: ygot.BinaryTypeName, ZeroValue: goZeroValues[ygot.BinaryTypeName], DefaultValue: defVal}, nil
default:
// Return an empty interface for the types that we do not currently
// support. Back-end validation is required for these types.
// TODO(robjs): Missing types currently bits. These
// should be added.
return &MappedType{NativeType: "interface{}", zeroValue: goZeroValues["interface{}"]}, nil
return &MappedType{NativeType: "interface{}", ZeroValue: goZeroValues["interface{}"]}, nil
}
}

@@ -370,8 +370,8 @@ func (s *genState) goUnionType(args resolveTypeArgs, compressOCPaths bool) (*Map

return &MappedType{
NativeType: NativeType,
unionTypes: unionTypes,
zeroValue: zeroValue,
UnionTypes: unionTypes,
ZeroValue: zeroValue,
}, nil
}

@@ -402,7 +402,7 @@ func (s *genState) goUnionSubTypes(subtype *yang.YangType, ctx *yang.Entry, curr
// leaf that refers to the union, not the specific subtype that is now being examined.
mtype = &MappedType{
NativeType: fmt.Sprintf("E_%s", s.identityrefBaseTypeFromIdentity(subtype.IdentityBase, false)),
zeroValue: "0",
ZeroValue: "0",
}
default:
var err error
@@ -174,43 +174,43 @@ func TestYangTypeToGoType(t *testing.T) {
}{{
name: "simple lookup resolution",
in: &yang.YangType{Kind: yang.Yint32, Name: "int32"},
want: &MappedType{NativeType: "int32", zeroValue: "0"},
want: &MappedType{NativeType: "int32", ZeroValue: "0"},
}, {
name: "int32 with default",
in: &yang.YangType{Kind: yang.Yint32, Name: "int32", Default: "42"},
want: &MappedType{NativeType: "int32", zeroValue: "0", defaultValue: ygot.String("42")},
want: &MappedType{NativeType: "int32", ZeroValue: "0", DefaultValue: ygot.String("42")},
}, {
name: "decimal64",
in: &yang.YangType{Kind: yang.Ydecimal64, Name: "decimal64"},
want: &MappedType{NativeType: "float64", zeroValue: "0.0"},
want: &MappedType{NativeType: "float64", ZeroValue: "0.0"},
}, {
name: "binary lookup resolution",
in: &yang.YangType{Kind: yang.Ybinary, Name: "binary"},
want: &MappedType{NativeType: "Binary", zeroValue: "nil"},
want: &MappedType{NativeType: "Binary", ZeroValue: "nil"},
}, {
name: "unknown lookup resolution",
in: &yang.YangType{Kind: yang.YinstanceIdentifier, Name: "instanceIdentifier"},
want: &MappedType{NativeType: "interface{}", zeroValue: "nil"},
want: &MappedType{NativeType: "interface{}", ZeroValue: "nil"},
}, {
name: "simple empty resolution",
in: &yang.YangType{Kind: yang.Yempty, Name: "empty"},
want: &MappedType{NativeType: "YANGEmpty", zeroValue: "false"},
want: &MappedType{NativeType: "YANGEmpty", ZeroValue: "false"},
}, {
name: "simple boolean resolution",
in: &yang.YangType{Kind: yang.Ybool, Name: "bool"},
want: &MappedType{NativeType: "bool", zeroValue: "false"},
want: &MappedType{NativeType: "bool", ZeroValue: "false"},
}, {
name: "simple int64 resolution",
in: &yang.YangType{Kind: yang.Yint64, Name: "int64"},
want: &MappedType{NativeType: "int64", zeroValue: "0"},
want: &MappedType{NativeType: "int64", ZeroValue: "0"},
}, {
name: "simple uint8 resolution",
in: &yang.YangType{Kind: yang.Yuint8, Name: "uint8"},
want: &MappedType{NativeType: "uint8", zeroValue: "0"},
want: &MappedType{NativeType: "uint8", ZeroValue: "0"},
}, {
name: "simple uint16 resolution",
in: &yang.YangType{Kind: yang.Yuint16, Name: "uint16"},
want: &MappedType{NativeType: "uint16", zeroValue: "0"},
want: &MappedType{NativeType: "uint16", ZeroValue: "0"},
}, {
name: "leafref without valid path",
in: &yang.YangType{Kind: yang.Yleafref, Name: "leafref"},
@@ -257,8 +257,8 @@ func TestYangTypeToGoType(t *testing.T) {
},
want: &MappedType{
NativeType: "Module_Container_Leaf_Union",
unionTypes: map[string]int{"string": 0, "int8": 1},
zeroValue: "nil",
UnionTypes: map[string]int{"string": 0, "int8": 1},
ZeroValue: "nil",
},
}, {
name: "string-only union",
@@ -271,8 +271,8 @@ func TestYangTypeToGoType(t *testing.T) {
},
want: &MappedType{
NativeType: "string",
unionTypes: map[string]int{"string": 0},
zeroValue: `""`,
UnionTypes: map[string]int{"string": 0},
ZeroValue: `""`,
},
}, {
name: "derived identityref",
@@ -291,8 +291,8 @@ func TestYangTypeToGoType(t *testing.T) {
},
want: &MappedType{
NativeType: "E_BaseModule_DerivedIdentityref",
isEnumeratedValue: true,
zeroValue: "0",
IsEnumeratedValue: true,
ZeroValue: "0",
},
}, {
name: "derived identityref",
@@ -311,9 +311,9 @@ func TestYangTypeToGoType(t *testing.T) {
},
want: &MappedType{
NativeType: "E_BaseModule_DerivedIdentityref",
isEnumeratedValue: true,
zeroValue: "0",
defaultValue: ygot.String("BaseModule_DerivedIdentityref_AARDVARK"),
IsEnumeratedValue: true,
ZeroValue: "0",
DefaultValue: ygot.String("BaseModule_DerivedIdentityref_AARDVARK"),
},
}, {
name: "enumeration",
@@ -331,8 +331,8 @@ func TestYangTypeToGoType(t *testing.T) {
},
want: &MappedType{
NativeType: "E_BaseModule_EnumerationLeaf",
isEnumeratedValue: true,
zeroValue: "0",
IsEnumeratedValue: true,
ZeroValue: "0",
},
}, {
name: "enumeration with default",
@@ -350,9 +350,9 @@ func TestYangTypeToGoType(t *testing.T) {
},
want: &MappedType{
NativeType: "E_BaseModule_EnumerationLeaf",
isEnumeratedValue: true,
zeroValue: "0",
defaultValue: ygot.String("BaseModule_EnumerationLeaf_BLUE"),
IsEnumeratedValue: true,
ZeroValue: "0",
DefaultValue: ygot.String("BaseModule_EnumerationLeaf_BLUE"),
},
}, {
name: "typedef enumeration",
@@ -371,8 +371,8 @@ func TestYangTypeToGoType(t *testing.T) {
},
want: &MappedType{
NativeType: "E_BaseModule_DerivedEnumeration",
isEnumeratedValue: true,
zeroValue: "0",
IsEnumeratedValue: true,
ZeroValue: "0",
},
}, {
name: "typedef enumeration with default",
@@ -391,9 +391,9 @@ func TestYangTypeToGoType(t *testing.T) {
},
want: &MappedType{
NativeType: "E_BaseModule_DerivedEnumeration",
isEnumeratedValue: true,
zeroValue: "0",
defaultValue: ygot.String("BaseModule_DerivedEnumeration_FISH"),
IsEnumeratedValue: true,
ZeroValue: "0",
DefaultValue: ygot.String("BaseModule_DerivedEnumeration_FISH"),
},
}, {
name: "identityref",
@@ -417,8 +417,8 @@ func TestYangTypeToGoType(t *testing.T) {
},
want: &MappedType{
NativeType: "E_TestModule_BaseIdentity",
isEnumeratedValue: true,
zeroValue: "0",
IsEnumeratedValue: true,
ZeroValue: "0",
},
}, {
name: "identityref with default",
@@ -442,9 +442,9 @@ func TestYangTypeToGoType(t *testing.T) {
},
want: &MappedType{
NativeType: "E_TestModule_BaseIdentity",
isEnumeratedValue: true,
zeroValue: "0",
defaultValue: ygot.String("TestModule_BaseIdentity_CHIPS"),
IsEnumeratedValue: true,
ZeroValue: "0",
DefaultValue: ygot.String("TestModule_BaseIdentity_CHIPS"),
},
}, {
name: "enumeration with compress paths",
@@ -464,8 +464,8 @@ func TestYangTypeToGoType(t *testing.T) {
compressPath: true,
want: &MappedType{
NativeType: "E_BaseModule_Container_Eleaf",
isEnumeratedValue: true,
zeroValue: "0",
IsEnumeratedValue: true,
ZeroValue: "0",
},
}, {
name: "enumeration in submodule",
@@ -479,7 +479,7 @@ func TestYangTypeToGoType(t *testing.T) {
},
},
compressPath: true,
want: &MappedType{NativeType: "E_BaseMod_Container_Eleaf", isEnumeratedValue: true, zeroValue: "0"},
want: &MappedType{NativeType: "E_BaseMod_Container_Eleaf", IsEnumeratedValue: true, ZeroValue: "0"},
}, {
name: "leafref",
in: &yang.YangType{Kind: yang.Yleafref, Name: "leafref", Path: "../c"},
@@ -521,7 +521,7 @@ func TestYangTypeToGoType(t *testing.T) {
Parent: &yang.Entry{Name: "module"},
},
},
want: &MappedType{NativeType: "uint32", zeroValue: "0"},
want: &MappedType{NativeType: "uint32", ZeroValue: "0"},
}}

for _, tt := range tests {
@@ -1023,8 +1023,8 @@ func TestTypeResolutionManyToOne(t *testing.T) {
},
}},
wantTypes: map[string]*MappedType{
"/test-module/leaf-one": {NativeType: "E_TestModule_BaseIdentity", isEnumeratedValue: true, zeroValue: "0"},
"/test-module/leaf-two": {NativeType: "E_TestModule_BaseIdentity", isEnumeratedValue: true, zeroValue: "0"},
"/test-module/leaf-one": {NativeType: "E_TestModule_BaseIdentity", IsEnumeratedValue: true, ZeroValue: "0"},
"/test-module/leaf-two": {NativeType: "E_TestModule_BaseIdentity", IsEnumeratedValue: true, ZeroValue: "0"},
},
}, {
name: "typedef with multiple references",
@@ -1060,8 +1060,8 @@ func TestTypeResolutionManyToOne(t *testing.T) {
},
}},
wantTypes: map[string]*MappedType{
"/base-module/leaf-one": {NativeType: "E_BaseModule_DefinedType", isEnumeratedValue: true, zeroValue: "0"},
"/base-module/leaf-two": {NativeType: "E_BaseModule_DefinedType", isEnumeratedValue: true, zeroValue: "0"},
"/base-module/leaf-one": {NativeType: "E_BaseModule_DefinedType", IsEnumeratedValue: true, ZeroValue: "0"},
"/base-module/leaf-two": {NativeType: "E_BaseModule_DefinedType", IsEnumeratedValue: true, ZeroValue: "0"},
},
}}

0 comments on commit fd57a44

Please sign in to comment.
You can’t perform that action at this time.