@@ -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.
947962func (* 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
0 commit comments