Skip to content

Commit 538cb0f

Browse files
authored
Add json encoding opts to EncodeTypedValue (#747)
* Add json encoding opts to EncodeTypedValue
1 parent 6d8a427 commit 538cb0f

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

ygot/render.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,26 @@ func leavesToNotifications(leaves map[*path]interface{}, ts int64, pfx *gnmiPath
665665
return []*gnmipb.Notification{n}, nil
666666
}
667667

668+
// EncodeTypedValueOpt is an interface implemented by arguments to
669+
// the EncodeTypedValueOpt function.
670+
type EncodeTypedValueOpt interface {
671+
// IsMarshal7951Arg is a market method.
672+
IsEncodeTypedValueOpt()
673+
}
674+
668675
// EncodeTypedValue encodes val into a gNMI TypedValue message, using the specified encoding
669676
// type if the value is a struct.
670-
func EncodeTypedValue(val interface{}, enc gnmipb.Encoding) (*gnmipb.TypedValue, error) {
677+
func EncodeTypedValue(val interface{}, enc gnmipb.Encoding, opts ...EncodeTypedValueOpt) (*gnmipb.TypedValue, error) {
678+
jc := &RFC7951JSONConfig{}
679+
for _, opt := range opts {
680+
if cfg, ok := opt.(*RFC7951JSONConfig); ok {
681+
jc = cfg
682+
}
683+
}
684+
671685
switch v := val.(type) {
672686
case GoStruct:
673-
return marshalStruct(v, enc)
687+
return marshalStruct(v, enc, jc)
674688
case GoEnum:
675689
en, err := EnumName(v)
676690
if err != nil {
@@ -731,7 +745,7 @@ func EncodeTypedValue(val interface{}, enc gnmipb.Encoding) (*gnmipb.TypedValue,
731745

732746
// marshalStruct encodes the struct s according to the encoding specified by enc. It
733747
// is returned as a TypedValue gNMI message.
734-
func marshalStruct(s GoStruct, enc gnmipb.Encoding) (*gnmipb.TypedValue, error) {
748+
func marshalStruct(s GoStruct, enc gnmipb.Encoding, cfg *RFC7951JSONConfig) (*gnmipb.TypedValue, error) {
735749
if reflect.ValueOf(s).IsNil() {
736750
return nil, nil
737751
}
@@ -750,7 +764,8 @@ func marshalStruct(s GoStruct, enc gnmipb.Encoding) (*gnmipb.TypedValue, error)
750764
}
751765
case gnmipb.Encoding_JSON_IETF:
752766
// We always prepend the module name when marshalling within a Notification.
753-
j, err = ConstructIETFJSON(s, &RFC7951JSONConfig{AppendModuleName: true})
767+
cfg.AppendModuleName = true
768+
j, err = ConstructIETFJSON(s, cfg)
754769
encfn = func(s string) *gnmipb.TypedValue {
755770
return &gnmipb.TypedValue{Value: &gnmipb.TypedValue_JsonIetfVal{[]byte(s)}}
756771
}
@@ -946,6 +961,10 @@ type RFC7951JSONConfig struct {
946961
// Marshal7951.
947962
func (*RFC7951JSONConfig) IsMarshal7951Arg() {}
948963

964+
// IsEncodeTypedValueOpt marks the RFC7951JSONConfig struct as a valid option to
965+
// EncodeTypedValue.
966+
func (*RFC7951JSONConfig) IsEncodeTypedValueOpt() {}
967+
949968
// ConstructIETFJSON marshals a supplied GoStruct to a map, suitable for
950969
// handing to json.Marshal. It complies with the convention for marshalling
951970
// to JSON described by RFC7951. The supplied args control options corresponding

ygot/render_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3786,6 +3786,7 @@ func TestEncodeTypedValue(t *testing.T) {
37863786
name string
37873787
inVal interface{}
37883788
inEnc gnmipb.Encoding
3789+
inArgs []EncodeTypedValueOpt
37893790
want *gnmipb.TypedValue
37903791
wantErrSubstring string
37913792
}{{
@@ -3907,6 +3908,18 @@ func TestEncodeTypedValue(t *testing.T) {
39073908
"f2mod:config": {
39083909
"f2": "hello"
39093910
}
3911+
}`)}},
3912+
}, {
3913+
name: "struct val - with PreferShadowPath",
3914+
inVal: &renderExample{
3915+
Str: String("test-string"),
3916+
},
3917+
inEnc: gnmipb.Encoding_JSON_IETF,
3918+
inArgs: []EncodeTypedValueOpt{
3919+
&RFC7951JSONConfig{PreferShadowPath: true},
3920+
},
3921+
want: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_JsonIetfVal{[]byte(`{
3922+
"srt": "test-string"
39103923
}`)}},
39113924
}, {
39123925
name: "struct val - internal json",
@@ -3941,7 +3954,7 @@ func TestEncodeTypedValue(t *testing.T) {
39413954

39423955
for _, tt := range tests {
39433956
t.Run(tt.name, func(t *testing.T) {
3944-
got, err := EncodeTypedValue(tt.inVal, tt.inEnc)
3957+
got, err := EncodeTypedValue(tt.inVal, tt.inEnc, tt.inArgs...)
39453958
if diff := errdiff.Substring(err, tt.wantErrSubstring); diff != "" {
39463959
t.Fatalf("did not get expected error, %s", diff)
39473960
}

0 commit comments

Comments
 (0)