openconfig / ygot

A YANG-centric Go toolkit - Go/Protobuf Code Generation; Validation; Marshaling/Unmarshaling
Apache License 2.0
284 stars 106 forks source link

Convert ygot diff output to JSON IETF #581

Open adelshafiei opened 2 years ago

adelshafiei commented 2 years ago

I am working with a gNMI server which only supports JSON IETF encodings. So I am trying to convert the output of ygot diff API to JSON IETF Encodings:

type TypedValue struct { // One of the fields within the val oneof is populated with the value // of the update. The type of the value being included in the Update // determines which field should be populated. In the case that the // encoding is a particular form of the base protobuf type, a specific // field is used to store the value (e.g., json_val). // // Types that are assignable to Value: // TypedValue_StringVal // TypedValue_IntVal // TypedValue_UintVal // TypedValue_BoolVal // TypedValue_BytesVal // TypedValue_FloatVal // TypedValue_DecimalVal // TypedValue_LeaflistVal // TypedValue_AnyVal // TypedValue_JsonVal // TypedValue_JsonIetfVal // TypedValue_AsciiVal // *TypedValue_ProtoBytes Value isTypedValue_Value protobuf_oneof:"value" // contains filtered or unexported fields }

Is there an API that gets TypedValue of different formats and converts them to JSON IETF?

Thanks,

wenovus commented 2 years ago

Unforunately I don't think there is a function that converts TypedValue to JSON, even if all of these are leafs.

One way is to unmarshal these one-by-one back into a GoStruct using ytypes' SetNode, and then getting each value via GetNode, and then calling ygot.Marshal7951.

Another way is to unmarshal all of them into a GoStruct using ytypes' SetNode, and then simply calling ygot.Marshal7951 that gives you a bigger piece of JSON.

Another way is to add to the implementation a new DiffOpt for ygot.Diff that encodes the values using Marshal7951 instead of EncodeTypedValue.