Skip to content

Commit 26dc738

Browse files
committed
simplification(ygot): catch up with master and simplify the change
1 parent 0a6b89a commit 26dc738

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

ygot/render.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"fmt"
2121
"reflect"
22+
"strconv"
2223
"strings"
2324

2425
"github.com/openconfig/gnmi/errlist"
@@ -65,7 +66,7 @@ var (
6566
// unionSingletonUnderlyingTypes stores the underlying types of the
6667
// singleton (i.e. non-struct, non-slice, non-map) typedefs used to
6768
// represent union subtypes for the "Simplified Union Leaf" way of
68-
// representatiing unions in the Go generated code.
69+
// representing unions in the Go generated code.
6970
unionSingletonUnderlyingTypes = map[string]reflect.Type{
7071
"UnionInt8": reflect.TypeOf(int8(0)),
7172
"UnionInt16": reflect.TypeOf(int16(0)),
@@ -93,7 +94,10 @@ func (p *path) String() string {
9394
if p.p.isPathElemPath() {
9495
return prototext.Format(&gnmipb.Path{Elem: p.p.pathElemPath})
9596
}
96-
return fmt.Sprintf("%v", p.p.pathElemPath)
97+
98+
// If the path is not path element path, return the joined string (unique for keying)
99+
// of the string slice path.
100+
return strings.Join(p.p.stringSlicePath, "/")
97101
}
98102

99103
// pathval combines path and the value for the relative or absolute path.
@@ -859,7 +863,7 @@ func EncodeTypedValue(val any, enc gnmipb.Encoding, opts ...EncodeTypedValueOpt)
859863
if err != nil {
860864
return nil, fmt.Errorf("cannot marshal enum, %v", err)
861865
}
862-
return &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{en}}, nil
866+
return &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{StringVal: en}}, nil
863867
}
864868

865869
vv := reflect.ValueOf(val)
@@ -871,9 +875,9 @@ func EncodeTypedValue(val any, enc gnmipb.Encoding, opts ...EncodeTypedValueOpt)
871875
return nil, fmt.Errorf("cannot represent field value %v as TypedValue", val)
872876
case vv.Type().Name() == BinaryTypeName:
873877
// This is a binary type which is defined as a []byte, so we encode it as the bytes.
874-
return &gnmipb.TypedValue{Value: &gnmipb.TypedValue_BytesVal{vv.Bytes()}}, nil
878+
return &gnmipb.TypedValue{Value: &gnmipb.TypedValue_BytesVal{BytesVal: vv.Bytes()}}, nil
875879
case vv.Type().Name() == EmptyTypeName:
876-
return &gnmipb.TypedValue{Value: &gnmipb.TypedValue_BoolVal{vv.Bool()}}, nil
880+
return &gnmipb.TypedValue{Value: &gnmipb.TypedValue_BoolVal{BoolVal: vv.Bool()}}, nil
877881
case vv.Kind() == reflect.Slice:
878882
sval, err := leaflistToSlice(vv, false)
879883
if err != nil {
@@ -884,7 +888,7 @@ func EncodeTypedValue(val any, enc gnmipb.Encoding, opts ...EncodeTypedValueOpt)
884888
if err != nil {
885889
return nil, err
886890
}
887-
return &gnmipb.TypedValue{Value: &gnmipb.TypedValue_LeaflistVal{arr}}, nil
891+
return &gnmipb.TypedValue{Value: &gnmipb.TypedValue_LeaflistVal{LeaflistVal: arr}}, nil
888892
case util.IsValueStructPtr(vv):
889893
nv, err := unwrapUnionInterfaceValue(vv, false)
890894
if err != nil {
@@ -893,7 +897,7 @@ func EncodeTypedValue(val any, enc gnmipb.Encoding, opts ...EncodeTypedValueOpt)
893897
vv = reflect.ValueOf(nv)
894898
// Apart from binary, all other possible union subtypes are scalars or typedefs of scalars.
895899
if vv.Type().Name() == BinaryTypeName {
896-
return &gnmipb.TypedValue{Value: &gnmipb.TypedValue_BytesVal{vv.Bytes()}}, nil
900+
return &gnmipb.TypedValue{Value: &gnmipb.TypedValue_BytesVal{BytesVal: vv.Bytes()}}, nil
897901
}
898902
case util.IsValuePtr(vv):
899903
vv = vv.Elem()
@@ -1246,9 +1250,6 @@ func rewriteModName(mod string, rules map[string]string) string {
12461250
// there are modules to be prepended, it also returns the module to which the
12471251
// field belongs. It will also return an error if it encounters one.
12481252
func prependmodsJSON(fType reflect.StructField, parentMod string, args jsonOutputConfig) ([][]string, string, error) {
1249-
var prependmods [][]string
1250-
var chMod string
1251-
12521253
mapModules, err := structTagToLibModules(fType, args.rfc7951Config.PreferShadowPath)
12531254
if err != nil {
12541255
return nil, "", fmt.Errorf("%s: %v", fType.Name, err)
@@ -1257,8 +1258,11 @@ func prependmodsJSON(fType reflect.StructField, parentMod string, args jsonOutpu
12571258
return nil, "", nil
12581259
}
12591260

1260-
for _, modulePath := range mapModules {
1261-
var prependmod []string
1261+
prependmods := make([][]string, len(mapModules))
1262+
var chMod string
1263+
1264+
for idx, modulePath := range mapModules {
1265+
prependmod := make([]string, modulePath.Len())
12621266
prevMod := parentMod
12631267
for i := 0; i != modulePath.Len(); i++ {
12641268
mod, err := modulePath.StringElemAt(i)
@@ -1274,12 +1278,12 @@ func prependmodsJSON(fType reflect.StructField, parentMod string, args jsonOutpu
12741278
} else {
12751279
prevMod = mod
12761280
}
1277-
prependmod = append(prependmod, mod)
1281+
prependmod[i] = mod
12781282
}
12791283
if chMod != "" && prevMod != chMod {
12801284
return nil, "", fmt.Errorf("%s: child modules between all paths are not equal: %v", fType.Name, mapModules)
12811285
}
1282-
prependmods = append(prependmods, prependmod)
1286+
prependmods[idx] = prependmod
12831287
chMod = prevMod
12841288
}
12851289
return prependmods, chMod, nil
@@ -1415,8 +1419,20 @@ func structJSON(s GoStruct, parentMod string, args jsonOutputConfig) (map[string
14151419
// and float64 values are represented as strings.
14161420
func writeIETFScalarJSON(i any) any {
14171421
switch reflect.ValueOf(i).Kind() {
1418-
case reflect.Uint64, reflect.Int64, reflect.Float64:
1422+
case reflect.Float64:
14191423
return fmt.Sprintf("%v", i)
1424+
case reflect.Int64:
1425+
if val, ok := i.(int64); ok {
1426+
return strconv.FormatInt(int64(val), 10)
1427+
}
1428+
1429+
return fmt.Sprintf("%d", i)
1430+
case reflect.Uint64:
1431+
if val, ok := i.(uint64); ok {
1432+
return strconv.FormatUint(uint64(val), 10)
1433+
}
1434+
1435+
return fmt.Sprintf("%d", i)
14201436
}
14211437
return i
14221438
}
@@ -1635,14 +1651,17 @@ func jsonValue(field reflect.Value, parentMod string, args jsonOutputConfig) (an
16351651
if err != nil {
16361652
errs.Add(err)
16371653
}
1654+
case reflect.String:
1655+
value = field.Elem().String()
1656+
case reflect.Bool:
1657+
value = field.Elem().Bool()
16381658
default:
16391659
value = field.Elem().Interface()
16401660
if args.jType == RFC7951 {
16411661
value = writeIETFScalarJSON(value)
16421662
}
16431663
}
16441664
case reflect.Slice:
1645-
16461665
isAnnotationSlice := func(v reflect.Value) bool {
16471666
annoT := reflect.TypeOf((*Annotation)(nil)).Elem()
16481667
return v.Type().Elem().Implements(annoT)

0 commit comments

Comments
 (0)