mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-18 13:11:16 +00:00
encoding/jsonpb: remove encoder and decoder types
I had defined these types at some point in order to reuse another instance of these within, but it's not needed anymore and hence removing them. Change-Id: I8aa127326a5926c6a8688d83cce7ca83c160b39b Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/169700 Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
parent
c4450377bd
commit
822de2d88c
@ -33,6 +33,8 @@ type UnmarshalOptions struct {
|
||||
// and processing Any. If Resolver is not set, unmarshaling will default to
|
||||
// using protoregistry.GlobalTypes.
|
||||
Resolver *protoregistry.Types
|
||||
|
||||
decoder *json.Decoder
|
||||
}
|
||||
|
||||
// Unmarshal reads the given []byte and populates the given proto.Message using
|
||||
@ -46,22 +48,18 @@ func (o UnmarshalOptions) Unmarshal(m proto.Message, b []byte) error {
|
||||
// marshaling.
|
||||
resetMessage(mr)
|
||||
|
||||
resolver := o.Resolver
|
||||
if resolver == nil {
|
||||
resolver = protoregistry.GlobalTypes
|
||||
if o.Resolver == nil {
|
||||
o.Resolver = protoregistry.GlobalTypes
|
||||
}
|
||||
o.decoder = json.NewDecoder(b)
|
||||
|
||||
dec := decoder{
|
||||
Decoder: json.NewDecoder(b),
|
||||
resolver: resolver,
|
||||
}
|
||||
var nerr errors.NonFatal
|
||||
if err := dec.unmarshalMessage(mr); !nerr.Merge(err) {
|
||||
if err := o.unmarshalMessage(mr); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check for EOF.
|
||||
val, err := dec.Read()
|
||||
val, err := o.decoder.Read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -91,8 +89,8 @@ func resetMessage(m pref.Message) {
|
||||
}
|
||||
|
||||
// unexpectedJSONError is an error that contains the unexpected json.Value. This
|
||||
// is used by decoder methods to provide callers the read json.Value that it
|
||||
// did not expect.
|
||||
// is returned by methods to provide callers the read json.Value that it did not
|
||||
// expect.
|
||||
// TODO: Consider moving this to internal/encoding/json for consistency with
|
||||
// errors that package returns.
|
||||
type unexpectedJSONError struct {
|
||||
@ -122,21 +120,15 @@ func newError(f string, x ...interface{}) error {
|
||||
return e
|
||||
}
|
||||
|
||||
// decoder decodes JSON into protoreflect values.
|
||||
type decoder struct {
|
||||
*json.Decoder
|
||||
resolver *protoregistry.Types
|
||||
}
|
||||
|
||||
// unmarshalMessage unmarshals a message into the given protoreflect.Message.
|
||||
func (d decoder) unmarshalMessage(m pref.Message) error {
|
||||
func (o UnmarshalOptions) unmarshalMessage(m pref.Message) error {
|
||||
var nerr errors.NonFatal
|
||||
|
||||
if isCustomType(m.Type().FullName()) {
|
||||
return d.unmarshalCustomType(m)
|
||||
return o.unmarshalCustomType(m)
|
||||
}
|
||||
|
||||
jval, err := d.Read()
|
||||
jval, err := o.decoder.Read()
|
||||
if !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
@ -144,7 +136,7 @@ func (d decoder) unmarshalMessage(m pref.Message) error {
|
||||
return unexpectedJSONError{jval}
|
||||
}
|
||||
|
||||
if err := d.unmarshalFields(m); !nerr.Merge(err) {
|
||||
if err := o.unmarshalFields(m); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -152,7 +144,7 @@ func (d decoder) unmarshalMessage(m pref.Message) error {
|
||||
}
|
||||
|
||||
// unmarshalFields unmarshals the fields into the given protoreflect.Message.
|
||||
func (d decoder) unmarshalFields(m pref.Message) error {
|
||||
func (o UnmarshalOptions) unmarshalFields(m pref.Message) error {
|
||||
var nerr errors.NonFatal
|
||||
var reqNums set.Ints
|
||||
var seenNums set.Ints
|
||||
@ -165,7 +157,7 @@ func (d decoder) unmarshalFields(m pref.Message) error {
|
||||
Loop:
|
||||
for {
|
||||
// Read field name.
|
||||
jval, err := d.Read()
|
||||
jval, err := o.decoder.Read()
|
||||
if !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
@ -190,7 +182,7 @@ Loop:
|
||||
xtName := pref.FullName(name[1 : len(name)-1])
|
||||
xt := xtTypes.ByName(xtName)
|
||||
if xt == nil {
|
||||
xt, err = d.findExtension(xtName)
|
||||
xt, err = o.findExtension(xtName)
|
||||
if err != nil && err != protoregistry.NotFound {
|
||||
return errors.New("unable to resolve [%v]: %v", xtName, err)
|
||||
}
|
||||
@ -222,19 +214,19 @@ Loop:
|
||||
|
||||
// No need to set values for JSON null unless the field type is
|
||||
// google.protobuf.Value.
|
||||
if d.Peek() == json.Null && !isKnownValue(fd) {
|
||||
d.Read()
|
||||
if o.decoder.Peek() == json.Null && !isKnownValue(fd) {
|
||||
o.decoder.Read()
|
||||
continue
|
||||
}
|
||||
|
||||
if cardinality := fd.Cardinality(); cardinality == pref.Repeated {
|
||||
// Map or list fields have cardinality of repeated.
|
||||
if err := d.unmarshalRepeated(knownFields, fd); !nerr.Merge(err) {
|
||||
if err := o.unmarshalRepeated(knownFields, fd); !nerr.Merge(err) {
|
||||
return errors.New("%v|%q: %v", fd.FullName(), name, err)
|
||||
}
|
||||
} else {
|
||||
// Required or optional fields.
|
||||
if err := d.unmarshalSingular(knownFields, fd); !nerr.Merge(err) {
|
||||
if err := o.unmarshalSingular(knownFields, fd); !nerr.Merge(err) {
|
||||
return errors.New("%v|%q: %v", fd.FullName(), name, err)
|
||||
}
|
||||
if cardinality == pref.Required {
|
||||
@ -257,14 +249,14 @@ Loop:
|
||||
}
|
||||
|
||||
// findExtension returns protoreflect.ExtensionType from the resolver if found.
|
||||
func (d decoder) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
|
||||
xt, err := d.resolver.FindExtensionByName(xtName)
|
||||
func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
|
||||
xt, err := o.Resolver.FindExtensionByName(xtName)
|
||||
if err == nil {
|
||||
return xt, nil
|
||||
}
|
||||
|
||||
// Check if this is a MessageSet extension field.
|
||||
xt, err = d.resolver.FindExtensionByName(xtName + ".message_set_extension")
|
||||
xt, err = o.Resolver.FindExtensionByName(xtName + ".message_set_extension")
|
||||
if err == nil && isMessageSetExtension(xt) {
|
||||
return xt, nil
|
||||
}
|
||||
@ -273,7 +265,7 @@ func (d decoder) findExtension(xtName pref.FullName) (pref.ExtensionType, error)
|
||||
|
||||
// unmarshalSingular unmarshals to the non-repeated field specified by the given
|
||||
// FieldDescriptor.
|
||||
func (d decoder) unmarshalSingular(knownFields pref.KnownFields, fd pref.FieldDescriptor) error {
|
||||
func (o UnmarshalOptions) unmarshalSingular(knownFields pref.KnownFields, fd pref.FieldDescriptor) error {
|
||||
var val pref.Value
|
||||
var err error
|
||||
num := fd.Number()
|
||||
@ -281,10 +273,10 @@ func (d decoder) unmarshalSingular(knownFields pref.KnownFields, fd pref.FieldDe
|
||||
switch fd.Kind() {
|
||||
case pref.MessageKind, pref.GroupKind:
|
||||
m := knownFields.NewMessage(num)
|
||||
err = d.unmarshalMessage(m)
|
||||
err = o.unmarshalMessage(m)
|
||||
val = pref.ValueOf(m)
|
||||
default:
|
||||
val, err = d.unmarshalScalar(fd)
|
||||
val, err = o.unmarshalScalar(fd)
|
||||
}
|
||||
|
||||
var nerr errors.NonFatal
|
||||
@ -297,12 +289,12 @@ func (d decoder) unmarshalSingular(knownFields pref.KnownFields, fd pref.FieldDe
|
||||
|
||||
// unmarshalScalar unmarshals to a scalar/enum protoreflect.Value specified by
|
||||
// the given FieldDescriptor.
|
||||
func (d decoder) unmarshalScalar(fd pref.FieldDescriptor) (pref.Value, error) {
|
||||
func (o UnmarshalOptions) unmarshalScalar(fd pref.FieldDescriptor) (pref.Value, error) {
|
||||
const b32 int = 32
|
||||
const b64 int = 64
|
||||
|
||||
var nerr errors.NonFatal
|
||||
jval, err := d.Read()
|
||||
jval, err := o.decoder.Read()
|
||||
if !nerr.Merge(err) {
|
||||
return pref.Value{}, err
|
||||
}
|
||||
@ -509,16 +501,16 @@ func unmarshalEnum(jval json.Value, fd pref.FieldDescriptor) (pref.Value, error)
|
||||
}
|
||||
|
||||
// unmarshalRepeated unmarshals into a repeated field.
|
||||
func (d decoder) unmarshalRepeated(knownFields pref.KnownFields, fd pref.FieldDescriptor) error {
|
||||
func (o UnmarshalOptions) unmarshalRepeated(knownFields pref.KnownFields, fd pref.FieldDescriptor) error {
|
||||
var nerr errors.NonFatal
|
||||
num := fd.Number()
|
||||
val := knownFields.Get(num)
|
||||
if !fd.IsMap() {
|
||||
if err := d.unmarshalList(val.List(), fd); !nerr.Merge(err) {
|
||||
if err := o.unmarshalList(val.List(), fd); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := d.unmarshalMap(val.Map(), fd); !nerr.Merge(err) {
|
||||
if err := o.unmarshalMap(val.Map(), fd); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -526,9 +518,9 @@ func (d decoder) unmarshalRepeated(knownFields pref.KnownFields, fd pref.FieldDe
|
||||
}
|
||||
|
||||
// unmarshalList unmarshals into given protoreflect.List.
|
||||
func (d decoder) unmarshalList(list pref.List, fd pref.FieldDescriptor) error {
|
||||
func (o UnmarshalOptions) unmarshalList(list pref.List, fd pref.FieldDescriptor) error {
|
||||
var nerr errors.NonFatal
|
||||
jval, err := d.Read()
|
||||
jval, err := o.decoder.Read()
|
||||
if !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
@ -540,7 +532,7 @@ func (d decoder) unmarshalList(list pref.List, fd pref.FieldDescriptor) error {
|
||||
case pref.MessageKind, pref.GroupKind:
|
||||
for {
|
||||
m := list.NewMessage()
|
||||
err := d.unmarshalMessage(m)
|
||||
err := o.unmarshalMessage(m)
|
||||
if !nerr.Merge(err) {
|
||||
if e, ok := err.(unexpectedJSONError); ok {
|
||||
if e.value.Type() == json.EndArray {
|
||||
@ -554,7 +546,7 @@ func (d decoder) unmarshalList(list pref.List, fd pref.FieldDescriptor) error {
|
||||
}
|
||||
default:
|
||||
for {
|
||||
val, err := d.unmarshalScalar(fd)
|
||||
val, err := o.unmarshalScalar(fd)
|
||||
if !nerr.Merge(err) {
|
||||
if e, ok := err.(unexpectedJSONError); ok {
|
||||
if e.value.Type() == json.EndArray {
|
||||
@ -571,10 +563,10 @@ func (d decoder) unmarshalList(list pref.List, fd pref.FieldDescriptor) error {
|
||||
}
|
||||
|
||||
// unmarshalMap unmarshals into given protoreflect.Map.
|
||||
func (d decoder) unmarshalMap(mmap pref.Map, fd pref.FieldDescriptor) error {
|
||||
func (o UnmarshalOptions) unmarshalMap(mmap pref.Map, fd pref.FieldDescriptor) error {
|
||||
var nerr errors.NonFatal
|
||||
|
||||
jval, err := d.Read()
|
||||
jval, err := o.decoder.Read()
|
||||
if !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
@ -590,14 +582,14 @@ func (d decoder) unmarshalMap(mmap pref.Map, fd pref.FieldDescriptor) error {
|
||||
// order to call the appropriate unmarshalMapValue func inside the for loop
|
||||
// below.
|
||||
unmarshalMapValue := func() (pref.Value, error) {
|
||||
return d.unmarshalScalar(valDesc)
|
||||
return o.unmarshalScalar(valDesc)
|
||||
}
|
||||
switch valDesc.Kind() {
|
||||
case pref.MessageKind, pref.GroupKind:
|
||||
unmarshalMapValue = func() (pref.Value, error) {
|
||||
var nerr errors.NonFatal
|
||||
m := mmap.NewMessage()
|
||||
if err := d.unmarshalMessage(m); !nerr.Merge(err) {
|
||||
if err := o.unmarshalMessage(m); !nerr.Merge(err) {
|
||||
return pref.Value{}, err
|
||||
}
|
||||
return pref.ValueOf(m), nerr.E
|
||||
@ -607,7 +599,7 @@ func (d decoder) unmarshalMap(mmap pref.Map, fd pref.FieldDescriptor) error {
|
||||
Loop:
|
||||
for {
|
||||
// Read field name.
|
||||
jval, err := d.Read()
|
||||
jval, err := o.decoder.Read()
|
||||
if !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
|
@ -37,55 +37,41 @@ type MarshalOptions struct {
|
||||
// google.protobuf.Any messages. If Resolver is not set, marshaling will
|
||||
// default to using protoregistry.GlobalTypes.
|
||||
Resolver *protoregistry.Types
|
||||
|
||||
encoder *json.Encoder
|
||||
}
|
||||
|
||||
// Marshal marshals the given proto.Message in the JSON format using options in
|
||||
// MarshalOptions.
|
||||
func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) {
|
||||
enc, err := newEncoder(o.Indent, o.Resolver)
|
||||
var err error
|
||||
o.encoder, err = json.NewEncoder(o.Indent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if o.Resolver == nil {
|
||||
o.Resolver = protoregistry.GlobalTypes
|
||||
}
|
||||
|
||||
var nerr errors.NonFatal
|
||||
err = enc.marshalMessage(m.ProtoReflect())
|
||||
err = o.marshalMessage(m.ProtoReflect())
|
||||
if !nerr.Merge(err) {
|
||||
return nil, err
|
||||
}
|
||||
return enc.Bytes(), nerr.E
|
||||
}
|
||||
|
||||
// encoder encodes protoreflect values into JSON.
|
||||
type encoder struct {
|
||||
*json.Encoder
|
||||
resolver *protoregistry.Types
|
||||
}
|
||||
|
||||
func newEncoder(indent string, resolver *protoregistry.Types) (encoder, error) {
|
||||
enc, err := json.NewEncoder(indent)
|
||||
if err != nil {
|
||||
return encoder{}, err
|
||||
}
|
||||
if resolver == nil {
|
||||
resolver = protoregistry.GlobalTypes
|
||||
}
|
||||
return encoder{
|
||||
Encoder: enc,
|
||||
resolver: resolver,
|
||||
}, nil
|
||||
return o.encoder.Bytes(), nerr.E
|
||||
}
|
||||
|
||||
// marshalMessage marshals the given protoreflect.Message.
|
||||
func (e encoder) marshalMessage(m pref.Message) error {
|
||||
func (o MarshalOptions) marshalMessage(m pref.Message) error {
|
||||
var nerr errors.NonFatal
|
||||
|
||||
if isCustomType(m.Type().FullName()) {
|
||||
return e.marshalCustomType(m)
|
||||
return o.marshalCustomType(m)
|
||||
}
|
||||
|
||||
e.StartObject()
|
||||
defer e.EndObject()
|
||||
if err := e.marshalFields(m); !nerr.Merge(err) {
|
||||
o.encoder.StartObject()
|
||||
defer o.encoder.EndObject()
|
||||
if err := o.marshalFields(m); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -93,7 +79,7 @@ func (e encoder) marshalMessage(m pref.Message) error {
|
||||
}
|
||||
|
||||
// marshalFields marshals the fields in the given protoreflect.Message.
|
||||
func (e encoder) marshalFields(m pref.Message) error {
|
||||
func (o MarshalOptions) marshalFields(m pref.Message) error {
|
||||
var nerr errors.NonFatal
|
||||
fieldDescs := m.Type().Fields()
|
||||
knownFields := m.KnownFields()
|
||||
@ -113,38 +99,38 @@ func (e encoder) marshalFields(m pref.Message) error {
|
||||
|
||||
name := fd.JSONName()
|
||||
val := knownFields.Get(num)
|
||||
if err := e.WriteName(name); !nerr.Merge(err) {
|
||||
if err := o.encoder.WriteName(name); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
if err := e.marshalValue(val, fd); !nerr.Merge(err) {
|
||||
if err := o.marshalValue(val, fd); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Marshal out extensions.
|
||||
if err := e.marshalExtensions(knownFields); !nerr.Merge(err) {
|
||||
if err := o.marshalExtensions(knownFields); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
return nerr.E
|
||||
}
|
||||
|
||||
// marshalValue marshals the given protoreflect.Value.
|
||||
func (e encoder) marshalValue(val pref.Value, fd pref.FieldDescriptor) error {
|
||||
func (o MarshalOptions) marshalValue(val pref.Value, fd pref.FieldDescriptor) error {
|
||||
var nerr errors.NonFatal
|
||||
if fd.Cardinality() == pref.Repeated {
|
||||
// Map or repeated fields.
|
||||
if fd.IsMap() {
|
||||
if err := e.marshalMap(val.Map(), fd); !nerr.Merge(err) {
|
||||
if err := o.marshalMap(val.Map(), fd); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := e.marshalList(val.List(), fd); !nerr.Merge(err) {
|
||||
if err := o.marshalList(val.List(), fd); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Required or optional fields.
|
||||
if err := e.marshalSingular(val, fd); !nerr.Merge(err) {
|
||||
if err := o.marshalSingular(val, fd); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -153,38 +139,38 @@ func (e encoder) marshalValue(val pref.Value, fd pref.FieldDescriptor) error {
|
||||
|
||||
// marshalSingular marshals the given non-repeated field value. This includes
|
||||
// all scalar types, enums, messages, and groups.
|
||||
func (e encoder) marshalSingular(val pref.Value, fd pref.FieldDescriptor) error {
|
||||
func (o MarshalOptions) marshalSingular(val pref.Value, fd pref.FieldDescriptor) error {
|
||||
var nerr errors.NonFatal
|
||||
switch kind := fd.Kind(); kind {
|
||||
case pref.BoolKind:
|
||||
e.WriteBool(val.Bool())
|
||||
o.encoder.WriteBool(val.Bool())
|
||||
|
||||
case pref.StringKind:
|
||||
if err := e.WriteString(val.String()); !nerr.Merge(err) {
|
||||
if err := o.encoder.WriteString(val.String()); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
|
||||
e.WriteInt(val.Int())
|
||||
o.encoder.WriteInt(val.Int())
|
||||
|
||||
case pref.Uint32Kind, pref.Fixed32Kind:
|
||||
e.WriteUint(val.Uint())
|
||||
o.encoder.WriteUint(val.Uint())
|
||||
|
||||
case pref.Int64Kind, pref.Sint64Kind, pref.Uint64Kind,
|
||||
pref.Sfixed64Kind, pref.Fixed64Kind:
|
||||
// 64-bit integers are written out as JSON string.
|
||||
e.WriteString(val.String())
|
||||
o.encoder.WriteString(val.String())
|
||||
|
||||
case pref.FloatKind:
|
||||
// Encoder.WriteFloat handles the special numbers NaN and infinites.
|
||||
e.WriteFloat(val.Float(), 32)
|
||||
o.encoder.WriteFloat(val.Float(), 32)
|
||||
|
||||
case pref.DoubleKind:
|
||||
// Encoder.WriteFloat handles the special numbers NaN and infinites.
|
||||
e.WriteFloat(val.Float(), 64)
|
||||
o.encoder.WriteFloat(val.Float(), 64)
|
||||
|
||||
case pref.BytesKind:
|
||||
err := e.WriteString(base64.StdEncoding.EncodeToString(val.Bytes()))
|
||||
err := o.encoder.WriteString(base64.StdEncoding.EncodeToString(val.Bytes()))
|
||||
if !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
@ -194,19 +180,19 @@ func (e encoder) marshalSingular(val pref.Value, fd pref.FieldDescriptor) error
|
||||
num := val.Enum()
|
||||
|
||||
if enumType.FullName() == "google.protobuf.NullValue" {
|
||||
e.WriteNull()
|
||||
o.encoder.WriteNull()
|
||||
} else if desc := enumType.Values().ByNumber(num); desc != nil {
|
||||
err := e.WriteString(string(desc.Name()))
|
||||
err := o.encoder.WriteString(string(desc.Name()))
|
||||
if !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// Use numeric value if there is no enum value descriptor.
|
||||
e.WriteInt(int64(num))
|
||||
o.encoder.WriteInt(int64(num))
|
||||
}
|
||||
|
||||
case pref.MessageKind, pref.GroupKind:
|
||||
if err := e.marshalMessage(val.Message()); !nerr.Merge(err) {
|
||||
if err := o.marshalMessage(val.Message()); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -217,14 +203,14 @@ func (e encoder) marshalSingular(val pref.Value, fd pref.FieldDescriptor) error
|
||||
}
|
||||
|
||||
// marshalList marshals the given protoreflect.List.
|
||||
func (e encoder) marshalList(list pref.List, fd pref.FieldDescriptor) error {
|
||||
e.StartArray()
|
||||
defer e.EndArray()
|
||||
func (o MarshalOptions) marshalList(list pref.List, fd pref.FieldDescriptor) error {
|
||||
o.encoder.StartArray()
|
||||
defer o.encoder.EndArray()
|
||||
|
||||
var nerr errors.NonFatal
|
||||
for i := 0; i < list.Len(); i++ {
|
||||
item := list.Get(i)
|
||||
if err := e.marshalSingular(item, fd); !nerr.Merge(err) {
|
||||
if err := o.marshalSingular(item, fd); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -237,9 +223,9 @@ type mapEntry struct {
|
||||
}
|
||||
|
||||
// marshalMap marshals given protoreflect.Map.
|
||||
func (e encoder) marshalMap(mmap pref.Map, fd pref.FieldDescriptor) error {
|
||||
e.StartObject()
|
||||
defer e.EndObject()
|
||||
func (o MarshalOptions) marshalMap(mmap pref.Map, fd pref.FieldDescriptor) error {
|
||||
o.encoder.StartObject()
|
||||
defer o.encoder.EndObject()
|
||||
|
||||
msgFields := fd.MessageType().Fields()
|
||||
keyType := msgFields.ByNumber(1)
|
||||
@ -256,10 +242,10 @@ func (e encoder) marshalMap(mmap pref.Map, fd pref.FieldDescriptor) error {
|
||||
// Write out sorted list.
|
||||
var nerr errors.NonFatal
|
||||
for _, entry := range entries {
|
||||
if err := e.WriteName(entry.key.String()); !nerr.Merge(err) {
|
||||
if err := o.encoder.WriteName(entry.key.String()); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
if err := e.marshalSingular(entry.value, valType); !nerr.Merge(err) {
|
||||
if err := o.marshalSingular(entry.value, valType); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -283,7 +269,7 @@ func sortMap(keyKind pref.Kind, values []mapEntry) {
|
||||
}
|
||||
|
||||
// marshalExtensions marshals extension fields.
|
||||
func (e encoder) marshalExtensions(knownFields pref.KnownFields) error {
|
||||
func (o MarshalOptions) marshalExtensions(knownFields pref.KnownFields) error {
|
||||
type xtEntry struct {
|
||||
key string
|
||||
value pref.Value
|
||||
@ -325,10 +311,10 @@ func (e encoder) marshalExtensions(knownFields pref.KnownFields) error {
|
||||
// JSON field name is the proto field name enclosed in [], similar to
|
||||
// textproto. This is consistent with Go v1 lib. C++ lib v3.7.0 does not
|
||||
// marshal out extension fields.
|
||||
if err := e.WriteName("[" + entry.key + "]"); !nerr.Merge(err) {
|
||||
if err := o.encoder.WriteName("[" + entry.key + "]"); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
if err := e.marshalValue(entry.value, entry.xtType); !nerr.Merge(err) {
|
||||
if err := o.marshalValue(entry.value, entry.xtType); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -48,11 +48,11 @@ func isCustomType(name pref.FullName) bool {
|
||||
// marshalCustomType marshals given well-known type message that have special
|
||||
// JSON conversion rules. It needs to be a message type where isCustomType
|
||||
// returns true, else it will panic.
|
||||
func (e encoder) marshalCustomType(m pref.Message) error {
|
||||
func (o MarshalOptions) marshalCustomType(m pref.Message) error {
|
||||
name := m.Type().FullName()
|
||||
switch name {
|
||||
case "google.protobuf.Any":
|
||||
return e.marshalAny(m)
|
||||
return o.marshalAny(m)
|
||||
|
||||
case "google.protobuf.BoolValue",
|
||||
"google.protobuf.DoubleValue",
|
||||
@ -63,25 +63,25 @@ func (e encoder) marshalCustomType(m pref.Message) error {
|
||||
"google.protobuf.UInt64Value",
|
||||
"google.protobuf.StringValue",
|
||||
"google.protobuf.BytesValue":
|
||||
return e.marshalWrapperType(m)
|
||||
return o.marshalWrapperType(m)
|
||||
|
||||
case "google.protobuf.Struct":
|
||||
return e.marshalStruct(m)
|
||||
return o.marshalStruct(m)
|
||||
|
||||
case "google.protobuf.ListValue":
|
||||
return e.marshalListValue(m)
|
||||
return o.marshalListValue(m)
|
||||
|
||||
case "google.protobuf.Value":
|
||||
return e.marshalKnownValue(m)
|
||||
return o.marshalKnownValue(m)
|
||||
|
||||
case "google.protobuf.Duration":
|
||||
return e.marshalDuration(m)
|
||||
return o.marshalDuration(m)
|
||||
|
||||
case "google.protobuf.Timestamp":
|
||||
return e.marshalTimestamp(m)
|
||||
return o.marshalTimestamp(m)
|
||||
|
||||
case "google.protobuf.FieldMask":
|
||||
return e.marshalFieldMask(m)
|
||||
return o.marshalFieldMask(m)
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("%q does not have a custom marshaler", name))
|
||||
@ -90,7 +90,7 @@ func (e encoder) marshalCustomType(m pref.Message) error {
|
||||
// unmarshalCustomType unmarshals given well-known type message that have
|
||||
// special JSON conversion rules. It needs to be a message type where
|
||||
// isCustomType returns true, else it will panic.
|
||||
func (d decoder) unmarshalCustomType(m pref.Message) error {
|
||||
func (o UnmarshalOptions) unmarshalCustomType(m pref.Message) error {
|
||||
name := m.Type().FullName()
|
||||
switch name {
|
||||
case "google.protobuf.Any":
|
||||
@ -105,25 +105,25 @@ func (d decoder) unmarshalCustomType(m pref.Message) error {
|
||||
"google.protobuf.UInt64Value",
|
||||
"google.protobuf.StringValue",
|
||||
"google.protobuf.BytesValue":
|
||||
return d.unmarshalWrapperType(m)
|
||||
return o.unmarshalWrapperType(m)
|
||||
|
||||
case "google.protobuf.Struct":
|
||||
return d.unmarshalStruct(m)
|
||||
return o.unmarshalStruct(m)
|
||||
|
||||
case "google.protobuf.ListValue":
|
||||
return d.unmarshalListValue(m)
|
||||
return o.unmarshalListValue(m)
|
||||
|
||||
case "google.protobuf.Value":
|
||||
return d.unmarshalKnownValue(m)
|
||||
return o.unmarshalKnownValue(m)
|
||||
|
||||
case "google.protobuf.Duration":
|
||||
return d.unmarshalDuration(m)
|
||||
return o.unmarshalDuration(m)
|
||||
|
||||
case "google.protobuf.Timestamp":
|
||||
return d.unmarshalTimestamp(m)
|
||||
return o.unmarshalTimestamp(m)
|
||||
|
||||
case "google.protobuf.FieldMask":
|
||||
return d.unmarshalFieldMask(m)
|
||||
return o.unmarshalFieldMask(m)
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("%q does not have a custom unmarshaler", name))
|
||||
@ -135,14 +135,14 @@ func (d decoder) unmarshalCustomType(m pref.Message) error {
|
||||
// custom JSON representation, that representation will be embedded adding a
|
||||
// field `value` which holds the custom JSON in addition to the `@type` field.
|
||||
|
||||
func (e encoder) marshalAny(m pref.Message) error {
|
||||
func (o MarshalOptions) marshalAny(m pref.Message) error {
|
||||
var nerr errors.NonFatal
|
||||
msgType := m.Type()
|
||||
knownFields := m.KnownFields()
|
||||
|
||||
// Start writing the JSON object.
|
||||
e.StartObject()
|
||||
defer e.EndObject()
|
||||
o.encoder.StartObject()
|
||||
defer o.encoder.EndObject()
|
||||
|
||||
if !knownFields.Has(fieldnum.Any_TypeUrl) {
|
||||
if !knownFields.Has(fieldnum.Any_Value) {
|
||||
@ -159,13 +159,13 @@ func (e encoder) marshalAny(m pref.Message) error {
|
||||
|
||||
// Marshal out @type field.
|
||||
typeURL := typeVal.String()
|
||||
e.WriteName("@type")
|
||||
if err := e.WriteString(typeURL); !nerr.Merge(err) {
|
||||
o.encoder.WriteName("@type")
|
||||
if err := o.encoder.WriteString(typeURL); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
// Resolve the type in order to unmarshal value field.
|
||||
emt, err := e.resolver.FindMessageByURL(typeURL)
|
||||
emt, err := o.Resolver.FindMessageByURL(typeURL)
|
||||
if !nerr.Merge(err) {
|
||||
return errors.New("%s: unable to resolve %q: %v", msgType.FullName(), typeURL, err)
|
||||
}
|
||||
@ -181,12 +181,12 @@ func (e encoder) marshalAny(m pref.Message) error {
|
||||
// with corresponding custom JSON encoding of the embedded message as a
|
||||
// field.
|
||||
if isCustomType(emt.FullName()) {
|
||||
e.WriteName("value")
|
||||
return e.marshalCustomType(em)
|
||||
o.encoder.WriteName("value")
|
||||
return o.marshalCustomType(em)
|
||||
}
|
||||
|
||||
// Else, marshal out the embedded message's fields in this Any object.
|
||||
if err := e.marshalFields(em); !nerr.Merge(err) {
|
||||
if err := o.marshalFields(em); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -195,7 +195,7 @@ func (e encoder) marshalAny(m pref.Message) error {
|
||||
|
||||
// Wrapper types are encoded as JSON primitives like string, number or boolean.
|
||||
|
||||
func (e encoder) marshalWrapperType(m pref.Message) error {
|
||||
func (o MarshalOptions) marshalWrapperType(m pref.Message) error {
|
||||
msgType := m.Type()
|
||||
fieldDescs := msgType.Fields()
|
||||
knownFields := m.KnownFields()
|
||||
@ -204,10 +204,10 @@ func (e encoder) marshalWrapperType(m pref.Message) error {
|
||||
const num = fieldnum.BoolValue_Value
|
||||
fd := fieldDescs.ByNumber(num)
|
||||
val := knownFields.Get(num)
|
||||
return e.marshalSingular(val, fd)
|
||||
return o.marshalSingular(val, fd)
|
||||
}
|
||||
|
||||
func (d decoder) unmarshalWrapperType(m pref.Message) error {
|
||||
func (o UnmarshalOptions) unmarshalWrapperType(m pref.Message) error {
|
||||
var nerr errors.NonFatal
|
||||
msgType := m.Type()
|
||||
fieldDescs := msgType.Fields()
|
||||
@ -216,7 +216,7 @@ func (d decoder) unmarshalWrapperType(m pref.Message) error {
|
||||
// The "value" field has the same field number for all wrapper types.
|
||||
const num = fieldnum.BoolValue_Value
|
||||
fd := fieldDescs.ByNumber(num)
|
||||
val, err := d.unmarshalScalar(fd)
|
||||
val, err := o.unmarshalScalar(fd)
|
||||
if !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
@ -227,55 +227,55 @@ func (d decoder) unmarshalWrapperType(m pref.Message) error {
|
||||
// The JSON representation for Struct is a JSON object that contains the encoded
|
||||
// Struct.fields map and follows the serialization rules for a map.
|
||||
|
||||
func (e encoder) marshalStruct(m pref.Message) error {
|
||||
func (o MarshalOptions) marshalStruct(m pref.Message) error {
|
||||
msgType := m.Type()
|
||||
fieldDescs := msgType.Fields()
|
||||
knownFields := m.KnownFields()
|
||||
|
||||
fd := fieldDescs.ByNumber(fieldnum.Struct_Fields)
|
||||
val := knownFields.Get(fieldnum.Struct_Fields)
|
||||
return e.marshalMap(val.Map(), fd)
|
||||
return o.marshalMap(val.Map(), fd)
|
||||
}
|
||||
|
||||
func (d decoder) unmarshalStruct(m pref.Message) error {
|
||||
func (o UnmarshalOptions) unmarshalStruct(m pref.Message) error {
|
||||
msgType := m.Type()
|
||||
fieldDescs := msgType.Fields()
|
||||
knownFields := m.KnownFields()
|
||||
|
||||
fd := fieldDescs.ByNumber(fieldnum.Struct_Fields)
|
||||
val := knownFields.Get(fieldnum.Struct_Fields)
|
||||
return d.unmarshalMap(val.Map(), fd)
|
||||
return o.unmarshalMap(val.Map(), fd)
|
||||
}
|
||||
|
||||
// The JSON representation for ListValue is JSON array that contains the encoded
|
||||
// ListValue.values repeated field and follows the serialization rules for a
|
||||
// repeated field.
|
||||
|
||||
func (e encoder) marshalListValue(m pref.Message) error {
|
||||
func (o MarshalOptions) marshalListValue(m pref.Message) error {
|
||||
msgType := m.Type()
|
||||
fieldDescs := msgType.Fields()
|
||||
knownFields := m.KnownFields()
|
||||
|
||||
fd := fieldDescs.ByNumber(fieldnum.ListValue_Values)
|
||||
val := knownFields.Get(fieldnum.ListValue_Values)
|
||||
return e.marshalList(val.List(), fd)
|
||||
return o.marshalList(val.List(), fd)
|
||||
}
|
||||
|
||||
func (d decoder) unmarshalListValue(m pref.Message) error {
|
||||
func (o UnmarshalOptions) unmarshalListValue(m pref.Message) error {
|
||||
msgType := m.Type()
|
||||
fieldDescs := msgType.Fields()
|
||||
knownFields := m.KnownFields()
|
||||
|
||||
fd := fieldDescs.ByNumber(fieldnum.ListValue_Values)
|
||||
val := knownFields.Get(fieldnum.ListValue_Values)
|
||||
return d.unmarshalList(val.List(), fd)
|
||||
return o.unmarshalList(val.List(), fd)
|
||||
}
|
||||
|
||||
// The JSON representation for a Value is dependent on the oneof field that is
|
||||
// set. Each of the field in the oneof has its own custom serialization rule. A
|
||||
// Value message needs to be a oneof field set, else it is an error.
|
||||
|
||||
func (e encoder) marshalKnownValue(m pref.Message) error {
|
||||
func (o MarshalOptions) marshalKnownValue(m pref.Message) error {
|
||||
msgType := m.Type()
|
||||
fieldDescs := msgType.Oneofs().Get(0).Fields()
|
||||
knownFields := m.KnownFields()
|
||||
@ -288,7 +288,7 @@ func (e encoder) marshalKnownValue(m pref.Message) error {
|
||||
}
|
||||
// Only one field should be set.
|
||||
val := knownFields.Get(num)
|
||||
return e.marshalSingular(val, fd)
|
||||
return o.marshalSingular(val, fd)
|
||||
}
|
||||
|
||||
// Return error if none of the fields are set.
|
||||
@ -300,17 +300,17 @@ func isKnownValue(fd pref.FieldDescriptor) bool {
|
||||
return md != nil && md.FullName() == "google.protobuf.Value"
|
||||
}
|
||||
|
||||
func (d decoder) unmarshalKnownValue(m pref.Message) error {
|
||||
func (o UnmarshalOptions) unmarshalKnownValue(m pref.Message) error {
|
||||
var nerr errors.NonFatal
|
||||
knownFields := m.KnownFields()
|
||||
|
||||
switch d.Peek() {
|
||||
switch o.decoder.Peek() {
|
||||
case json.Null:
|
||||
d.Read()
|
||||
o.decoder.Read()
|
||||
knownFields.Set(fieldnum.Value_NullValue, pref.ValueOf(pref.EnumNumber(0)))
|
||||
|
||||
case json.Bool:
|
||||
jval, err := d.Read()
|
||||
jval, err := o.decoder.Read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -321,7 +321,7 @@ func (d decoder) unmarshalKnownValue(m pref.Message) error {
|
||||
knownFields.Set(fieldnum.Value_BoolValue, val)
|
||||
|
||||
case json.Number:
|
||||
jval, err := d.Read()
|
||||
jval, err := o.decoder.Read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -338,7 +338,7 @@ func (d decoder) unmarshalKnownValue(m pref.Message) error {
|
||||
// however, there is no way to identify that and hence a JSON string is
|
||||
// always assigned to the string_value field, which means that certain
|
||||
// encoding cannot be parsed back to the same field.
|
||||
jval, err := d.Read()
|
||||
jval, err := o.decoder.Read()
|
||||
if !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
@ -350,20 +350,20 @@ func (d decoder) unmarshalKnownValue(m pref.Message) error {
|
||||
|
||||
case json.StartObject:
|
||||
m := knownFields.NewMessage(fieldnum.Value_StructValue)
|
||||
if err := d.unmarshalStruct(m); !nerr.Merge(err) {
|
||||
if err := o.unmarshalStruct(m); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
knownFields.Set(fieldnum.Value_StructValue, pref.ValueOf(m))
|
||||
|
||||
case json.StartArray:
|
||||
m := knownFields.NewMessage(fieldnum.Value_ListValue)
|
||||
if err := d.unmarshalListValue(m); !nerr.Merge(err) {
|
||||
if err := o.unmarshalListValue(m); !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
knownFields.Set(fieldnum.Value_ListValue, pref.ValueOf(m))
|
||||
|
||||
default:
|
||||
jval, err := d.Read()
|
||||
jval, err := o.decoder.Read()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -390,7 +390,7 @@ const (
|
||||
maxSecondsInDuration = 315576000000
|
||||
)
|
||||
|
||||
func (e encoder) marshalDuration(m pref.Message) error {
|
||||
func (o MarshalOptions) marshalDuration(m pref.Message) error {
|
||||
msgType := m.Type()
|
||||
knownFields := m.KnownFields()
|
||||
|
||||
@ -420,13 +420,13 @@ func (e encoder) marshalDuration(m pref.Message) error {
|
||||
x = strings.TrimSuffix(x, "000")
|
||||
x = strings.TrimSuffix(x, "000")
|
||||
x = strings.TrimSuffix(x, ".000")
|
||||
e.WriteString(x + "s")
|
||||
o.encoder.WriteString(x + "s")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d decoder) unmarshalDuration(m pref.Message) error {
|
||||
func (o UnmarshalOptions) unmarshalDuration(m pref.Message) error {
|
||||
var nerr errors.NonFatal
|
||||
jval, err := d.Read()
|
||||
jval, err := o.decoder.Read()
|
||||
if !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
@ -527,7 +527,7 @@ const (
|
||||
minTimestampSeconds = -62135596800
|
||||
)
|
||||
|
||||
func (e encoder) marshalTimestamp(m pref.Message) error {
|
||||
func (o MarshalOptions) marshalTimestamp(m pref.Message) error {
|
||||
msgType := m.Type()
|
||||
knownFields := m.KnownFields()
|
||||
|
||||
@ -548,13 +548,13 @@ func (e encoder) marshalTimestamp(m pref.Message) error {
|
||||
x = strings.TrimSuffix(x, "000")
|
||||
x = strings.TrimSuffix(x, "000")
|
||||
x = strings.TrimSuffix(x, ".000")
|
||||
e.WriteString(x + "Z")
|
||||
o.encoder.WriteString(x + "Z")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d decoder) unmarshalTimestamp(m pref.Message) error {
|
||||
func (o UnmarshalOptions) unmarshalTimestamp(m pref.Message) error {
|
||||
var nerr errors.NonFatal
|
||||
jval, err := d.Read()
|
||||
jval, err := o.decoder.Read()
|
||||
if !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
@ -586,7 +586,7 @@ func (d decoder) unmarshalTimestamp(m pref.Message) error {
|
||||
// lower-camel naming conventions. Encoding should fail if the path name would
|
||||
// end up differently after a round-trip.
|
||||
|
||||
func (e encoder) marshalFieldMask(m pref.Message) error {
|
||||
func (o MarshalOptions) marshalFieldMask(m pref.Message) error {
|
||||
msgType := m.Type()
|
||||
knownFields := m.KnownFields()
|
||||
name := msgType.FullName()
|
||||
@ -605,13 +605,13 @@ func (e encoder) marshalFieldMask(m pref.Message) error {
|
||||
paths = append(paths, cc)
|
||||
}
|
||||
|
||||
e.WriteString(strings.Join(paths, ","))
|
||||
o.encoder.WriteString(strings.Join(paths, ","))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d decoder) unmarshalFieldMask(m pref.Message) error {
|
||||
func (o UnmarshalOptions) unmarshalFieldMask(m pref.Message) error {
|
||||
var nerr errors.NonFatal
|
||||
jval, err := d.Read()
|
||||
jval, err := o.decoder.Read()
|
||||
if !nerr.Merge(err) {
|
||||
return err
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user