protogen, encoding/jsonpb, encoding/textpb: rename packages
Rename encoding/*pb to follow the convention of prefixing package names
with 'proto':
google.golang.org/protobuf/encoding/protojson
google.golang.org/protobuf/encoding/prototext
Move protogen under a compiler/ directory, just in case we ever do add
more compiler-related packages.
google.golang.org/protobuf/compiler/protogen
Change-Id: I31010cb5cabcea8274fffcac468477b58b56e8eb
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/177178
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/encoding/jsonpb/bench_test.go b/encoding/jsonpb/bench_test.go
deleted file mode 100644
index 3dcc5e0..0000000
--- a/encoding/jsonpb/bench_test.go
+++ /dev/null
@@ -1,23 +0,0 @@
-// Copyright 2019 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package jsonpb_test
-
-import (
- "testing"
-
- "google.golang.org/protobuf/encoding/jsonpb"
- knownpb "google.golang.org/protobuf/types/known"
-)
-
-func BenchmarkUnmarshal_Duration(b *testing.B) {
- input := []byte(`"-123456789.123456789s"`)
-
- for i := 0; i < b.N; i++ {
- err := jsonpb.Unmarshal(&knownpb.Duration{}, input)
- if err != nil {
- b.Fatal(err)
- }
- }
-}
diff --git a/encoding/jsonpb/decode.go b/encoding/jsonpb/decode.go
deleted file mode 100644
index 1712bf6..0000000
--- a/encoding/jsonpb/decode.go
+++ /dev/null
@@ -1,726 +0,0 @@
-// Copyright 2019 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package jsonpb
-
-import (
- "encoding/base64"
- "fmt"
- "math"
- "strconv"
- "strings"
-
- "google.golang.org/protobuf/internal/encoding/json"
- "google.golang.org/protobuf/internal/errors"
- "google.golang.org/protobuf/internal/pragma"
- "google.golang.org/protobuf/internal/set"
- "google.golang.org/protobuf/proto"
- pref "google.golang.org/protobuf/reflect/protoreflect"
- "google.golang.org/protobuf/reflect/protoregistry"
-)
-
-// Unmarshal reads the given []byte into the given proto.Message.
-func Unmarshal(m proto.Message, b []byte) error {
- return UnmarshalOptions{}.Unmarshal(m, b)
-}
-
-// UnmarshalOptions is a configurable JSON format parser.
-type UnmarshalOptions struct {
- pragma.NoUnkeyedLiterals
-
- // If AllowPartial is set, input for messages that will result in missing
- // required fields will not return an error.
- AllowPartial bool
-
- // If DiscardUnknown is set, unknown fields are ignored.
- DiscardUnknown bool
-
- // Resolver is the registry used for type lookups when unmarshaling extensions
- // 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
-// options in UnmarshalOptions object. It will clear the message first before
-// setting the fields. If it returns an error, the given message may be
-// partially set.
-func (o UnmarshalOptions) Unmarshal(m proto.Message, b []byte) error {
- mr := m.ProtoReflect()
- // TODO: Determine if we would like to have an option for merging or only
- // have merging behavior. We should at least be consistent with textproto
- // marshaling.
- resetMessage(mr)
-
- if o.Resolver == nil {
- o.Resolver = protoregistry.GlobalTypes
- }
- o.decoder = json.NewDecoder(b)
-
- var nerr errors.NonFatal
- if err := o.unmarshalMessage(mr, false); !nerr.Merge(err) {
- return err
- }
-
- // Check for EOF.
- val, err := o.decoder.Read()
- if err != nil {
- return err
- }
- if val.Type() != json.EOF {
- return unexpectedJSONError{val}
- }
-
- if !o.AllowPartial {
- nerr.Merge(proto.IsInitialized(m))
- }
- return nerr.E
-}
-
-// resetMessage clears all fields of given protoreflect.Message.
-func resetMessage(m pref.Message) {
- knownFields := m.KnownFields()
- knownFields.Range(func(num pref.FieldNumber, _ pref.Value) bool {
- knownFields.Clear(num)
- return true
- })
- unknownFields := m.UnknownFields()
- unknownFields.Range(func(num pref.FieldNumber, _ pref.RawFields) bool {
- unknownFields.Set(num, nil)
- return true
- })
- extTypes := knownFields.ExtensionTypes()
- extTypes.Range(func(xt pref.ExtensionType) bool {
- extTypes.Remove(xt)
- return true
- })
-}
-
-// unexpectedJSONError is an error that contains the unexpected json.Value. This
-// 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 {
- value json.Value
-}
-
-func (e unexpectedJSONError) Error() string {
- return newError("unexpected value %s", e.value).Error()
-}
-
-// newError returns an error object. If one of the values passed in is of
-// json.Value type, it produces an error with position info.
-func newError(f string, x ...interface{}) error {
- var hasValue bool
- var line, column int
- for i := 0; i < len(x); i++ {
- if val, ok := x[i].(json.Value); ok {
- line, column = val.Position()
- hasValue = true
- break
- }
- }
- e := errors.New(f, x...)
- if hasValue {
- return errors.New("(line %d:%d): %v", line, column, e)
- }
- return e
-}
-
-// unmarshalMessage unmarshals a message into the given protoreflect.Message.
-func (o UnmarshalOptions) unmarshalMessage(m pref.Message, skipTypeURL bool) error {
- var nerr errors.NonFatal
-
- if isCustomType(m.Descriptor().FullName()) {
- return o.unmarshalCustomType(m)
- }
-
- jval, err := o.decoder.Read()
- if !nerr.Merge(err) {
- return err
- }
- if jval.Type() != json.StartObject {
- return unexpectedJSONError{jval}
- }
-
- if err := o.unmarshalFields(m, skipTypeURL); !nerr.Merge(err) {
- return err
- }
-
- return nerr.E
-}
-
-// unmarshalFields unmarshals the fields into the given protoreflect.Message.
-func (o UnmarshalOptions) unmarshalFields(m pref.Message, skipTypeURL bool) error {
- var nerr errors.NonFatal
- var seenNums set.Ints
- var seenOneofs set.Ints
-
- messageDesc := m.Descriptor()
- knownFields := m.KnownFields()
- fieldDescs := messageDesc.Fields()
- xtTypes := knownFields.ExtensionTypes()
-
-Loop:
- for {
- // Read field name.
- jval, err := o.decoder.Read()
- if !nerr.Merge(err) {
- return err
- }
- switch jval.Type() {
- default:
- return unexpectedJSONError{jval}
- case json.EndObject:
- break Loop
- case json.Name:
- // Continue below.
- }
-
- name, err := jval.Name()
- if !nerr.Merge(err) {
- return err
- }
- // Unmarshaling a non-custom embedded message in Any will contain the
- // JSON field "@type" which should be skipped because it is not a field
- // of the embedded message, but simply an artifact of the Any format.
- if skipTypeURL && name == "@type" {
- o.decoder.Read()
- continue
- }
-
- // Get the FieldDescriptor.
- var fd pref.FieldDescriptor
- if strings.HasPrefix(name, "[") && strings.HasSuffix(name, "]") {
- // Only extension names are in [name] format.
- xtName := pref.FullName(name[1 : len(name)-1])
- xt := xtTypes.ByName(xtName)
- if xt == nil {
- xt, err = o.findExtension(xtName)
- if err != nil && err != protoregistry.NotFound {
- return errors.New("unable to resolve [%v]: %v", xtName, err)
- }
- if xt != nil {
- xtTypes.Register(xt)
- }
- }
- if xt != nil {
- fd = xt.Descriptor()
- }
- } else {
- // The name can either be the JSON name or the proto field name.
- fd = fieldDescs.ByJSONName(name)
- if fd == nil {
- fd = fieldDescs.ByName(pref.Name(name))
- }
- }
-
- if fd == nil {
- // Field is unknown.
- if o.DiscardUnknown {
- if err := skipJSONValue(o.decoder); !nerr.Merge(err) {
- return err
- }
- continue
- }
- return newError("%v contains unknown field %s", messageDesc.FullName(), jval)
- }
-
- // Do not allow duplicate fields.
- num := uint64(fd.Number())
- if seenNums.Has(num) {
- return newError("%v contains repeated field %s", messageDesc.FullName(), jval)
- }
- seenNums.Set(num)
-
- // No need to set values for JSON null unless the field type is
- // google.protobuf.Value or google.protobuf.NullValue.
- if o.decoder.Peek() == json.Null && !isKnownValue(fd) && !isNullValue(fd) {
- o.decoder.Read()
- continue
- }
-
- switch {
- case fd.IsList():
- list := knownFields.Get(fd.Number()).List()
- if err := o.unmarshalList(list, fd); !nerr.Merge(err) {
- return errors.New("%v|%q: %v", fd.FullName(), name, err)
- }
- case fd.IsMap():
- mmap := knownFields.Get(fd.Number()).Map()
- if err := o.unmarshalMap(mmap, fd); !nerr.Merge(err) {
- return errors.New("%v|%q: %v", fd.FullName(), name, err)
- }
- default:
- // If field is a oneof, check if it has already been set.
- if od := fd.ContainingOneof(); od != nil {
- idx := uint64(od.Index())
- if seenOneofs.Has(idx) {
- return errors.New("%v: oneof is already set", od.FullName())
- }
- seenOneofs.Set(idx)
- }
-
- // Required or optional fields.
- if err := o.unmarshalSingular(knownFields, fd); !nerr.Merge(err) {
- return errors.New("%v|%q: %v", fd.FullName(), name, err)
- }
- }
- }
-
- return nerr.E
-}
-
-// findExtension returns protoreflect.ExtensionType from the resolver if found.
-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 = o.Resolver.FindExtensionByName(xtName + ".message_set_extension")
- if err == nil && isMessageSetExtension(xt) {
- return xt, nil
- }
- return nil, protoregistry.NotFound
-}
-
-func isKnownValue(fd pref.FieldDescriptor) bool {
- md := fd.Message()
- return md != nil && md.FullName() == "google.protobuf.Value"
-}
-
-func isNullValue(fd pref.FieldDescriptor) bool {
- ed := fd.Enum()
- return ed != nil && ed.FullName() == "google.protobuf.NullValue"
-}
-
-// unmarshalSingular unmarshals to the non-repeated field specified by the given
-// FieldDescriptor.
-func (o UnmarshalOptions) unmarshalSingular(knownFields pref.KnownFields, fd pref.FieldDescriptor) error {
- var val pref.Value
- var err error
- num := fd.Number()
-
- switch fd.Kind() {
- case pref.MessageKind, pref.GroupKind:
- m := knownFields.NewMessage(num)
- err = o.unmarshalMessage(m, false)
- val = pref.ValueOf(m)
- default:
- val, err = o.unmarshalScalar(fd)
- }
-
- var nerr errors.NonFatal
- if !nerr.Merge(err) {
- return err
- }
- knownFields.Set(num, val)
- return nerr.E
-}
-
-// unmarshalScalar unmarshals to a scalar/enum protoreflect.Value specified by
-// the given FieldDescriptor.
-func (o UnmarshalOptions) unmarshalScalar(fd pref.FieldDescriptor) (pref.Value, error) {
- const b32 int = 32
- const b64 int = 64
-
- var nerr errors.NonFatal
- jval, err := o.decoder.Read()
- if !nerr.Merge(err) {
- return pref.Value{}, err
- }
-
- kind := fd.Kind()
- switch kind {
- case pref.BoolKind:
- return unmarshalBool(jval)
-
- case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
- return unmarshalInt(jval, b32)
-
- case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
- return unmarshalInt(jval, b64)
-
- case pref.Uint32Kind, pref.Fixed32Kind:
- return unmarshalUint(jval, b32)
-
- case pref.Uint64Kind, pref.Fixed64Kind:
- return unmarshalUint(jval, b64)
-
- case pref.FloatKind:
- return unmarshalFloat(jval, b32)
-
- case pref.DoubleKind:
- return unmarshalFloat(jval, b64)
-
- case pref.StringKind:
- pval, err := unmarshalString(jval)
- if !nerr.Merge(err) {
- return pval, err
- }
- return pval, nerr.E
-
- case pref.BytesKind:
- return unmarshalBytes(jval)
-
- case pref.EnumKind:
- return unmarshalEnum(jval, fd)
- }
-
- panic(fmt.Sprintf("invalid scalar kind %v", kind))
-}
-
-func unmarshalBool(jval json.Value) (pref.Value, error) {
- if jval.Type() != json.Bool {
- return pref.Value{}, unexpectedJSONError{jval}
- }
- b, err := jval.Bool()
- return pref.ValueOf(b), err
-}
-
-func unmarshalInt(jval json.Value, bitSize int) (pref.Value, error) {
- switch jval.Type() {
- case json.Number:
- return getInt(jval, bitSize)
-
- case json.String:
- // Decode number from string.
- s := strings.TrimSpace(jval.String())
- if len(s) != len(jval.String()) {
- return pref.Value{}, errors.New("invalid number %v", jval.Raw())
- }
- dec := json.NewDecoder([]byte(s))
- var nerr errors.NonFatal
- jval, err := dec.Read()
- if !nerr.Merge(err) {
- return pref.Value{}, err
- }
- return getInt(jval, bitSize)
- }
- return pref.Value{}, unexpectedJSONError{jval}
-}
-
-func getInt(jval json.Value, bitSize int) (pref.Value, error) {
- n, err := jval.Int(bitSize)
- if err != nil {
- return pref.Value{}, err
- }
- if bitSize == 32 {
- return pref.ValueOf(int32(n)), nil
- }
- return pref.ValueOf(n), nil
-}
-
-func unmarshalUint(jval json.Value, bitSize int) (pref.Value, error) {
- switch jval.Type() {
- case json.Number:
- return getUint(jval, bitSize)
-
- case json.String:
- // Decode number from string.
- s := strings.TrimSpace(jval.String())
- if len(s) != len(jval.String()) {
- return pref.Value{}, errors.New("invalid number %v", jval.Raw())
- }
- dec := json.NewDecoder([]byte(s))
- var nerr errors.NonFatal
- jval, err := dec.Read()
- if !nerr.Merge(err) {
- return pref.Value{}, err
- }
- return getUint(jval, bitSize)
- }
- return pref.Value{}, unexpectedJSONError{jval}
-}
-
-func getUint(jval json.Value, bitSize int) (pref.Value, error) {
- n, err := jval.Uint(bitSize)
- if err != nil {
- return pref.Value{}, err
- }
- if bitSize == 32 {
- return pref.ValueOf(uint32(n)), nil
- }
- return pref.ValueOf(n), nil
-}
-
-func unmarshalFloat(jval json.Value, bitSize int) (pref.Value, error) {
- switch jval.Type() {
- case json.Number:
- return getFloat(jval, bitSize)
-
- case json.String:
- s := jval.String()
- switch s {
- case "NaN":
- if bitSize == 32 {
- return pref.ValueOf(float32(math.NaN())), nil
- }
- return pref.ValueOf(math.NaN()), nil
- case "Infinity":
- if bitSize == 32 {
- return pref.ValueOf(float32(math.Inf(+1))), nil
- }
- return pref.ValueOf(math.Inf(+1)), nil
- case "-Infinity":
- if bitSize == 32 {
- return pref.ValueOf(float32(math.Inf(-1))), nil
- }
- return pref.ValueOf(math.Inf(-1)), nil
- }
- // Decode number from string.
- if len(s) != len(strings.TrimSpace(s)) {
- return pref.Value{}, errors.New("invalid number %v", jval.Raw())
- }
- dec := json.NewDecoder([]byte(s))
- var nerr errors.NonFatal
- jval, err := dec.Read()
- if !nerr.Merge(err) {
- return pref.Value{}, err
- }
- return getFloat(jval, bitSize)
- }
- return pref.Value{}, unexpectedJSONError{jval}
-}
-
-func getFloat(jval json.Value, bitSize int) (pref.Value, error) {
- n, err := jval.Float(bitSize)
- if err != nil {
- return pref.Value{}, err
- }
- if bitSize == 32 {
- return pref.ValueOf(float32(n)), nil
- }
- return pref.ValueOf(n), nil
-}
-
-func unmarshalString(jval json.Value) (pref.Value, error) {
- if jval.Type() != json.String {
- return pref.Value{}, unexpectedJSONError{jval}
- }
- return pref.ValueOf(jval.String()), nil
-}
-
-func unmarshalBytes(jval json.Value) (pref.Value, error) {
- if jval.Type() != json.String {
- return pref.Value{}, unexpectedJSONError{jval}
- }
-
- s := jval.String()
- enc := base64.StdEncoding
- if strings.ContainsAny(s, "-_") {
- enc = base64.URLEncoding
- }
- if len(s)%4 != 0 {
- enc = enc.WithPadding(base64.NoPadding)
- }
- b, err := enc.DecodeString(s)
- if err != nil {
- return pref.Value{}, err
- }
- return pref.ValueOf(b), nil
-}
-
-func unmarshalEnum(jval json.Value, fd pref.FieldDescriptor) (pref.Value, error) {
- switch jval.Type() {
- case json.String:
- // Lookup EnumNumber based on name.
- s := jval.String()
- if enumVal := fd.Enum().Values().ByName(pref.Name(s)); enumVal != nil {
- return pref.ValueOf(enumVal.Number()), nil
- }
- return pref.Value{}, newError("invalid enum value %q", jval)
-
- case json.Number:
- n, err := jval.Int(32)
- if err != nil {
- return pref.Value{}, err
- }
- return pref.ValueOf(pref.EnumNumber(n)), nil
-
- case json.Null:
- // This is only valid for google.protobuf.NullValue.
- if isNullValue(fd) {
- return pref.ValueOf(pref.EnumNumber(0)), nil
- }
- }
-
- return pref.Value{}, unexpectedJSONError{jval}
-}
-
-func (o UnmarshalOptions) unmarshalList(list pref.List, fd pref.FieldDescriptor) error {
- var nerr errors.NonFatal
- jval, err := o.decoder.Read()
- if !nerr.Merge(err) {
- return err
- }
- if jval.Type() != json.StartArray {
- return unexpectedJSONError{jval}
- }
-
- switch fd.Kind() {
- case pref.MessageKind, pref.GroupKind:
- for {
- m := list.NewMessage()
- err := o.unmarshalMessage(m, false)
- if !nerr.Merge(err) {
- if e, ok := err.(unexpectedJSONError); ok {
- if e.value.Type() == json.EndArray {
- // Done with list.
- return nerr.E
- }
- }
- return err
- }
- list.Append(pref.ValueOf(m))
- }
- default:
- for {
- val, err := o.unmarshalScalar(fd)
- if !nerr.Merge(err) {
- if e, ok := err.(unexpectedJSONError); ok {
- if e.value.Type() == json.EndArray {
- // Done with list.
- return nerr.E
- }
- }
- return err
- }
- list.Append(val)
- }
- }
- return nerr.E
-}
-
-func (o UnmarshalOptions) unmarshalMap(mmap pref.Map, fd pref.FieldDescriptor) error {
- var nerr errors.NonFatal
- jval, err := o.decoder.Read()
- if !nerr.Merge(err) {
- return err
- }
- if jval.Type() != json.StartObject {
- return unexpectedJSONError{jval}
- }
-
- // Determine ahead whether map entry is a scalar type or a message type in
- // order to call the appropriate unmarshalMapValue func inside the for loop
- // below.
- var unmarshalMapValue func() (pref.Value, error)
- switch fd.MapValue().Kind() {
- case pref.MessageKind, pref.GroupKind:
- unmarshalMapValue = func() (pref.Value, error) {
- var nerr errors.NonFatal
- m := mmap.NewMessage()
- if err := o.unmarshalMessage(m, false); !nerr.Merge(err) {
- return pref.Value{}, err
- }
- return pref.ValueOf(m), nerr.E
- }
- default:
- unmarshalMapValue = func() (pref.Value, error) {
- return o.unmarshalScalar(fd.MapValue())
- }
- }
-
-Loop:
- for {
- // Read field name.
- jval, err := o.decoder.Read()
- if !nerr.Merge(err) {
- return err
- }
- switch jval.Type() {
- default:
- return unexpectedJSONError{jval}
- case json.EndObject:
- break Loop
- case json.Name:
- // Continue.
- }
-
- name, err := jval.Name()
- if !nerr.Merge(err) {
- return err
- }
-
- // Unmarshal field name.
- pkey, err := unmarshalMapKey(name, fd.MapKey())
- if !nerr.Merge(err) {
- return err
- }
-
- // Check for duplicate field name.
- if mmap.Has(pkey) {
- return newError("duplicate map key %q", jval)
- }
-
- // Read and unmarshal field value.
- pval, err := unmarshalMapValue()
- if !nerr.Merge(err) {
- return err
- }
-
- mmap.Set(pkey, pval)
- }
-
- return nerr.E
-}
-
-// unmarshalMapKey converts given string into a protoreflect.MapKey. A map key type is any
-// integral or string type.
-func unmarshalMapKey(name string, fd pref.FieldDescriptor) (pref.MapKey, error) {
- const b32 = 32
- const b64 = 64
- const base10 = 10
-
- kind := fd.Kind()
- switch kind {
- case pref.StringKind:
- return pref.ValueOf(name).MapKey(), nil
-
- case pref.BoolKind:
- switch name {
- case "true":
- return pref.ValueOf(true).MapKey(), nil
- case "false":
- return pref.ValueOf(false).MapKey(), nil
- }
- return pref.MapKey{}, errors.New("invalid value for boolean key %q", name)
-
- case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
- n, err := strconv.ParseInt(name, base10, b32)
- if err != nil {
- return pref.MapKey{}, err
- }
- return pref.ValueOf(int32(n)).MapKey(), nil
-
- case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
- n, err := strconv.ParseInt(name, base10, b64)
- if err != nil {
- return pref.MapKey{}, err
- }
- return pref.ValueOf(int64(n)).MapKey(), nil
-
- case pref.Uint32Kind, pref.Fixed32Kind:
- n, err := strconv.ParseUint(name, base10, b32)
- if err != nil {
- return pref.MapKey{}, err
- }
- return pref.ValueOf(uint32(n)).MapKey(), nil
-
- case pref.Uint64Kind, pref.Fixed64Kind:
- n, err := strconv.ParseUint(name, base10, b64)
- if err != nil {
- return pref.MapKey{}, err
- }
- return pref.ValueOf(uint64(n)).MapKey(), nil
- }
-
- panic(fmt.Sprintf("%s: invalid kind %s for map key", fd.FullName(), kind))
-}
diff --git a/encoding/jsonpb/decode_test.go b/encoding/jsonpb/decode_test.go
deleted file mode 100644
index 0410d53..0000000
--- a/encoding/jsonpb/decode_test.go
+++ /dev/null
@@ -1,2611 +0,0 @@
-// Copyright 2019 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package jsonpb_test
-
-import (
- "bytes"
- "math"
- "testing"
-
- protoV1 "github.com/golang/protobuf/proto"
- "google.golang.org/protobuf/encoding/jsonpb"
- "google.golang.org/protobuf/encoding/testprotos/pb2"
- "google.golang.org/protobuf/encoding/testprotos/pb3"
- pimpl "google.golang.org/protobuf/internal/impl"
- "google.golang.org/protobuf/internal/scalar"
- "google.golang.org/protobuf/proto"
- preg "google.golang.org/protobuf/reflect/protoregistry"
- "google.golang.org/protobuf/runtime/protoiface"
-
- knownpb "google.golang.org/protobuf/types/known"
-)
-
-func init() {
- // TODO: remove these registerExtension calls when generated code registers
- // to V2 global registry.
- registerExtension(pb2.E_OptExtBool)
- registerExtension(pb2.E_OptExtString)
- registerExtension(pb2.E_OptExtEnum)
- registerExtension(pb2.E_OptExtNested)
- registerExtension(pb2.E_RptExtFixed32)
- registerExtension(pb2.E_RptExtEnum)
- registerExtension(pb2.E_RptExtNested)
- registerExtension(pb2.E_ExtensionsContainer_OptExtBool)
- registerExtension(pb2.E_ExtensionsContainer_OptExtString)
- registerExtension(pb2.E_ExtensionsContainer_OptExtEnum)
- registerExtension(pb2.E_ExtensionsContainer_OptExtNested)
- registerExtension(pb2.E_ExtensionsContainer_RptExtString)
- registerExtension(pb2.E_ExtensionsContainer_RptExtEnum)
- registerExtension(pb2.E_ExtensionsContainer_RptExtNested)
- registerExtension(pb2.E_MessageSetExtension)
- registerExtension(pb2.E_MessageSetExtension_MessageSetExtension)
- registerExtension(pb2.E_MessageSetExtension_NotMessageSetExtension)
- registerExtension(pb2.E_MessageSetExtension_ExtNested)
- registerExtension(pb2.E_FakeMessageSetExtension_MessageSetExtension)
-}
-
-func registerExtension(xd *protoiface.ExtensionDescV1) {
- preg.GlobalTypes.Register(xd.Type)
-}
-
-func TestUnmarshal(t *testing.T) {
- tests := []struct {
- desc string
- umo jsonpb.UnmarshalOptions
- inputMessage proto.Message
- inputText string
- wantMessage proto.Message
- // TODO: verify expected error message substring.
- wantErr bool
- }{{
- desc: "proto2 empty message",
- inputMessage: &pb2.Scalars{},
- inputText: "{}",
- wantMessage: &pb2.Scalars{},
- }, {
- desc: "unexpected value instead of EOF",
- inputMessage: &pb2.Scalars{},
- inputText: "{} {}",
- wantErr: true,
- }, {
- desc: "proto2 optional scalars set to zero values",
- inputMessage: &pb2.Scalars{},
- inputText: `{
- "optBool": false,
- "optInt32": 0,
- "optInt64": 0,
- "optUint32": 0,
- "optUint64": 0,
- "optSint32": 0,
- "optSint64": 0,
- "optFixed32": 0,
- "optFixed64": 0,
- "optSfixed32": 0,
- "optSfixed64": 0,
- "optFloat": 0,
- "optDouble": 0,
- "optBytes": "",
- "optString": ""
-}`,
- wantMessage: &pb2.Scalars{
- OptBool: scalar.Bool(false),
- OptInt32: scalar.Int32(0),
- OptInt64: scalar.Int64(0),
- OptUint32: scalar.Uint32(0),
- OptUint64: scalar.Uint64(0),
- OptSint32: scalar.Int32(0),
- OptSint64: scalar.Int64(0),
- OptFixed32: scalar.Uint32(0),
- OptFixed64: scalar.Uint64(0),
- OptSfixed32: scalar.Int32(0),
- OptSfixed64: scalar.Int64(0),
- OptFloat: scalar.Float32(0),
- OptDouble: scalar.Float64(0),
- OptBytes: []byte{},
- OptString: scalar.String(""),
- },
- }, {
- desc: "proto3 scalars set to zero values",
- inputMessage: &pb3.Scalars{},
- inputText: `{
- "sBool": false,
- "sInt32": 0,
- "sInt64": 0,
- "sUint32": 0,
- "sUint64": 0,
- "sSint32": 0,
- "sSint64": 0,
- "sFixed32": 0,
- "sFixed64": 0,
- "sSfixed32": 0,
- "sSfixed64": 0,
- "sFloat": 0,
- "sDouble": 0,
- "sBytes": "",
- "sString": ""
-}`,
- wantMessage: &pb3.Scalars{},
- }, {
- desc: "proto2 optional scalars set to null",
- inputMessage: &pb2.Scalars{},
- inputText: `{
- "optBool": null,
- "optInt32": null,
- "optInt64": null,
- "optUint32": null,
- "optUint64": null,
- "optSint32": null,
- "optSint64": null,
- "optFixed32": null,
- "optFixed64": null,
- "optSfixed32": null,
- "optSfixed64": null,
- "optFloat": null,
- "optDouble": null,
- "optBytes": null,
- "optString": null
-}`,
- wantMessage: &pb2.Scalars{},
- }, {
- desc: "proto3 scalars set to null",
- inputMessage: &pb3.Scalars{},
- inputText: `{
- "sBool": null,
- "sInt32": null,
- "sInt64": null,
- "sUint32": null,
- "sUint64": null,
- "sSint32": null,
- "sSint64": null,
- "sFixed32": null,
- "sFixed64": null,
- "sSfixed32": null,
- "sSfixed64": null,
- "sFloat": null,
- "sDouble": null,
- "sBytes": null,
- "sString": null
-}`,
- wantMessage: &pb3.Scalars{},
- }, {
- desc: "boolean",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sBool": true}`,
- wantMessage: &pb3.Scalars{
- SBool: true,
- },
- }, {
- desc: "not boolean",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sBool": "true"}`,
- wantErr: true,
- }, {
- desc: "float and double",
- inputMessage: &pb3.Scalars{},
- inputText: `{
- "sFloat": 1.234,
- "sDouble": 5.678
-}`,
- wantMessage: &pb3.Scalars{
- SFloat: 1.234,
- SDouble: 5.678,
- },
- }, {
- desc: "float and double in string",
- inputMessage: &pb3.Scalars{},
- inputText: `{
- "sFloat": "1.234",
- "sDouble": "5.678"
-}`,
- wantMessage: &pb3.Scalars{
- SFloat: 1.234,
- SDouble: 5.678,
- },
- }, {
- desc: "float and double in E notation",
- inputMessage: &pb3.Scalars{},
- inputText: `{
- "sFloat": 12.34E-1,
- "sDouble": 5.678e4
-}`,
- wantMessage: &pb3.Scalars{
- SFloat: 1.234,
- SDouble: 56780,
- },
- }, {
- desc: "float and double in string E notation",
- inputMessage: &pb3.Scalars{},
- inputText: `{
- "sFloat": "12.34E-1",
- "sDouble": "5.678e4"
-}`,
- wantMessage: &pb3.Scalars{
- SFloat: 1.234,
- SDouble: 56780,
- },
- }, {
- desc: "float exceeds limit",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sFloat": 3.4e39}`,
- wantErr: true,
- }, {
- desc: "float in string exceeds limit",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sFloat": "-3.4e39"}`,
- wantErr: true,
- }, {
- desc: "double exceeds limit",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sFloat": -1.79e+309}`,
- wantErr: true,
- }, {
- desc: "double in string exceeds limit",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sFloat": "1.79e+309"}`,
- wantErr: true,
- }, {
- desc: "infinites",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sFloat": "Infinity", "sDouble": "-Infinity"}`,
- wantMessage: &pb3.Scalars{
- SFloat: float32(math.Inf(+1)),
- SDouble: math.Inf(-1),
- },
- }, {
- desc: "float string with leading space",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sFloat": " 1.234"}`,
- wantErr: true,
- }, {
- desc: "double string with trailing space",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sDouble": "5.678 "}`,
- wantErr: true,
- }, {
- desc: "not float",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sFloat": true}`,
- wantErr: true,
- }, {
- desc: "not double",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sDouble": "not a number"}`,
- wantErr: true,
- }, {
- desc: "integers",
- inputMessage: &pb3.Scalars{},
- inputText: `{
- "sInt32": 1234,
- "sInt64": -1234,
- "sUint32": 1e2,
- "sUint64": 100E-2,
- "sSint32": 1.0,
- "sSint64": -1.0,
- "sFixed32": 1.234e+5,
- "sFixed64": 1200E-2,
- "sSfixed32": -1.234e05,
- "sSfixed64": -1200e-02
-}`,
- wantMessage: &pb3.Scalars{
- SInt32: 1234,
- SInt64: -1234,
- SUint32: 100,
- SUint64: 1,
- SSint32: 1,
- SSint64: -1,
- SFixed32: 123400,
- SFixed64: 12,
- SSfixed32: -123400,
- SSfixed64: -12,
- },
- }, {
- desc: "integers in string",
- inputMessage: &pb3.Scalars{},
- inputText: `{
- "sInt32": "1234",
- "sInt64": "-1234",
- "sUint32": "1e2",
- "sUint64": "100E-2",
- "sSint32": "1.0",
- "sSint64": "-1.0",
- "sFixed32": "1.234e+5",
- "sFixed64": "1200E-2",
- "sSfixed32": "-1.234e05",
- "sSfixed64": "-1200e-02"
-}`,
- wantMessage: &pb3.Scalars{
- SInt32: 1234,
- SInt64: -1234,
- SUint32: 100,
- SUint64: 1,
- SSint32: 1,
- SSint64: -1,
- SFixed32: 123400,
- SFixed64: 12,
- SSfixed32: -123400,
- SSfixed64: -12,
- },
- }, {
- desc: "integers in escaped string",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sInt32": "\u0031\u0032"}`,
- wantMessage: &pb3.Scalars{
- SInt32: 12,
- },
- }, {
- desc: "integer string with leading space",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sInt32": " 1234"}`,
- wantErr: true,
- }, {
- desc: "integer string with trailing space",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sUint32": "1e2 "}`,
- wantErr: true,
- }, {
- desc: "number is not an integer",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sInt32": 1.001}`,
- wantErr: true,
- }, {
- desc: "32-bit int exceeds limit",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sInt32": 2e10}`,
- wantErr: true,
- }, {
- desc: "64-bit int exceeds limit",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sSfixed64": -9e19}`,
- wantErr: true,
- }, {
- desc: "not integer",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sInt32": "not a number"}`,
- wantErr: true,
- }, {
- desc: "not unsigned integer",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sUint32": "not a number"}`,
- wantErr: true,
- }, {
- desc: "number is not an unsigned integer",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sUint32": -1}`,
- wantErr: true,
- }, {
- desc: "string",
- inputMessage: &pb2.Scalars{},
- inputText: `{"optString": "è°·æŒ"}`,
- wantMessage: &pb2.Scalars{
- OptString: scalar.String("è°·æŒ"),
- },
- }, {
- desc: "string with invalid UTF-8",
- inputMessage: &pb3.Scalars{},
- inputText: "{\"sString\": \"\xff\"}",
- wantMessage: &pb3.Scalars{
- SString: "\xff",
- },
- wantErr: true,
- }, {
- desc: "not string",
- inputMessage: &pb2.Scalars{},
- inputText: `{"optString": 42}`,
- wantErr: true,
- }, {
- desc: "bytes",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sBytes": "aGVsbG8gd29ybGQ"}`,
- wantMessage: &pb3.Scalars{
- SBytes: []byte("hello world"),
- },
- }, {
- desc: "bytes padded",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sBytes": "aGVsbG8gd29ybGQ="}`,
- wantMessage: &pb3.Scalars{
- SBytes: []byte("hello world"),
- },
- }, {
- desc: "not bytes",
- inputMessage: &pb3.Scalars{},
- inputText: `{"sBytes": true}`,
- wantErr: true,
- }, {
- desc: "proto2 enum",
- inputMessage: &pb2.Enums{},
- inputText: `{
- "optEnum": "ONE",
- "optNestedEnum": "UNO"
-}`,
- wantMessage: &pb2.Enums{
- OptEnum: pb2.Enum_ONE.Enum(),
- OptNestedEnum: pb2.Enums_UNO.Enum(),
- },
- }, {
- desc: "proto3 enum",
- inputMessage: &pb3.Enums{},
- inputText: `{
- "sEnum": "ONE",
- "sNestedEnum": "DIEZ"
-}`,
- wantMessage: &pb3.Enums{
- SEnum: pb3.Enum_ONE,
- SNestedEnum: pb3.Enums_DIEZ,
- },
- }, {
- desc: "enum numeric value",
- inputMessage: &pb3.Enums{},
- inputText: `{
- "sEnum": 2,
- "sNestedEnum": 2
-}`,
- wantMessage: &pb3.Enums{
- SEnum: pb3.Enum_TWO,
- SNestedEnum: pb3.Enums_DOS,
- },
- }, {
- desc: "enum unnamed numeric value",
- inputMessage: &pb3.Enums{},
- inputText: `{
- "sEnum": 101,
- "sNestedEnum": -101
-}`,
- wantMessage: &pb3.Enums{
- SEnum: 101,
- SNestedEnum: -101,
- },
- }, {
- desc: "enum set to number string",
- inputMessage: &pb3.Enums{},
- inputText: `{
- "sEnum": "1"
-}`,
- wantErr: true,
- }, {
- desc: "enum set to invalid named",
- inputMessage: &pb3.Enums{},
- inputText: `{
- "sEnum": "UNNAMED"
-}`,
- wantErr: true,
- }, {
- desc: "enum set to not enum",
- inputMessage: &pb3.Enums{},
- inputText: `{
- "sEnum": true
-}`,
- wantErr: true,
- }, {
- desc: "enum set to JSON null",
- inputMessage: &pb3.Enums{},
- inputText: `{
- "sEnum": null
-}`,
- wantMessage: &pb3.Enums{},
- }, {
- desc: "proto name",
- inputMessage: &pb3.JSONNames{},
- inputText: `{
- "s_string": "proto name used"
-}`,
- wantMessage: &pb3.JSONNames{
- SString: "proto name used",
- },
- }, {
- desc: "json_name",
- inputMessage: &pb3.JSONNames{},
- inputText: `{
- "foo_bar": "json_name used"
-}`,
- wantMessage: &pb3.JSONNames{
- SString: "json_name used",
- },
- }, {
- desc: "camelCase name",
- inputMessage: &pb3.JSONNames{},
- inputText: `{
- "sString": "camelcase used"
-}`,
- wantErr: true,
- }, {
- desc: "proto name and json_name",
- inputMessage: &pb3.JSONNames{},
- inputText: `{
- "foo_bar": "json_name used",
- "s_string": "proto name used"
-}`,
- wantErr: true,
- }, {
- desc: "duplicate field names",
- inputMessage: &pb3.JSONNames{},
- inputText: `{
- "foo_bar": "one",
- "foo_bar": "two",
-}`,
- wantErr: true,
- }, {
- desc: "null message",
- inputMessage: &pb2.Nests{},
- inputText: "null",
- wantErr: true,
- }, {
- desc: "proto2 nested message not set",
- inputMessage: &pb2.Nests{},
- inputText: "{}",
- wantMessage: &pb2.Nests{},
- }, {
- desc: "proto2 nested message set to null",
- inputMessage: &pb2.Nests{},
- inputText: `{
- "optNested": null,
- "optgroup": null
-}`,
- wantMessage: &pb2.Nests{},
- }, {
- desc: "proto2 nested message set to empty",
- inputMessage: &pb2.Nests{},
- inputText: `{
- "optNested": {},
- "optgroup": {}
-}`,
- wantMessage: &pb2.Nests{
- OptNested: &pb2.Nested{},
- Optgroup: &pb2.Nests_OptGroup{},
- },
- }, {
- desc: "proto2 nested messages",
- inputMessage: &pb2.Nests{},
- inputText: `{
- "optNested": {
- "optString": "nested message",
- "optNested": {
- "optString": "another nested message"
- }
- }
-}`,
- wantMessage: &pb2.Nests{
- OptNested: &pb2.Nested{
- OptString: scalar.String("nested message"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("another nested message"),
- },
- },
- },
- }, {
- desc: "proto2 groups",
- inputMessage: &pb2.Nests{},
- inputText: `{
- "optgroup": {
- "optString": "inside a group",
- "optNested": {
- "optString": "nested message inside a group"
- },
- "optnestedgroup": {
- "optFixed32": 47
- }
- }
-}`,
- wantMessage: &pb2.Nests{
- Optgroup: &pb2.Nests_OptGroup{
- OptString: scalar.String("inside a group"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("nested message inside a group"),
- },
- Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{
- OptFixed32: scalar.Uint32(47),
- },
- },
- },
- }, {
- desc: "proto3 nested message not set",
- inputMessage: &pb3.Nests{},
- inputText: "{}",
- wantMessage: &pb3.Nests{},
- }, {
- desc: "proto3 nested message set to null",
- inputMessage: &pb3.Nests{},
- inputText: `{"sNested": null}`,
- wantMessage: &pb3.Nests{},
- }, {
- desc: "proto3 nested message set to empty",
- inputMessage: &pb3.Nests{},
- inputText: `{"sNested": {}}`,
- wantMessage: &pb3.Nests{
- SNested: &pb3.Nested{},
- },
- }, {
- desc: "proto3 nested message",
- inputMessage: &pb3.Nests{},
- inputText: `{
- "sNested": {
- "sString": "nested message",
- "sNested": {
- "sString": "another nested message"
- }
- }
-}`,
- wantMessage: &pb3.Nests{
- SNested: &pb3.Nested{
- SString: "nested message",
- SNested: &pb3.Nested{
- SString: "another nested message",
- },
- },
- },
- }, {
- desc: "message set to non-message",
- inputMessage: &pb3.Nests{},
- inputText: `"not valid"`,
- wantErr: true,
- }, {
- desc: "nested message set to non-message",
- inputMessage: &pb3.Nests{},
- inputText: `{"sNested": true}`,
- wantErr: true,
- }, {
- desc: "oneof not set",
- inputMessage: &pb3.Oneofs{},
- inputText: "{}",
- wantMessage: &pb3.Oneofs{},
- }, {
- desc: "oneof set to empty string",
- inputMessage: &pb3.Oneofs{},
- inputText: `{"oneofString": ""}`,
- wantMessage: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofString{},
- },
- }, {
- desc: "oneof set to string",
- inputMessage: &pb3.Oneofs{},
- inputText: `{"oneofString": "hello"}`,
- wantMessage: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofString{
- OneofString: "hello",
- },
- },
- }, {
- desc: "oneof set to enum",
- inputMessage: &pb3.Oneofs{},
- inputText: `{"oneofEnum": "ZERO"}`,
- wantMessage: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofEnum{
- OneofEnum: pb3.Enum_ZERO,
- },
- },
- }, {
- desc: "oneof set to empty message",
- inputMessage: &pb3.Oneofs{},
- inputText: `{"oneofNested": {}}`,
- wantMessage: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofNested{
- OneofNested: &pb3.Nested{},
- },
- },
- }, {
- desc: "oneof set to message",
- inputMessage: &pb3.Oneofs{},
- inputText: `{
- "oneofNested": {
- "sString": "nested message"
- }
-}`,
- wantMessage: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofNested{
- OneofNested: &pb3.Nested{
- SString: "nested message",
- },
- },
- },
- }, {
- desc: "oneof set to more than one field",
- inputMessage: &pb3.Oneofs{},
- inputText: `{
- "oneofEnum": "ZERO",
- "oneofString": "hello"
-}`,
- wantErr: true,
- }, {
- desc: "oneof set to null and value",
- inputMessage: &pb3.Oneofs{},
- inputText: `{
- "oneofEnum": "ZERO",
- "oneofString": null
-}`,
- wantMessage: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofEnum{
- OneofEnum: pb3.Enum_ZERO,
- },
- },
- }, {
- desc: "repeated null fields",
- inputMessage: &pb2.Repeats{},
- inputText: `{
- "rptString": null,
- "rptInt32" : null,
- "rptFloat" : null,
- "rptBytes" : null
-}`,
- wantMessage: &pb2.Repeats{},
- }, {
- desc: "repeated scalars",
- inputMessage: &pb2.Repeats{},
- inputText: `{
- "rptString": ["hello", "world"],
- "rptInt32" : [-1, 0, 1],
- "rptBool" : [false, true]
-}`,
- wantMessage: &pb2.Repeats{
- RptString: []string{"hello", "world"},
- RptInt32: []int32{-1, 0, 1},
- RptBool: []bool{false, true},
- },
- }, {
- desc: "repeated enums",
- inputMessage: &pb2.Enums{},
- inputText: `{
- "rptEnum" : ["TEN", 1, 42],
- "rptNestedEnum": ["DOS", 2, -47]
-}`,
- wantMessage: &pb2.Enums{
- RptEnum: []pb2.Enum{pb2.Enum_TEN, pb2.Enum_ONE, 42},
- RptNestedEnum: []pb2.Enums_NestedEnum{pb2.Enums_DOS, pb2.Enums_DOS, -47},
- },
- }, {
- desc: "repeated messages",
- inputMessage: &pb2.Nests{},
- inputText: `{
- "rptNested": [
- {
- "optString": "repeat nested one"
- },
- {
- "optString": "repeat nested two",
- "optNested": {
- "optString": "inside repeat nested two"
- }
- },
- {}
- ]
-}`,
- wantMessage: &pb2.Nests{
- RptNested: []*pb2.Nested{
- {
- OptString: scalar.String("repeat nested one"),
- },
- {
- OptString: scalar.String("repeat nested two"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("inside repeat nested two"),
- },
- },
- {},
- },
- },
- }, {
- desc: "repeated groups",
- inputMessage: &pb2.Nests{},
- inputText: `{
- "rptgroup": [
- {
- "rptString": ["hello", "world"]
- },
- {}
- ]
-}
-`,
- wantMessage: &pb2.Nests{
- Rptgroup: []*pb2.Nests_RptGroup{
- {
- RptString: []string{"hello", "world"},
- },
- {},
- },
- },
- }, {
- desc: "repeated string contains invalid UTF8",
- inputMessage: &pb2.Repeats{},
- inputText: `{"rptString": ["` + "abc\xff" + `"]}`,
- wantMessage: &pb2.Repeats{
- RptString: []string{"abc\xff"},
- },
- wantErr: true,
- }, {
- desc: "repeated messages contain invalid UTF8",
- inputMessage: &pb2.Nests{},
- inputText: `{"rptNested": [{"optString": "` + "abc\xff" + `"}]}`,
- wantMessage: &pb2.Nests{
- RptNested: []*pb2.Nested{{OptString: scalar.String("abc\xff")}},
- },
- wantErr: true,
- }, {
- desc: "repeated scalars contain invalid type",
- inputMessage: &pb2.Repeats{},
- inputText: `{"rptString": ["hello", null, "world"]}`,
- wantErr: true,
- }, {
- desc: "repeated messages contain invalid type",
- inputMessage: &pb2.Nests{},
- inputText: `{"rptNested": [{}, null]}`,
- wantErr: true,
- }, {
- desc: "map fields 1",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "int32ToStr": {
- "-101": "-101",
- "0" : "zero",
- "255" : "0xff"
- },
- "boolToUint32": {
- "false": 101,
- "true" : "42"
- }
-}`,
- wantMessage: &pb3.Maps{
- Int32ToStr: map[int32]string{
- -101: "-101",
- 0xff: "0xff",
- 0: "zero",
- },
- BoolToUint32: map[bool]uint32{
- true: 42,
- false: 101,
- },
- },
- }, {
- desc: "map fields 2",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "uint64ToEnum": {
- "1" : "ONE",
- "2" : 2,
- "10": 101
- }
-}`,
- wantMessage: &pb3.Maps{
- Uint64ToEnum: map[uint64]pb3.Enum{
- 1: pb3.Enum_ONE,
- 2: pb3.Enum_TWO,
- 10: 101,
- },
- },
- }, {
- desc: "map fields 3",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "strToNested": {
- "nested_one": {
- "sString": "nested in a map"
- },
- "nested_two": {}
- }
-}`,
- wantMessage: &pb3.Maps{
- StrToNested: map[string]*pb3.Nested{
- "nested_one": {
- SString: "nested in a map",
- },
- "nested_two": {},
- },
- },
- }, {
- desc: "map fields 4",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "strToOneofs": {
- "nested": {
- "oneofNested": {
- "sString": "nested oneof in map field value"
- }
- },
- "string": {
- "oneofString": "hello"
- }
- }
-}`,
- wantMessage: &pb3.Maps{
- StrToOneofs: map[string]*pb3.Oneofs{
- "string": {
- Union: &pb3.Oneofs_OneofString{
- OneofString: "hello",
- },
- },
- "nested": {
- Union: &pb3.Oneofs_OneofNested{
- OneofNested: &pb3.Nested{
- SString: "nested oneof in map field value",
- },
- },
- },
- },
- },
- }, {
- desc: "map contains duplicate keys",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "int32ToStr": {
- "0": "cero",
- "0": "zero"
- }
-}
-`,
- wantErr: true,
- }, {
- desc: "map key empty string",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "strToNested": {
- "": {}
- }
-}`,
- wantMessage: &pb3.Maps{
- StrToNested: map[string]*pb3.Nested{
- "": {},
- },
- },
- }, {
- desc: "map contains invalid key 1",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "int32ToStr": {
- "invalid": "cero"
-}`,
- wantErr: true,
- }, {
- desc: "map contains invalid key 2",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "int32ToStr": {
- "1.02": "float"
-}`,
- wantErr: true,
- }, {
- desc: "map contains invalid key 3",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "int32ToStr": {
- "2147483648": "exceeds 32-bit integer max limit"
-}`,
- wantErr: true,
- }, {
- desc: "map contains invalid key 4",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "uint64ToEnum": {
- "-1": 0
- }
-}`,
- wantErr: true,
- }, {
- desc: "map contains invalid value",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "int32ToStr": {
- "101": true
-}`,
- wantErr: true,
- }, {
- desc: "map contains null for scalar value",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "int32ToStr": {
- "101": null
-}`,
- wantErr: true,
- }, {
- desc: "map contains null for message value",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "strToNested": {
- "hello": null
- }
-}`,
- wantErr: true,
- }, {
- desc: "map contains contains message value with invalid UTF8",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "strToNested": {
- "hello": {
- "sString": "` + "abc\xff" + `"
- }
- }
-}`,
- wantMessage: &pb3.Maps{
- StrToNested: map[string]*pb3.Nested{
- "hello": {SString: "abc\xff"},
- },
- },
- wantErr: true,
- }, {
- desc: "map key contains invalid UTF8",
- inputMessage: &pb3.Maps{},
- inputText: `{
- "strToNested": {
- "` + "abc\xff" + `": {}
- }
-}`,
- wantMessage: &pb3.Maps{
- StrToNested: map[string]*pb3.Nested{
- "abc\xff": {},
- },
- },
- wantErr: true,
- }, {
- desc: "required fields not set",
- inputMessage: &pb2.Requireds{},
- wantErr: true,
- }, {
- desc: "required field set",
- inputMessage: &pb2.PartialRequired{},
- inputText: `{
- "reqString": "this is required"
-}`,
- wantMessage: &pb2.PartialRequired{
- ReqString: scalar.String("this is required"),
- },
- }, {
- desc: "required fields partially set",
- inputMessage: &pb2.Requireds{},
- inputText: `{
- "reqBool": false,
- "reqSfixed64": 42,
- "reqString": "hello",
- "reqEnum": "ONE"
-}`,
- wantMessage: &pb2.Requireds{
- ReqBool: scalar.Bool(false),
- ReqSfixed64: scalar.Int64(42),
- ReqString: scalar.String("hello"),
- ReqEnum: pb2.Enum_ONE.Enum(),
- },
- wantErr: true,
- }, {
- desc: "required fields partially set with AllowPartial",
- umo: jsonpb.UnmarshalOptions{AllowPartial: true},
- inputMessage: &pb2.Requireds{},
- inputText: `{
- "reqBool": false,
- "reqSfixed64": 42,
- "reqString": "hello",
- "reqEnum": "ONE"
-}`,
- wantMessage: &pb2.Requireds{
- ReqBool: scalar.Bool(false),
- ReqSfixed64: scalar.Int64(42),
- ReqString: scalar.String("hello"),
- ReqEnum: pb2.Enum_ONE.Enum(),
- },
- }, {
- desc: "required fields all set",
- inputMessage: &pb2.Requireds{},
- inputText: `{
- "reqBool": false,
- "reqSfixed64": 42,
- "reqDouble": 1.23,
- "reqString": "hello",
- "reqEnum": "ONE",
- "reqNested": {}
-}`,
- wantMessage: &pb2.Requireds{
- ReqBool: scalar.Bool(false),
- ReqSfixed64: scalar.Int64(42),
- ReqDouble: scalar.Float64(1.23),
- ReqString: scalar.String("hello"),
- ReqEnum: pb2.Enum_ONE.Enum(),
- ReqNested: &pb2.Nested{},
- },
- }, {
- desc: "indirect required field",
- inputMessage: &pb2.IndirectRequired{},
- inputText: `{
- "optNested": {}
-}`,
- wantMessage: &pb2.IndirectRequired{
- OptNested: &pb2.NestedWithRequired{},
- },
- wantErr: true,
- }, {
- desc: "indirect required field with AllowPartial",
- umo: jsonpb.UnmarshalOptions{AllowPartial: true},
- inputMessage: &pb2.IndirectRequired{},
- inputText: `{
- "optNested": {}
-}`,
- wantMessage: &pb2.IndirectRequired{
- OptNested: &pb2.NestedWithRequired{},
- },
- }, {
- desc: "indirect required field in repeated",
- inputMessage: &pb2.IndirectRequired{},
- inputText: `{
- "rptNested": [
- {"reqString": "one"},
- {}
- ]
-}`,
- wantMessage: &pb2.IndirectRequired{
- RptNested: []*pb2.NestedWithRequired{
- {
- ReqString: scalar.String("one"),
- },
- {},
- },
- },
- wantErr: true,
- }, {
- desc: "indirect required field in repeated with AllowPartial",
- umo: jsonpb.UnmarshalOptions{AllowPartial: true},
- inputMessage: &pb2.IndirectRequired{},
- inputText: `{
- "rptNested": [
- {"reqString": "one"},
- {}
- ]
-}`,
- wantMessage: &pb2.IndirectRequired{
- RptNested: []*pb2.NestedWithRequired{
- {
- ReqString: scalar.String("one"),
- },
- {},
- },
- },
- }, {
- desc: "indirect required field in map",
- inputMessage: &pb2.IndirectRequired{},
- inputText: `{
- "strToNested": {
- "missing": {},
- "contains": {
- "reqString": "here"
- }
- }
-}`,
- wantMessage: &pb2.IndirectRequired{
- StrToNested: map[string]*pb2.NestedWithRequired{
- "missing": &pb2.NestedWithRequired{},
- "contains": &pb2.NestedWithRequired{
- ReqString: scalar.String("here"),
- },
- },
- },
- wantErr: true,
- }, {
- desc: "indirect required field in map with AllowPartial",
- umo: jsonpb.UnmarshalOptions{AllowPartial: true},
- inputMessage: &pb2.IndirectRequired{},
- inputText: `{
- "strToNested": {
- "missing": {},
- "contains": {
- "reqString": "here"
- }
- }
-}`,
- wantMessage: &pb2.IndirectRequired{
- StrToNested: map[string]*pb2.NestedWithRequired{
- "missing": &pb2.NestedWithRequired{},
- "contains": &pb2.NestedWithRequired{
- ReqString: scalar.String("here"),
- },
- },
- },
- }, {
- desc: "indirect required field in oneof",
- inputMessage: &pb2.IndirectRequired{},
- inputText: `{
- "oneofNested": {}
-}`,
- wantMessage: &pb2.IndirectRequired{
- Union: &pb2.IndirectRequired_OneofNested{
- OneofNested: &pb2.NestedWithRequired{},
- },
- },
- wantErr: true,
- }, {
- desc: "indirect required field in oneof with AllowPartial",
- umo: jsonpb.UnmarshalOptions{AllowPartial: true},
- inputMessage: &pb2.IndirectRequired{},
- inputText: `{
- "oneofNested": {}
-}`,
- wantMessage: &pb2.IndirectRequired{
- Union: &pb2.IndirectRequired_OneofNested{
- OneofNested: &pb2.NestedWithRequired{},
- },
- },
- }, {
- desc: "extensions of non-repeated fields",
- inputMessage: &pb2.Extensions{},
- inputText: `{
- "optString": "non-extension field",
- "optBool": true,
- "optInt32": 42,
- "[pb2.opt_ext_bool]": true,
- "[pb2.opt_ext_nested]": {
- "optString": "nested in an extension",
- "optNested": {
- "optString": "another nested in an extension"
- }
- },
- "[pb2.opt_ext_string]": "extension field",
- "[pb2.opt_ext_enum]": "TEN"
-}`,
- wantMessage: func() proto.Message {
- m := &pb2.Extensions{
- OptString: scalar.String("non-extension field"),
- OptBool: scalar.Bool(true),
- OptInt32: scalar.Int32(42),
- }
- setExtension(m, pb2.E_OptExtBool, true)
- setExtension(m, pb2.E_OptExtString, "extension field")
- setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN)
- setExtension(m, pb2.E_OptExtNested, &pb2.Nested{
- OptString: scalar.String("nested in an extension"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("another nested in an extension"),
- },
- })
- return m
- }(),
- }, {
- desc: "extensions of repeated fields",
- inputMessage: &pb2.Extensions{},
- inputText: `{
- "[pb2.rpt_ext_enum]": ["TEN", 101, "ONE"],
- "[pb2.rpt_ext_fixed32]": [42, 47],
- "[pb2.rpt_ext_nested]": [
- {"optString": "one"},
- {"optString": "two"},
- {"optString": "three"}
- ]
-}`,
- wantMessage: func() proto.Message {
- m := &pb2.Extensions{}
- setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
- setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
- setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
- &pb2.Nested{OptString: scalar.String("one")},
- &pb2.Nested{OptString: scalar.String("two")},
- &pb2.Nested{OptString: scalar.String("three")},
- })
- return m
- }(),
- }, {
- desc: "extensions of non-repeated fields in another message",
- inputMessage: &pb2.Extensions{},
- inputText: `{
- "[pb2.ExtensionsContainer.opt_ext_bool]": true,
- "[pb2.ExtensionsContainer.opt_ext_enum]": "TEN",
- "[pb2.ExtensionsContainer.opt_ext_nested]": {
- "optString": "nested in an extension",
- "optNested": {
- "optString": "another nested in an extension"
- }
- },
- "[pb2.ExtensionsContainer.opt_ext_string]": "extension field"
-}`,
- wantMessage: func() proto.Message {
- m := &pb2.Extensions{}
- setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
- setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
- setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
- setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
- OptString: scalar.String("nested in an extension"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("another nested in an extension"),
- },
- })
- return m
- }(),
- }, {
- desc: "extensions of repeated fields in another message",
- inputMessage: &pb2.Extensions{},
- inputText: `{
- "optString": "non-extension field",
- "optBool": true,
- "optInt32": 42,
- "[pb2.ExtensionsContainer.rpt_ext_nested]": [
- {"optString": "one"},
- {"optString": "two"},
- {"optString": "three"}
- ],
- "[pb2.ExtensionsContainer.rpt_ext_enum]": ["TEN", 101, "ONE"],
- "[pb2.ExtensionsContainer.rpt_ext_string]": ["hello", "world"]
-}`,
- wantMessage: func() proto.Message {
- m := &pb2.Extensions{
- OptString: scalar.String("non-extension field"),
- OptBool: scalar.Bool(true),
- OptInt32: scalar.Int32(42),
- }
- setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
- setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
- setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
- &pb2.Nested{OptString: scalar.String("one")},
- &pb2.Nested{OptString: scalar.String("two")},
- &pb2.Nested{OptString: scalar.String("three")},
- })
- return m
- }(),
- }, {
- desc: "invalid extension field name",
- inputMessage: &pb2.Extensions{},
- inputText: `{ "[pb2.invalid_message_field]": true }`,
- wantErr: true,
- }, {
- desc: "MessageSet",
- inputMessage: &pb2.MessageSet{},
- inputText: `{
- "[pb2.MessageSetExtension]": {
- "optString": "a messageset extension"
- },
- "[pb2.MessageSetExtension.ext_nested]": {
- "optString": "just a regular extension"
- },
- "[pb2.MessageSetExtension.not_message_set_extension]": {
- "optString": "not a messageset extension"
- }
-}`,
- wantMessage: func() proto.Message {
- m := &pb2.MessageSet{}
- setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
- OptString: scalar.String("a messageset extension"),
- })
- setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
- OptString: scalar.String("not a messageset extension"),
- })
- setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
- OptString: scalar.String("just a regular extension"),
- })
- return m
- }(),
- }, {
- desc: "extension field set to null",
- inputMessage: &pb2.Extensions{},
- inputText: `{
- "[pb2.ExtensionsContainer.opt_ext_bool]": null,
- "[pb2.ExtensionsContainer.opt_ext_nested]": null
-}`,
- wantMessage: func() proto.Message {
- m := &pb2.Extensions{}
- setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, nil)
- setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, nil)
- return m
- }(),
- }, {
- desc: "extensions of repeated field contains null",
- inputMessage: &pb2.Extensions{},
- inputText: `{
- "[pb2.ExtensionsContainer.rpt_ext_nested]": [
- {"optString": "one"},
- null,
- {"optString": "three"}
- ],
-}`,
- wantErr: true,
- }, {
- desc: "not real MessageSet 1",
- inputMessage: &pb2.FakeMessageSet{},
- inputText: `{
- "[pb2.FakeMessageSetExtension.message_set_extension]": {
- "optString": "not a messageset extension"
- }
-}`,
- wantMessage: func() proto.Message {
- m := &pb2.FakeMessageSet{}
- setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
- OptString: scalar.String("not a messageset extension"),
- })
- return m
- }(),
- }, {
- desc: "not real MessageSet 2",
- inputMessage: &pb2.FakeMessageSet{},
- inputText: `{
- "[pb2.FakeMessageSetExtension]": {
- "optString": "not a messageset extension"
- }
-}`,
- wantErr: true,
- }, {
- desc: "not real MessageSet 3",
- inputMessage: &pb2.MessageSet{},
- inputText: `{
- "[pb2.message_set_extension]": {
- "optString": "another not a messageset extension"
- }
-}`,
- wantMessage: func() proto.Message {
- m := &pb2.MessageSet{}
- setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
- OptString: scalar.String("another not a messageset extension"),
- })
- return m
- }(),
- }, {
- desc: "Empty",
- inputMessage: &knownpb.Empty{},
- inputText: `{}`,
- wantMessage: &knownpb.Empty{},
- }, {
- desc: "Empty contains unknown",
- inputMessage: &knownpb.Empty{},
- inputText: `{"unknown": null}`,
- wantErr: true,
- }, {
- desc: "BoolValue false",
- inputMessage: &knownpb.BoolValue{},
- inputText: `false`,
- wantMessage: &knownpb.BoolValue{},
- }, {
- desc: "BoolValue true",
- inputMessage: &knownpb.BoolValue{},
- inputText: `true`,
- wantMessage: &knownpb.BoolValue{Value: true},
- }, {
- desc: "BoolValue invalid value",
- inputMessage: &knownpb.BoolValue{},
- inputText: `{}`,
- wantErr: true,
- }, {
- desc: "Int32Value",
- inputMessage: &knownpb.Int32Value{},
- inputText: `42`,
- wantMessage: &knownpb.Int32Value{Value: 42},
- }, {
- desc: "Int32Value in JSON string",
- inputMessage: &knownpb.Int32Value{},
- inputText: `"1.23e3"`,
- wantMessage: &knownpb.Int32Value{Value: 1230},
- }, {
- desc: "Int64Value",
- inputMessage: &knownpb.Int64Value{},
- inputText: `"42"`,
- wantMessage: &knownpb.Int64Value{Value: 42},
- }, {
- desc: "UInt32Value",
- inputMessage: &knownpb.UInt32Value{},
- inputText: `42`,
- wantMessage: &knownpb.UInt32Value{Value: 42},
- }, {
- desc: "UInt64Value",
- inputMessage: &knownpb.UInt64Value{},
- inputText: `"42"`,
- wantMessage: &knownpb.UInt64Value{Value: 42},
- }, {
- desc: "FloatValue",
- inputMessage: &knownpb.FloatValue{},
- inputText: `1.02`,
- wantMessage: &knownpb.FloatValue{Value: 1.02},
- }, {
- desc: "FloatValue exceeds max limit",
- inputMessage: &knownpb.FloatValue{},
- inputText: `1.23+40`,
- wantErr: true,
- }, {
- desc: "FloatValue Infinity",
- inputMessage: &knownpb.FloatValue{},
- inputText: `"-Infinity"`,
- wantMessage: &knownpb.FloatValue{Value: float32(math.Inf(-1))},
- }, {
- desc: "DoubleValue",
- inputMessage: &knownpb.DoubleValue{},
- inputText: `1.02`,
- wantMessage: &knownpb.DoubleValue{Value: 1.02},
- }, {
- desc: "DoubleValue Infinity",
- inputMessage: &knownpb.DoubleValue{},
- inputText: `"Infinity"`,
- wantMessage: &knownpb.DoubleValue{Value: math.Inf(+1)},
- }, {
- desc: "StringValue empty",
- inputMessage: &knownpb.StringValue{},
- inputText: `""`,
- wantMessage: &knownpb.StringValue{},
- }, {
- desc: "StringValue",
- inputMessage: &knownpb.StringValue{},
- inputText: `"è°·æŒ"`,
- wantMessage: &knownpb.StringValue{Value: "è°·æŒ"},
- }, {
- desc: "StringValue with invalid UTF8 error",
- inputMessage: &knownpb.StringValue{},
- inputText: "\"abc\xff\"",
- wantMessage: &knownpb.StringValue{Value: "abc\xff"},
- wantErr: true,
- }, {
- desc: "StringValue field with invalid UTF8 error",
- inputMessage: &pb2.KnownTypes{},
- inputText: "{\n \"optString\": \"abc\xff\"\n}",
- wantMessage: &pb2.KnownTypes{
- OptString: &knownpb.StringValue{Value: "abc\xff"},
- },
- wantErr: true,
- }, {
- desc: "NullValue field with JSON null",
- inputMessage: &pb2.KnownTypes{},
- inputText: `{
- "optNull": null
-}`,
- wantMessage: &pb2.KnownTypes{OptNull: new(knownpb.NullValue)},
- }, {
- desc: "NullValue field with string",
- inputMessage: &pb2.KnownTypes{},
- inputText: `{
- "optNull": "NULL_VALUE"
-}`,
- wantMessage: &pb2.KnownTypes{OptNull: new(knownpb.NullValue)},
- }, {
- desc: "BytesValue",
- inputMessage: &knownpb.BytesValue{},
- inputText: `"aGVsbG8="`,
- wantMessage: &knownpb.BytesValue{Value: []byte("hello")},
- }, {
- desc: "Value null",
- inputMessage: &knownpb.Value{},
- inputText: `null`,
- wantMessage: &knownpb.Value{Kind: &knownpb.Value_NullValue{}},
- }, {
- desc: "Value field null",
- inputMessage: &pb2.KnownTypes{},
- inputText: `{
- "optValue": null
-}`,
- wantMessage: &pb2.KnownTypes{
- OptValue: &knownpb.Value{Kind: &knownpb.Value_NullValue{}},
- },
- }, {
- desc: "Value bool",
- inputMessage: &knownpb.Value{},
- inputText: `false`,
- wantMessage: &knownpb.Value{Kind: &knownpb.Value_BoolValue{}},
- }, {
- desc: "Value field bool",
- inputMessage: &pb2.KnownTypes{},
- inputText: `{
- "optValue": true
-}`,
- wantMessage: &pb2.KnownTypes{
- OptValue: &knownpb.Value{Kind: &knownpb.Value_BoolValue{true}},
- },
- }, {
- desc: "Value number",
- inputMessage: &knownpb.Value{},
- inputText: `1.02`,
- wantMessage: &knownpb.Value{Kind: &knownpb.Value_NumberValue{1.02}},
- }, {
- desc: "Value field number",
- inputMessage: &pb2.KnownTypes{},
- inputText: `{
- "optValue": 1.02
-}`,
- wantMessage: &pb2.KnownTypes{
- OptValue: &knownpb.Value{Kind: &knownpb.Value_NumberValue{1.02}},
- },
- }, {
- desc: "Value string",
- inputMessage: &knownpb.Value{},
- inputText: `"hello"`,
- wantMessage: &knownpb.Value{Kind: &knownpb.Value_StringValue{"hello"}},
- }, {
- desc: "Value string with invalid UTF8",
- inputMessage: &knownpb.Value{},
- inputText: "\"\xff\"",
- wantMessage: &knownpb.Value{Kind: &knownpb.Value_StringValue{"\xff"}},
- wantErr: true,
- }, {
- desc: "Value field string",
- inputMessage: &pb2.KnownTypes{},
- inputText: `{
- "optValue": "NaN"
-}`,
- wantMessage: &pb2.KnownTypes{
- OptValue: &knownpb.Value{Kind: &knownpb.Value_StringValue{"NaN"}},
- },
- }, {
- desc: "Value field string with invalid UTF8",
- inputMessage: &pb2.KnownTypes{},
- inputText: `{
- "optValue": "` + "\xff" + `"
-}`,
- wantMessage: &pb2.KnownTypes{
- OptValue: &knownpb.Value{Kind: &knownpb.Value_StringValue{"\xff"}},
- },
- wantErr: true,
- }, {
- desc: "Value empty struct",
- inputMessage: &knownpb.Value{},
- inputText: `{}`,
- wantMessage: &knownpb.Value{
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{Fields: map[string]*knownpb.Value{}},
- },
- },
- }, {
- desc: "Value struct",
- inputMessage: &knownpb.Value{},
- inputText: `{
- "string": "hello",
- "number": 123,
- "null": null,
- "bool": false,
- "struct": {
- "string": "world"
- },
- "list": []
-}`,
- wantMessage: &knownpb.Value{
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"hello"}},
- "number": {Kind: &knownpb.Value_NumberValue{123}},
- "null": {Kind: &knownpb.Value_NullValue{}},
- "bool": {Kind: &knownpb.Value_BoolValue{false}},
- "struct": {
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"world"}},
- },
- },
- },
- },
- "list": {
- Kind: &knownpb.Value_ListValue{&knownpb.ListValue{}},
- },
- },
- },
- },
- },
- }, {
- desc: "Value struct with invalid UTF8 string",
- inputMessage: &knownpb.Value{},
- inputText: "{\"string\": \"abc\xff\"}",
- wantMessage: &knownpb.Value{
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"abc\xff"}},
- },
- },
- },
- },
- wantErr: true,
- }, {
- desc: "Value field struct",
- inputMessage: &pb2.KnownTypes{},
- inputText: `{
- "optValue": {
- "string": "hello"
- }
-}`,
- wantMessage: &pb2.KnownTypes{
- OptValue: &knownpb.Value{
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"hello"}},
- },
- },
- },
- },
- },
- }, {
- desc: "Value empty list",
- inputMessage: &knownpb.Value{},
- inputText: `[]`,
- wantMessage: &knownpb.Value{
- Kind: &knownpb.Value_ListValue{
- &knownpb.ListValue{Values: []*knownpb.Value{}},
- },
- },
- }, {
- desc: "Value list",
- inputMessage: &knownpb.Value{},
- inputText: `[
- "string",
- 123,
- null,
- true,
- {},
- [
- "string",
- 1.23,
- null,
- false
- ]
-]`,
- wantMessage: &knownpb.Value{
- Kind: &knownpb.Value_ListValue{
- &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_StringValue{"string"}},
- {Kind: &knownpb.Value_NumberValue{123}},
- {Kind: &knownpb.Value_NullValue{}},
- {Kind: &knownpb.Value_BoolValue{true}},
- {Kind: &knownpb.Value_StructValue{&knownpb.Struct{}}},
- {
- Kind: &knownpb.Value_ListValue{
- &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_StringValue{"string"}},
- {Kind: &knownpb.Value_NumberValue{1.23}},
- {Kind: &knownpb.Value_NullValue{}},
- {Kind: &knownpb.Value_BoolValue{false}},
- },
- },
- },
- },
- },
- },
- },
- },
- }, {
- desc: "Value list with invalid UTF8 string",
- inputMessage: &knownpb.Value{},
- inputText: "[\"abc\xff\"]",
- wantMessage: &knownpb.Value{
- Kind: &knownpb.Value_ListValue{
- &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_StringValue{"abc\xff"}},
- },
- },
- },
- },
- wantErr: true,
- }, {
- desc: "Value field list with invalid UTF8 string",
- inputMessage: &pb2.KnownTypes{},
- inputText: `{
- "optValue": [ "` + "abc\xff" + `"]
-}`,
- wantMessage: &pb2.KnownTypes{
- OptValue: &knownpb.Value{
- Kind: &knownpb.Value_ListValue{
- &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_StringValue{"abc\xff"}},
- },
- },
- },
- },
- },
- wantErr: true,
- }, {
- desc: "Duration empty string",
- inputMessage: &knownpb.Duration{},
- inputText: `""`,
- wantErr: true,
- }, {
- desc: "Duration with secs",
- inputMessage: &knownpb.Duration{},
- inputText: `"3s"`,
- wantMessage: &knownpb.Duration{Seconds: 3},
- }, {
- desc: "Duration with escaped unicode",
- inputMessage: &knownpb.Duration{},
- inputText: `"\u0033s"`,
- wantMessage: &knownpb.Duration{Seconds: 3},
- }, {
- desc: "Duration with -secs",
- inputMessage: &knownpb.Duration{},
- inputText: `"-3s"`,
- wantMessage: &knownpb.Duration{Seconds: -3},
- }, {
- desc: "Duration with plus sign",
- inputMessage: &knownpb.Duration{},
- inputText: `"+3s"`,
- wantMessage: &knownpb.Duration{Seconds: 3},
- }, {
- desc: "Duration with nanos",
- inputMessage: &knownpb.Duration{},
- inputText: `"0.001s"`,
- wantMessage: &knownpb.Duration{Nanos: 1e6},
- }, {
- desc: "Duration with -nanos",
- inputMessage: &knownpb.Duration{},
- inputText: `"-0.001s"`,
- wantMessage: &knownpb.Duration{Nanos: -1e6},
- }, {
- desc: "Duration with -nanos",
- inputMessage: &knownpb.Duration{},
- inputText: `"-.001s"`,
- wantMessage: &knownpb.Duration{Nanos: -1e6},
- }, {
- desc: "Duration with +nanos",
- inputMessage: &knownpb.Duration{},
- inputText: `"+.001s"`,
- wantMessage: &knownpb.Duration{Nanos: 1e6},
- }, {
- desc: "Duration with -secs -nanos",
- inputMessage: &knownpb.Duration{},
- inputText: `"-123.000000450s"`,
- wantMessage: &knownpb.Duration{Seconds: -123, Nanos: -450},
- }, {
- desc: "Duration with large secs",
- inputMessage: &knownpb.Duration{},
- inputText: `"10000000000.000000001s"`,
- wantMessage: &knownpb.Duration{Seconds: 1e10, Nanos: 1},
- }, {
- desc: "Duration with decimal without fractional",
- inputMessage: &knownpb.Duration{},
- inputText: `"3.s"`,
- wantMessage: &knownpb.Duration{Seconds: 3},
- }, {
- desc: "Duration with decimal without integer",
- inputMessage: &knownpb.Duration{},
- inputText: `"0.5s"`,
- wantMessage: &knownpb.Duration{Nanos: 5e8},
- }, {
- desc: "Duration max value",
- inputMessage: &knownpb.Duration{},
- inputText: `"315576000000.999999999s"`,
- wantMessage: &knownpb.Duration{Seconds: 315576000000, Nanos: 999999999},
- }, {
- desc: "Duration min value",
- inputMessage: &knownpb.Duration{},
- inputText: `"-315576000000.999999999s"`,
- wantMessage: &knownpb.Duration{Seconds: -315576000000, Nanos: -999999999},
- }, {
- desc: "Duration with +secs out of range",
- inputMessage: &knownpb.Duration{},
- inputText: `"315576000001s"`,
- wantErr: true,
- }, {
- desc: "Duration with -secs out of range",
- inputMessage: &knownpb.Duration{},
- inputText: `"-315576000001s"`,
- wantErr: true,
- }, {
- desc: "Duration with nanos beyond 9 digits",
- inputMessage: &knownpb.Duration{},
- inputText: `"0.1000000000s"`,
- wantErr: true,
- }, {
- desc: "Duration without suffix s",
- inputMessage: &knownpb.Duration{},
- inputText: `"123"`,
- wantErr: true,
- }, {
- desc: "Duration invalid signed fraction",
- inputMessage: &knownpb.Duration{},
- inputText: `"123.+123s"`,
- wantErr: true,
- }, {
- desc: "Duration invalid multiple .",
- inputMessage: &knownpb.Duration{},
- inputText: `"123.123.s"`,
- wantErr: true,
- }, {
- desc: "Duration invalid integer",
- inputMessage: &knownpb.Duration{},
- inputText: `"01s"`,
- wantErr: true,
- }, {
- desc: "Timestamp zero",
- inputMessage: &knownpb.Timestamp{},
- inputText: `"1970-01-01T00:00:00Z"`,
- wantMessage: &knownpb.Timestamp{},
- }, {
- desc: "Timestamp with tz adjustment",
- inputMessage: &knownpb.Timestamp{},
- inputText: `"1970-01-01T00:00:00+01:00"`,
- wantMessage: &knownpb.Timestamp{Seconds: -3600},
- }, {
- desc: "Timestamp UTC",
- inputMessage: &knownpb.Timestamp{},
- inputText: `"2019-03-19T23:03:21Z"`,
- wantMessage: &knownpb.Timestamp{Seconds: 1553036601},
- }, {
- desc: "Timestamp with escaped unicode",
- inputMessage: &knownpb.Timestamp{},
- inputText: `"2019-0\u0033-19T23:03:21Z"`,
- wantMessage: &knownpb.Timestamp{Seconds: 1553036601},
- }, {
- desc: "Timestamp with nanos",
- inputMessage: &knownpb.Timestamp{},
- inputText: `"2019-03-19T23:03:21.000000001Z"`,
- wantMessage: &knownpb.Timestamp{Seconds: 1553036601, Nanos: 1},
- }, {
- desc: "Timestamp max value",
- inputMessage: &knownpb.Timestamp{},
- inputText: `"9999-12-31T23:59:59.999999999Z"`,
- wantMessage: &knownpb.Timestamp{Seconds: 253402300799, Nanos: 999999999},
- }, {
- desc: "Timestamp above max value",
- inputMessage: &knownpb.Timestamp{},
- inputText: `"9999-12-31T23:59:59-01:00"`,
- wantErr: true,
- }, {
- desc: "Timestamp min value",
- inputMessage: &knownpb.Timestamp{},
- inputText: `"0001-01-01T00:00:00Z"`,
- wantMessage: &knownpb.Timestamp{Seconds: -62135596800},
- }, {
- desc: "Timestamp below min value",
- inputMessage: &knownpb.Timestamp{},
- inputText: `"0001-01-01T00:00:00+01:00"`,
- wantErr: true,
- }, {
- desc: "Timestamp with nanos beyond 9 digits",
- inputMessage: &knownpb.Timestamp{},
- inputText: `"1970-01-01T00:00:00.0000000001Z"`,
- wantErr: true,
- }, {
- desc: "FieldMask empty",
- inputMessage: &knownpb.FieldMask{},
- inputText: `""`,
- wantMessage: &knownpb.FieldMask{Paths: []string{}},
- }, {
- desc: "FieldMask",
- inputMessage: &knownpb.FieldMask{},
- inputText: `"foo,fooBar , foo.barQux ,Foo"`,
- wantMessage: &knownpb.FieldMask{
- Paths: []string{
- "foo",
- "foo_bar",
- "foo.bar_qux",
- "_foo",
- },
- },
- }, {
- desc: "FieldMask field",
- inputMessage: &pb2.KnownTypes{},
- inputText: `{
- "optFieldmask": "foo, qux.fooBar"
-}`,
- wantMessage: &pb2.KnownTypes{
- OptFieldmask: &knownpb.FieldMask{
- Paths: []string{
- "foo",
- "qux.foo_bar",
- },
- },
- },
- }, {
- desc: "Any empty",
- inputMessage: &knownpb.Any{},
- inputText: `{}`,
- wantMessage: &knownpb.Any{},
- }, {
- desc: "Any with non-custom message",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "foo/pb2.Nested",
- "optString": "embedded inside Any",
- "optNested": {
- "optString": "inception"
- }
-}`,
- wantMessage: func() proto.Message {
- m := &pb2.Nested{
- OptString: scalar.String("embedded inside Any"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("inception"),
- },
- }
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "foo/pb2.Nested",
- Value: b,
- }
- }(),
- }, {
- desc: "Any with empty embedded message",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{"@type": "foo/pb2.Nested"}`,
- wantMessage: &knownpb.Any{TypeUrl: "foo/pb2.Nested"},
- }, {
- desc: "Any without registered type",
- umo: jsonpb.UnmarshalOptions{Resolver: preg.NewTypes()},
- inputMessage: &knownpb.Any{},
- inputText: `{"@type": "foo/pb2.Nested"}`,
- wantErr: true,
- }, {
- desc: "Any with missing required error",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "pb2.PartialRequired",
- "optString": "embedded inside Any"
-}`,
- wantMessage: func() proto.Message {
- m := &pb2.PartialRequired{
- OptString: scalar.String("embedded inside Any"),
- }
- b, err := proto.MarshalOptions{
- Deterministic: true,
- AllowPartial: true,
- }.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
- Value: b,
- }
- }(),
- wantErr: true,
- }, {
- desc: "Any with partial required and AllowPartial",
- umo: jsonpb.UnmarshalOptions{
- AllowPartial: true,
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "pb2.PartialRequired",
- "optString": "embedded inside Any"
-}`,
- wantMessage: func() proto.Message {
- m := &pb2.PartialRequired{
- OptString: scalar.String("embedded inside Any"),
- }
- b, err := proto.MarshalOptions{
- Deterministic: true,
- AllowPartial: true,
- }.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
- Value: b,
- }
- }(),
- }, {
- desc: "Any with invalid UTF8",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "optString": "` + "abc\xff" + `",
- "@type": "foo/pb2.Nested"
-}`,
- wantMessage: func() proto.Message {
- m := &pb2.Nested{
- OptString: scalar.String("abc\xff"),
- }
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "foo/pb2.Nested",
- Value: b,
- }
- }(),
- wantErr: true,
- }, {
- desc: "Any with BoolValue",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.BoolValue{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "type.googleapis.com/google.protobuf.BoolValue",
- "value": true
-}`,
- wantMessage: func() proto.Message {
- m := &knownpb.BoolValue{Value: true}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.BoolValue",
- Value: b,
- }
- }(),
- }, {
- desc: "Any with Empty",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Empty{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "value": {},
- "@type": "type.googleapis.com/google.protobuf.Empty"
-}`,
- wantMessage: &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.Empty",
- },
- }, {
- desc: "Any with missing Empty",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Empty{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "type.googleapis.com/google.protobuf.Empty"
-}`,
- wantErr: true,
- }, {
- desc: "Any with StringValue containing invalid UTF8",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.StringValue{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "google.protobuf.StringValue",
- "value": "` + "abc\xff" + `"
-}`,
- wantMessage: func() proto.Message {
- m := &knownpb.StringValue{Value: "abcd"}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "google.protobuf.StringValue",
- Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
- }
- }(),
- wantErr: true,
- }, {
- desc: "Any with Int64Value",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Int64Value{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "google.protobuf.Int64Value",
- "value": "42"
-}`,
- wantMessage: func() proto.Message {
- m := &knownpb.Int64Value{Value: 42}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "google.protobuf.Int64Value",
- Value: b,
- }
- }(),
- }, {
- desc: "Any with invalid Int64Value",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Int64Value{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "google.protobuf.Int64Value",
- "value": "forty-two"
-}`,
- wantErr: true,
- }, {
- desc: "Any with invalid UInt64Value",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.UInt64Value{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "google.protobuf.UInt64Value",
- "value": -42
-}`,
- wantErr: true,
- }, {
- desc: "Any with Duration",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Duration{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "type.googleapis.com/google.protobuf.Duration",
- "value": "0s"
-}`,
- wantMessage: func() proto.Message {
- m := &knownpb.Duration{}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.Duration",
- Value: b,
- }
- }(),
- }, {
- desc: "Any with Value of StringValue",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Value{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "google.protobuf.Value",
- "value": "` + "abc\xff" + `"
-}`,
- wantMessage: func() proto.Message {
- m := &knownpb.Value{Kind: &knownpb.Value_StringValue{"abcd"}}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "google.protobuf.Value",
- Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
- }
- }(),
- wantErr: true,
- }, {
- desc: "Any with Value of NullValue",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Value{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "google.protobuf.Value",
- "value": null
-}`,
- wantMessage: func() proto.Message {
- m := &knownpb.Value{Kind: &knownpb.Value_NullValue{}}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "google.protobuf.Value",
- Value: b,
- }
- }(),
- }, {
- desc: "Any with Struct",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(
- pimpl.Export{}.MessageTypeOf(&knownpb.Struct{}),
- pimpl.Export{}.MessageTypeOf(&knownpb.Value{}),
- pimpl.Export{}.MessageTypeOf(&knownpb.BoolValue{}),
- pimpl.Export{}.EnumTypeOf(knownpb.NullValue_NULL_VALUE),
- pimpl.Export{}.MessageTypeOf(&knownpb.StringValue{}),
- ),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "google.protobuf.Struct",
- "value": {
- "bool": true,
- "null": null,
- "string": "hello",
- "struct": {
- "string": "world"
- }
- }
-}`,
- wantMessage: func() proto.Message {
- m := &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "bool": {Kind: &knownpb.Value_BoolValue{true}},
- "null": {Kind: &knownpb.Value_NullValue{}},
- "string": {Kind: &knownpb.Value_StringValue{"hello"}},
- "struct": {
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"world"}},
- },
- },
- },
- },
- },
- }
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "google.protobuf.Struct",
- Value: b,
- }
- }(),
- }, {
- desc: "Any with missing @type",
- umo: jsonpb.UnmarshalOptions{},
- inputMessage: &knownpb.Any{},
- inputText: `{
- "value": {}
-}`,
- wantErr: true,
- }, {
- desc: "Any with empty @type",
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": ""
-}`,
- wantErr: true,
- }, {
- desc: "Any with duplicate @type",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(
- pimpl.Export{}.MessageTypeOf(&pb2.Nested{}),
- pimpl.Export{}.MessageTypeOf(&knownpb.StringValue{}),
- ),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "google.protobuf.StringValue",
- "value": "hello",
- "@type": "pb2.Nested"
-}`,
- wantErr: true,
- }, {
- desc: "Any with duplicate value",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.StringValue{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "google.protobuf.StringValue",
- "value": "hello",
- "value": "world"
-}`,
- wantErr: true,
- }, {
- desc: "Any with unknown field",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "pb2.Nested",
- "optString": "hello",
- "unknown": "world"
-}`,
- wantErr: true,
- }, {
- desc: "Any with embedded type containing Any",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(
- pimpl.Export{}.MessageTypeOf(&pb2.KnownTypes{}),
- pimpl.Export{}.MessageTypeOf(&knownpb.Any{}),
- pimpl.Export{}.MessageTypeOf(&knownpb.StringValue{}),
- ),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "pb2.KnownTypes",
- "optAny": {
- "@type": "google.protobuf.StringValue",
- "value": "` + "abc\xff" + `"
- }
-}`,
- wantMessage: func() proto.Message {
- m1 := &knownpb.StringValue{Value: "abcd"}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m1)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- m2 := &knownpb.Any{
- TypeUrl: "google.protobuf.StringValue",
- Value: b,
- }
- m3 := &pb2.KnownTypes{OptAny: m2}
- b, err = proto.MarshalOptions{Deterministic: true}.Marshal(m3)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "pb2.KnownTypes",
- Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
- }
- }(),
- wantErr: true,
- }, {
- desc: "well known types as field values",
- umo: jsonpb.UnmarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Empty{})),
- },
- inputMessage: &pb2.KnownTypes{},
- inputText: `{
- "optBool": false,
- "optInt32": 42,
- "optInt64": "42",
- "optUint32": 42,
- "optUint64": "42",
- "optFloat": 1.23,
- "optDouble": 3.1415,
- "optString": "hello",
- "optBytes": "aGVsbG8=",
- "optDuration": "123s",
- "optTimestamp": "2019-03-19T23:03:21Z",
- "optStruct": {
- "string": "hello"
- },
- "optList": [
- null,
- "",
- {},
- []
- ],
- "optValue": "world",
- "optEmpty": {},
- "optAny": {
- "@type": "google.protobuf.Empty",
- "value": {}
- },
- "optFieldmask": "fooBar,barFoo"
-}`,
- wantMessage: &pb2.KnownTypes{
- OptBool: &knownpb.BoolValue{Value: false},
- OptInt32: &knownpb.Int32Value{Value: 42},
- OptInt64: &knownpb.Int64Value{Value: 42},
- OptUint32: &knownpb.UInt32Value{Value: 42},
- OptUint64: &knownpb.UInt64Value{Value: 42},
- OptFloat: &knownpb.FloatValue{Value: 1.23},
- OptDouble: &knownpb.DoubleValue{Value: 3.1415},
- OptString: &knownpb.StringValue{Value: "hello"},
- OptBytes: &knownpb.BytesValue{Value: []byte("hello")},
- OptDuration: &knownpb.Duration{Seconds: 123},
- OptTimestamp: &knownpb.Timestamp{Seconds: 1553036601},
- OptStruct: &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"hello"}},
- },
- },
- OptList: &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_NullValue{}},
- {Kind: &knownpb.Value_StringValue{}},
- {
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{Fields: map[string]*knownpb.Value{}},
- },
- },
- {
- Kind: &knownpb.Value_ListValue{
- &knownpb.ListValue{Values: []*knownpb.Value{}},
- },
- },
- },
- },
- OptValue: &knownpb.Value{
- Kind: &knownpb.Value_StringValue{"world"},
- },
- OptEmpty: &knownpb.Empty{},
- OptAny: &knownpb.Any{
- TypeUrl: "google.protobuf.Empty",
- },
- OptFieldmask: &knownpb.FieldMask{
- Paths: []string{"foo_bar", "bar_foo"},
- },
- },
- }, {
- desc: "DiscardUnknown: regular messages",
- umo: jsonpb.UnmarshalOptions{DiscardUnknown: true},
- inputMessage: &pb3.Nests{},
- inputText: `{
- "sNested": {
- "unknown": {
- "foo": 1,
- "bar": [1, 2, 3]
- }
- },
- "unknown": "not known"
-}`,
- wantMessage: &pb3.Nests{SNested: &pb3.Nested{}},
- }, {
- desc: "DiscardUnknown: repeated",
- umo: jsonpb.UnmarshalOptions{DiscardUnknown: true},
- inputMessage: &pb2.Nests{},
- inputText: `{
- "rptNested": [
- {"unknown": "blah"},
- {"optString": "hello"}
- ]
-}`,
- wantMessage: &pb2.Nests{
- RptNested: []*pb2.Nested{
- {},
- {OptString: scalar.String("hello")},
- },
- },
- }, {
- desc: "DiscardUnknown: map",
- umo: jsonpb.UnmarshalOptions{DiscardUnknown: true},
- inputMessage: &pb3.Maps{},
- inputText: `{
- "strToNested": {
- "nested_one": {
- "unknown": "what you see is not"
- }
- }
-}`,
- wantMessage: &pb3.Maps{
- StrToNested: map[string]*pb3.Nested{
- "nested_one": {},
- },
- },
- }, {
- desc: "DiscardUnknown: extension",
- umo: jsonpb.UnmarshalOptions{DiscardUnknown: true},
- inputMessage: &pb2.Extensions{},
- inputText: `{
- "[pb2.opt_ext_nested]": {
- "unknown": []
- }
-}`,
- wantMessage: func() proto.Message {
- m := &pb2.Extensions{}
- setExtension(m, pb2.E_OptExtNested, &pb2.Nested{})
- return m
- }(),
- }, {
- desc: "DiscardUnknown: Empty",
- umo: jsonpb.UnmarshalOptions{DiscardUnknown: true},
- inputMessage: &knownpb.Empty{},
- inputText: `{"unknown": "something"}`,
- wantMessage: &knownpb.Empty{},
- }, {
- desc: "DiscardUnknown: Any without type",
- umo: jsonpb.UnmarshalOptions{DiscardUnknown: true},
- inputMessage: &knownpb.Any{},
- inputText: `{
- "value": {"foo": "bar"},
- "unknown": true
-}`,
- wantMessage: &knownpb.Any{},
- }, {
- desc: "DiscardUnknown: Any",
- umo: jsonpb.UnmarshalOptions{
- DiscardUnknown: true,
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "foo/pb2.Nested",
- "unknown": "none"
-}`,
- wantMessage: &knownpb.Any{
- TypeUrl: "foo/pb2.Nested",
- },
- }, {
- desc: "DiscardUnknown: Any with Empty",
- umo: jsonpb.UnmarshalOptions{
- DiscardUnknown: true,
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Empty{})),
- },
- inputMessage: &knownpb.Any{},
- inputText: `{
- "@type": "type.googleapis.com/google.protobuf.Empty",
- "value": {"unknown": 47}
-}`,
- wantMessage: &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.Empty",
- },
- }}
-
- for _, tt := range tests {
- tt := tt
- t.Run(tt.desc, func(t *testing.T) {
- err := tt.umo.Unmarshal(tt.inputMessage, []byte(tt.inputText))
- if err != nil && !tt.wantErr {
- t.Errorf("Unmarshal() returned error: %v\n\n", err)
- }
- if err == nil && tt.wantErr {
- t.Error("Unmarshal() got nil error, want error\n\n")
- }
- if tt.wantMessage != nil && !protoV1.Equal(tt.inputMessage.(protoV1.Message), tt.wantMessage.(protoV1.Message)) {
- t.Errorf("Unmarshal()\n<got>\n%v\n<want>\n%v\n", tt.inputMessage, tt.wantMessage)
- }
- })
- }
-}
diff --git a/encoding/jsonpb/doc.go b/encoding/jsonpb/doc.go
deleted file mode 100644
index 99cf2dd..0000000
--- a/encoding/jsonpb/doc.go
+++ /dev/null
@@ -1,11 +0,0 @@
-// Copyright 2019 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// Package jsonpb marshals and unmarshals protocol buffer messages as JSON
-// format. It follows the guide at
-// https://developers.google.com/protocol-buffers/docs/proto3#json.
-//
-// This package produces a different output than the standard "encoding/json"
-// package, which does not operate correctly on protocol buffer messages.
-package jsonpb
diff --git a/encoding/jsonpb/encode.go b/encoding/jsonpb/encode.go
deleted file mode 100644
index 1d353b7..0000000
--- a/encoding/jsonpb/encode.go
+++ /dev/null
@@ -1,323 +0,0 @@
-// Copyright 2019 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package jsonpb
-
-import (
- "encoding/base64"
- "fmt"
- "sort"
-
- "google.golang.org/protobuf/internal/encoding/json"
- "google.golang.org/protobuf/internal/errors"
- "google.golang.org/protobuf/internal/pragma"
- "google.golang.org/protobuf/proto"
- pref "google.golang.org/protobuf/reflect/protoreflect"
- "google.golang.org/protobuf/reflect/protoregistry"
-)
-
-// Marshal writes the given proto.Message in JSON format using default options.
-func Marshal(m proto.Message) ([]byte, error) {
- return MarshalOptions{}.Marshal(m)
-}
-
-// MarshalOptions is a configurable JSON format marshaler.
-type MarshalOptions struct {
- pragma.NoUnkeyedLiterals
-
- // AllowPartial allows messages that have missing required fields to marshal
- // without returning an error. If AllowPartial is false (the default),
- // Marshal will return error if there are any missing required fields.
- AllowPartial bool
-
- // If Indent is a non-empty string, it causes entries for an Array or Object
- // to be preceded by the indent and trailed by a newline. Indent can only be
- // composed of space or tab characters.
- Indent string
-
- // Resolver is the registry used for type lookups when marshaling
- // 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) {
- 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 = o.marshalMessage(m.ProtoReflect())
- if !nerr.Merge(err) {
- return nil, err
- }
- if !o.AllowPartial {
- nerr.Merge(proto.IsInitialized(m))
- }
- return o.encoder.Bytes(), nerr.E
-}
-
-// marshalMessage marshals the given protoreflect.Message.
-func (o MarshalOptions) marshalMessage(m pref.Message) error {
- var nerr errors.NonFatal
-
- if isCustomType(m.Descriptor().FullName()) {
- return o.marshalCustomType(m)
- }
-
- o.encoder.StartObject()
- defer o.encoder.EndObject()
- if err := o.marshalFields(m); !nerr.Merge(err) {
- return err
- }
-
- return nerr.E
-}
-
-// marshalFields marshals the fields in the given protoreflect.Message.
-func (o MarshalOptions) marshalFields(m pref.Message) error {
- var nerr errors.NonFatal
- fieldDescs := m.Descriptor().Fields()
- knownFields := m.KnownFields()
-
- // Marshal out known fields.
- for i := 0; i < fieldDescs.Len(); i++ {
- fd := fieldDescs.Get(i)
- num := fd.Number()
-
- if !knownFields.Has(num) {
- continue
- }
-
- name := fd.JSONName()
- val := knownFields.Get(num)
- if err := o.encoder.WriteName(name); !nerr.Merge(err) {
- return err
- }
- if err := o.marshalValue(val, fd); !nerr.Merge(err) {
- return err
- }
- }
-
- // Marshal out extensions.
- if err := o.marshalExtensions(knownFields); !nerr.Merge(err) {
- return err
- }
- return nerr.E
-}
-
-// marshalValue marshals the given protoreflect.Value.
-func (o MarshalOptions) marshalValue(val pref.Value, fd pref.FieldDescriptor) error {
- switch {
- case fd.IsList():
- return o.marshalList(val.List(), fd)
- case fd.IsMap():
- return o.marshalMap(val.Map(), fd)
- default:
- return o.marshalSingular(val, fd)
- }
-}
-
-// marshalSingular marshals the given non-repeated field value. This includes
-// all scalar types, enums, messages, and groups.
-func (o MarshalOptions) marshalSingular(val pref.Value, fd pref.FieldDescriptor) error {
- var nerr errors.NonFatal
- switch kind := fd.Kind(); kind {
- case pref.BoolKind:
- o.encoder.WriteBool(val.Bool())
-
- case pref.StringKind:
- if err := o.encoder.WriteString(val.String()); !nerr.Merge(err) {
- return err
- }
-
- case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
- o.encoder.WriteInt(val.Int())
-
- case pref.Uint32Kind, pref.Fixed32Kind:
- 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.
- o.encoder.WriteString(val.String())
-
- case pref.FloatKind:
- // Encoder.WriteFloat handles the special numbers NaN and infinites.
- o.encoder.WriteFloat(val.Float(), 32)
-
- case pref.DoubleKind:
- // Encoder.WriteFloat handles the special numbers NaN and infinites.
- o.encoder.WriteFloat(val.Float(), 64)
-
- case pref.BytesKind:
- err := o.encoder.WriteString(base64.StdEncoding.EncodeToString(val.Bytes()))
- if !nerr.Merge(err) {
- return err
- }
-
- case pref.EnumKind:
- if fd.Enum().FullName() == "google.protobuf.NullValue" {
- o.encoder.WriteNull()
- } else if desc := fd.Enum().Values().ByNumber(val.Enum()); desc != nil {
- err := o.encoder.WriteString(string(desc.Name()))
- if !nerr.Merge(err) {
- return err
- }
- } else {
- // Use numeric value if there is no enum value descriptor.
- o.encoder.WriteInt(int64(val.Enum()))
- }
-
- case pref.MessageKind, pref.GroupKind:
- if err := o.marshalMessage(val.Message()); !nerr.Merge(err) {
- return err
- }
-
- default:
- panic(fmt.Sprintf("%v has unknown kind: %v", fd.FullName(), kind))
- }
- return nerr.E
-}
-
-// marshalList marshals the given protoreflect.List.
-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 := o.marshalSingular(item, fd); !nerr.Merge(err) {
- return err
- }
- }
- return nerr.E
-}
-
-type mapEntry struct {
- key pref.MapKey
- value pref.Value
-}
-
-// marshalMap marshals given protoreflect.Map.
-func (o MarshalOptions) marshalMap(mmap pref.Map, fd pref.FieldDescriptor) error {
- o.encoder.StartObject()
- defer o.encoder.EndObject()
-
- // Get a sorted list based on keyType first.
- entries := make([]mapEntry, 0, mmap.Len())
- mmap.Range(func(key pref.MapKey, val pref.Value) bool {
- entries = append(entries, mapEntry{key: key, value: val})
- return true
- })
- sortMap(fd.MapKey().Kind(), entries)
-
- // Write out sorted list.
- var nerr errors.NonFatal
- for _, entry := range entries {
- if err := o.encoder.WriteName(entry.key.String()); !nerr.Merge(err) {
- return err
- }
- if err := o.marshalSingular(entry.value, fd.MapValue()); !nerr.Merge(err) {
- return err
- }
- }
- return nerr.E
-}
-
-// sortMap orders list based on value of key field for deterministic ordering.
-func sortMap(keyKind pref.Kind, values []mapEntry) {
- sort.Slice(values, func(i, j int) bool {
- switch keyKind {
- case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind,
- pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
- return values[i].key.Int() < values[j].key.Int()
-
- case pref.Uint32Kind, pref.Fixed32Kind,
- pref.Uint64Kind, pref.Fixed64Kind:
- return values[i].key.Uint() < values[j].key.Uint()
- }
- return values[i].key.String() < values[j].key.String()
- })
-}
-
-// marshalExtensions marshals extension fields.
-func (o MarshalOptions) marshalExtensions(knownFields pref.KnownFields) error {
- type xtEntry struct {
- key string
- value pref.Value
- xtType pref.ExtensionType
- }
-
- xtTypes := knownFields.ExtensionTypes()
-
- // Get a sorted list based on field key first.
- entries := make([]xtEntry, 0, xtTypes.Len())
- xtTypes.Range(func(xt pref.ExtensionType) bool {
- name := xt.Descriptor().FullName()
- // If extended type is a MessageSet, set field name to be the message type name.
- if isMessageSetExtension(xt) {
- name = xt.Descriptor().Message().FullName()
- }
-
- num := xt.Descriptor().Number()
- if knownFields.Has(num) {
- // Use [name] format for JSON field name.
- pval := knownFields.Get(num)
- entries = append(entries, xtEntry{
- key: string(name),
- value: pval,
- xtType: xt,
- })
- }
- return true
- })
-
- // Sort extensions lexicographically.
- sort.Slice(entries, func(i, j int) bool {
- return entries[i].key < entries[j].key
- })
-
- // Write out sorted list.
- var nerr errors.NonFatal
- for _, entry := range entries {
- // 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 := o.encoder.WriteName("[" + entry.key + "]"); !nerr.Merge(err) {
- return err
- }
- if err := o.marshalValue(entry.value, entry.xtType.Descriptor()); !nerr.Merge(err) {
- return err
- }
- }
- return nerr.E
-}
-
-// isMessageSetExtension reports whether extension extends a message set.
-func isMessageSetExtension(xt pref.ExtensionType) bool {
- xd := xt.Descriptor()
- if xd.Name() != "message_set_extension" {
- return false
- }
- md := xd.Message()
- if md == nil {
- return false
- }
- if xd.FullName().Parent() != md.FullName() {
- return false
- }
- xmd, ok := xd.ContainingMessage().(interface{ IsMessageSet() bool })
- return ok && xmd.IsMessageSet()
-}
diff --git a/encoding/jsonpb/encode_test.go b/encoding/jsonpb/encode_test.go
deleted file mode 100644
index 6043209..0000000
--- a/encoding/jsonpb/encode_test.go
+++ /dev/null
@@ -1,1975 +0,0 @@
-// Copyright 2019 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package jsonpb_test
-
-import (
- "bytes"
- "encoding/hex"
- "math"
- "strings"
- "testing"
-
- "github.com/google/go-cmp/cmp"
- "github.com/google/go-cmp/cmp/cmpopts"
- "google.golang.org/protobuf/encoding/jsonpb"
- "google.golang.org/protobuf/internal/encoding/pack"
- "google.golang.org/protobuf/internal/encoding/wire"
- pimpl "google.golang.org/protobuf/internal/impl"
- "google.golang.org/protobuf/internal/scalar"
- "google.golang.org/protobuf/proto"
- preg "google.golang.org/protobuf/reflect/protoregistry"
- "google.golang.org/protobuf/runtime/protoiface"
-
- "google.golang.org/protobuf/encoding/testprotos/pb2"
- "google.golang.org/protobuf/encoding/testprotos/pb3"
- knownpb "google.golang.org/protobuf/types/known"
-)
-
-// splitLines is a cmpopts.Option for comparing strings with line breaks.
-var splitLines = cmpopts.AcyclicTransformer("SplitLines", func(s string) []string {
- return strings.Split(s, "\n")
-})
-
-func pb2Enum(i int32) *pb2.Enum {
- p := new(pb2.Enum)
- *p = pb2.Enum(i)
- return p
-}
-
-func pb2Enums_NestedEnum(i int32) *pb2.Enums_NestedEnum {
- p := new(pb2.Enums_NestedEnum)
- *p = pb2.Enums_NestedEnum(i)
- return p
-}
-
-func setExtension(m proto.Message, xd *protoiface.ExtensionDescV1, val interface{}) {
- knownFields := m.ProtoReflect().KnownFields()
- extTypes := knownFields.ExtensionTypes()
- extTypes.Register(xd.Type)
- if val == nil {
- return
- }
- pval := xd.Type.ValueOf(val)
- knownFields.Set(wire.Number(xd.Field), pval)
-}
-
-// dhex decodes a hex-string and returns the bytes and panics if s is invalid.
-func dhex(s string) []byte {
- b, err := hex.DecodeString(s)
- if err != nil {
- panic(err)
- }
- return b
-}
-
-func TestMarshal(t *testing.T) {
- tests := []struct {
- desc string
- mo jsonpb.MarshalOptions
- input proto.Message
- want string
- wantErr bool // TODO: Verify error message substring.
- }{{
- desc: "proto2 optional scalars not set",
- input: &pb2.Scalars{},
- want: "{}",
- }, {
- desc: "proto3 scalars not set",
- input: &pb3.Scalars{},
- want: "{}",
- }, {
- desc: "proto2 optional scalars set to zero values",
- input: &pb2.Scalars{
- OptBool: scalar.Bool(false),
- OptInt32: scalar.Int32(0),
- OptInt64: scalar.Int64(0),
- OptUint32: scalar.Uint32(0),
- OptUint64: scalar.Uint64(0),
- OptSint32: scalar.Int32(0),
- OptSint64: scalar.Int64(0),
- OptFixed32: scalar.Uint32(0),
- OptFixed64: scalar.Uint64(0),
- OptSfixed32: scalar.Int32(0),
- OptSfixed64: scalar.Int64(0),
- OptFloat: scalar.Float32(0),
- OptDouble: scalar.Float64(0),
- OptBytes: []byte{},
- OptString: scalar.String(""),
- },
- want: `{
- "optBool": false,
- "optInt32": 0,
- "optInt64": "0",
- "optUint32": 0,
- "optUint64": "0",
- "optSint32": 0,
- "optSint64": "0",
- "optFixed32": 0,
- "optFixed64": "0",
- "optSfixed32": 0,
- "optSfixed64": "0",
- "optFloat": 0,
- "optDouble": 0,
- "optBytes": "",
- "optString": ""
-}`,
- }, {
- desc: "proto2 optional scalars set to some values",
- input: &pb2.Scalars{
- OptBool: scalar.Bool(true),
- OptInt32: scalar.Int32(0xff),
- OptInt64: scalar.Int64(0xdeadbeef),
- OptUint32: scalar.Uint32(47),
- OptUint64: scalar.Uint64(0xdeadbeef),
- OptSint32: scalar.Int32(-1001),
- OptSint64: scalar.Int64(-0xffff),
- OptFixed64: scalar.Uint64(64),
- OptSfixed32: scalar.Int32(-32),
- OptFloat: scalar.Float32(1.02),
- OptDouble: scalar.Float64(1.234),
- OptBytes: []byte("è°·æŒ"),
- OptString: scalar.String("è°·æŒ"),
- },
- want: `{
- "optBool": true,
- "optInt32": 255,
- "optInt64": "3735928559",
- "optUint32": 47,
- "optUint64": "3735928559",
- "optSint32": -1001,
- "optSint64": "-65535",
- "optFixed64": "64",
- "optSfixed32": -32,
- "optFloat": 1.02,
- "optDouble": 1.234,
- "optBytes": "6LC35q2M",
- "optString": "è°·æŒ"
-}`,
- }, {
- desc: "string",
- input: &pb3.Scalars{
- SString: "è°·æŒ",
- },
- want: `{
- "sString": "è°·æŒ"
-}`,
- }, {
- desc: "string with invalid UTF8",
- input: &pb3.Scalars{
- SString: "abc\xff",
- },
- want: "{\n \"sString\": \"abc\xff\"\n}",
- wantErr: true,
- }, {
- desc: "float nan",
- input: &pb3.Scalars{
- SFloat: float32(math.NaN()),
- },
- want: `{
- "sFloat": "NaN"
-}`,
- }, {
- desc: "float positive infinity",
- input: &pb3.Scalars{
- SFloat: float32(math.Inf(1)),
- },
- want: `{
- "sFloat": "Infinity"
-}`,
- }, {
- desc: "float negative infinity",
- input: &pb3.Scalars{
- SFloat: float32(math.Inf(-1)),
- },
- want: `{
- "sFloat": "-Infinity"
-}`,
- }, {
- desc: "double nan",
- input: &pb3.Scalars{
- SDouble: math.NaN(),
- },
- want: `{
- "sDouble": "NaN"
-}`,
- }, {
- desc: "double positive infinity",
- input: &pb3.Scalars{
- SDouble: math.Inf(1),
- },
- want: `{
- "sDouble": "Infinity"
-}`,
- }, {
- desc: "double negative infinity",
- input: &pb3.Scalars{
- SDouble: math.Inf(-1),
- },
- want: `{
- "sDouble": "-Infinity"
-}`,
- }, {
- desc: "proto2 enum not set",
- input: &pb2.Enums{},
- want: "{}",
- }, {
- desc: "proto2 enum set to zero value",
- input: &pb2.Enums{
- OptEnum: pb2Enum(0),
- OptNestedEnum: pb2Enums_NestedEnum(0),
- },
- want: `{
- "optEnum": 0,
- "optNestedEnum": 0
-}`,
- }, {
- desc: "proto2 enum",
- input: &pb2.Enums{
- OptEnum: pb2.Enum_ONE.Enum(),
- OptNestedEnum: pb2.Enums_UNO.Enum(),
- },
- want: `{
- "optEnum": "ONE",
- "optNestedEnum": "UNO"
-}`,
- }, {
- desc: "proto2 enum set to numeric values",
- input: &pb2.Enums{
- OptEnum: pb2Enum(2),
- OptNestedEnum: pb2Enums_NestedEnum(2),
- },
- want: `{
- "optEnum": "TWO",
- "optNestedEnum": "DOS"
-}`,
- }, {
- desc: "proto2 enum set to unnamed numeric values",
- input: &pb2.Enums{
- OptEnum: pb2Enum(101),
- OptNestedEnum: pb2Enums_NestedEnum(-101),
- },
- want: `{
- "optEnum": 101,
- "optNestedEnum": -101
-}`,
- }, {
- desc: "proto3 enum not set",
- input: &pb3.Enums{},
- want: "{}",
- }, {
- desc: "proto3 enum set to zero value",
- input: &pb3.Enums{
- SEnum: pb3.Enum_ZERO,
- SNestedEnum: pb3.Enums_CERO,
- },
- want: "{}",
- }, {
- desc: "proto3 enum",
- input: &pb3.Enums{
- SEnum: pb3.Enum_ONE,
- SNestedEnum: pb3.Enums_UNO,
- },
- want: `{
- "sEnum": "ONE",
- "sNestedEnum": "UNO"
-}`,
- }, {
- desc: "proto3 enum set to numeric values",
- input: &pb3.Enums{
- SEnum: 2,
- SNestedEnum: 2,
- },
- want: `{
- "sEnum": "TWO",
- "sNestedEnum": "DOS"
-}`,
- }, {
- desc: "proto3 enum set to unnamed numeric values",
- input: &pb3.Enums{
- SEnum: -47,
- SNestedEnum: 47,
- },
- want: `{
- "sEnum": -47,
- "sNestedEnum": 47
-}`,
- }, {
- desc: "proto2 nested message not set",
- input: &pb2.Nests{},
- want: "{}",
- }, {
- desc: "proto2 nested message set to empty",
- input: &pb2.Nests{
- OptNested: &pb2.Nested{},
- Optgroup: &pb2.Nests_OptGroup{},
- },
- want: `{
- "optNested": {},
- "optgroup": {}
-}`,
- }, {
- desc: "proto2 nested messages",
- input: &pb2.Nests{
- OptNested: &pb2.Nested{
- OptString: scalar.String("nested message"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("another nested message"),
- },
- },
- },
- want: `{
- "optNested": {
- "optString": "nested message",
- "optNested": {
- "optString": "another nested message"
- }
- }
-}`,
- }, {
- desc: "proto2 groups",
- input: &pb2.Nests{
- Optgroup: &pb2.Nests_OptGroup{
- OptString: scalar.String("inside a group"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("nested message inside a group"),
- },
- Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{
- OptFixed32: scalar.Uint32(47),
- },
- },
- },
- want: `{
- "optgroup": {
- "optString": "inside a group",
- "optNested": {
- "optString": "nested message inside a group"
- },
- "optnestedgroup": {
- "optFixed32": 47
- }
- }
-}`,
- }, {
- desc: "proto3 nested message not set",
- input: &pb3.Nests{},
- want: "{}",
- }, {
- desc: "proto3 nested message set to empty",
- input: &pb3.Nests{
- SNested: &pb3.Nested{},
- },
- want: `{
- "sNested": {}
-}`,
- }, {
- desc: "proto3 nested message",
- input: &pb3.Nests{
- SNested: &pb3.Nested{
- SString: "nested message",
- SNested: &pb3.Nested{
- SString: "another nested message",
- },
- },
- },
- want: `{
- "sNested": {
- "sString": "nested message",
- "sNested": {
- "sString": "another nested message"
- }
- }
-}`,
- }, {
- desc: "oneof not set",
- input: &pb3.Oneofs{},
- want: "{}",
- }, {
- desc: "oneof set to empty string",
- input: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofString{},
- },
- want: `{
- "oneofString": ""
-}`,
- }, {
- desc: "oneof set to string",
- input: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofString{
- OneofString: "hello",
- },
- },
- want: `{
- "oneofString": "hello"
-}`,
- }, {
- desc: "oneof set to enum",
- input: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofEnum{
- OneofEnum: pb3.Enum_ZERO,
- },
- },
- want: `{
- "oneofEnum": "ZERO"
-}`,
- }, {
- desc: "oneof set to empty message",
- input: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofNested{
- OneofNested: &pb3.Nested{},
- },
- },
- want: `{
- "oneofNested": {}
-}`,
- }, {
- desc: "oneof set to message",
- input: &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofNested{
- OneofNested: &pb3.Nested{
- SString: "nested message",
- },
- },
- },
- want: `{
- "oneofNested": {
- "sString": "nested message"
- }
-}`,
- }, {
- desc: "repeated fields not set",
- input: &pb2.Repeats{},
- want: "{}",
- }, {
- desc: "repeated fields set to empty slices",
- input: &pb2.Repeats{
- RptBool: []bool{},
- RptInt32: []int32{},
- RptInt64: []int64{},
- RptUint32: []uint32{},
- RptUint64: []uint64{},
- RptFloat: []float32{},
- RptDouble: []float64{},
- RptBytes: [][]byte{},
- },
- want: "{}",
- }, {
- desc: "repeated fields set to some values",
- input: &pb2.Repeats{
- RptBool: []bool{true, false, true, true},
- RptInt32: []int32{1, 6, 0, 0},
- RptInt64: []int64{-64, 47},
- RptUint32: []uint32{0xff, 0xffff},
- RptUint64: []uint64{0xdeadbeef},
- RptFloat: []float32{float32(math.NaN()), float32(math.Inf(1)), float32(math.Inf(-1)), 1.034},
- RptDouble: []float64{math.NaN(), math.Inf(1), math.Inf(-1), 1.23e-308},
- RptString: []string{"hello", "世界"},
- RptBytes: [][]byte{
- []byte("hello"),
- []byte("\xe4\xb8\x96\xe7\x95\x8c"),
- },
- },
- want: `{
- "rptBool": [
- true,
- false,
- true,
- true
- ],
- "rptInt32": [
- 1,
- 6,
- 0,
- 0
- ],
- "rptInt64": [
- "-64",
- "47"
- ],
- "rptUint32": [
- 255,
- 65535
- ],
- "rptUint64": [
- "3735928559"
- ],
- "rptFloat": [
- "NaN",
- "Infinity",
- "-Infinity",
- 1.034
- ],
- "rptDouble": [
- "NaN",
- "Infinity",
- "-Infinity",
- 1.23e-308
- ],
- "rptString": [
- "hello",
- "世界"
- ],
- "rptBytes": [
- "aGVsbG8=",
- "5LiW55WM"
- ]
-}`,
- }, {
- desc: "repeated enums",
- input: &pb2.Enums{
- RptEnum: []pb2.Enum{pb2.Enum_ONE, 2, pb2.Enum_TEN, 42},
- RptNestedEnum: []pb2.Enums_NestedEnum{2, 47, 10},
- },
- want: `{
- "rptEnum": [
- "ONE",
- "TWO",
- "TEN",
- 42
- ],
- "rptNestedEnum": [
- "DOS",
- 47,
- "DIEZ"
- ]
-}`,
- }, {
- desc: "repeated messages set to empty",
- input: &pb2.Nests{
- RptNested: []*pb2.Nested{},
- Rptgroup: []*pb2.Nests_RptGroup{},
- },
- want: "{}",
- }, {
- desc: "repeated messages",
- input: &pb2.Nests{
- RptNested: []*pb2.Nested{
- {
- OptString: scalar.String("repeat nested one"),
- },
- {
- OptString: scalar.String("repeat nested two"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("inside repeat nested two"),
- },
- },
- {},
- },
- },
- want: `{
- "rptNested": [
- {
- "optString": "repeat nested one"
- },
- {
- "optString": "repeat nested two",
- "optNested": {
- "optString": "inside repeat nested two"
- }
- },
- {}
- ]
-}`,
- }, {
- desc: "repeated messages contains nil value",
- input: &pb2.Nests{
- RptNested: []*pb2.Nested{nil, {}},
- },
- want: `{
- "rptNested": [
- {},
- {}
- ]
-}`,
- }, {
- desc: "repeated groups",
- input: &pb2.Nests{
- Rptgroup: []*pb2.Nests_RptGroup{
- {
- RptString: []string{"hello", "world"},
- },
- {},
- nil,
- },
- },
- want: `{
- "rptgroup": [
- {
- "rptString": [
- "hello",
- "world"
- ]
- },
- {},
- {}
- ]
-}`,
- }, {
- desc: "map fields not set",
- input: &pb3.Maps{},
- want: "{}",
- }, {
- desc: "map fields set to empty",
- input: &pb3.Maps{
- Int32ToStr: map[int32]string{},
- BoolToUint32: map[bool]uint32{},
- Uint64ToEnum: map[uint64]pb3.Enum{},
- StrToNested: map[string]*pb3.Nested{},
- StrToOneofs: map[string]*pb3.Oneofs{},
- },
- want: "{}",
- }, {
- desc: "map fields 1",
- input: &pb3.Maps{
- BoolToUint32: map[bool]uint32{
- true: 42,
- false: 101,
- },
- },
- want: `{
- "boolToUint32": {
- "false": 101,
- "true": 42
- }
-}`,
- }, {
- desc: "map fields 2",
- input: &pb3.Maps{
- Int32ToStr: map[int32]string{
- -101: "-101",
- 0xff: "0xff",
- 0: "zero",
- },
- },
- want: `{
- "int32ToStr": {
- "-101": "-101",
- "0": "zero",
- "255": "0xff"
- }
-}`,
- }, {
- desc: "map fields 3",
- input: &pb3.Maps{
- Uint64ToEnum: map[uint64]pb3.Enum{
- 1: pb3.Enum_ONE,
- 2: pb3.Enum_TWO,
- 10: pb3.Enum_TEN,
- 47: 47,
- },
- },
- want: `{
- "uint64ToEnum": {
- "1": "ONE",
- "2": "TWO",
- "10": "TEN",
- "47": 47
- }
-}`,
- }, {
- desc: "map fields 4",
- input: &pb3.Maps{
- StrToNested: map[string]*pb3.Nested{
- "nested": &pb3.Nested{
- SString: "nested in a map",
- },
- },
- },
- want: `{
- "strToNested": {
- "nested": {
- "sString": "nested in a map"
- }
- }
-}`,
- }, {
- desc: "map fields 5",
- input: &pb3.Maps{
- StrToOneofs: map[string]*pb3.Oneofs{
- "string": &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofString{
- OneofString: "hello",
- },
- },
- "nested": &pb3.Oneofs{
- Union: &pb3.Oneofs_OneofNested{
- OneofNested: &pb3.Nested{
- SString: "nested oneof in map field value",
- },
- },
- },
- },
- },
- want: `{
- "strToOneofs": {
- "nested": {
- "oneofNested": {
- "sString": "nested oneof in map field value"
- }
- },
- "string": {
- "oneofString": "hello"
- }
- }
-}`,
- }, {
- desc: "map field contains nil value",
- input: &pb3.Maps{
- StrToNested: map[string]*pb3.Nested{
- "nil": nil,
- },
- },
- want: `{
- "strToNested": {
- "nil": {}
- }
-}`,
- }, {
- desc: "required fields not set",
- input: &pb2.Requireds{},
- want: `{}`,
- wantErr: true,
- }, {
- desc: "required fields partially set",
- input: &pb2.Requireds{
- ReqBool: scalar.Bool(false),
- ReqSfixed64: scalar.Int64(0),
- ReqDouble: scalar.Float64(1.23),
- ReqString: scalar.String("hello"),
- ReqEnum: pb2.Enum_ONE.Enum(),
- },
- want: `{
- "reqBool": false,
- "reqSfixed64": "0",
- "reqDouble": 1.23,
- "reqString": "hello",
- "reqEnum": "ONE"
-}`,
- wantErr: true,
- }, {
- desc: "required fields not set with AllowPartial",
- mo: jsonpb.MarshalOptions{AllowPartial: true},
- input: &pb2.Requireds{
- ReqBool: scalar.Bool(false),
- ReqSfixed64: scalar.Int64(0),
- ReqDouble: scalar.Float64(1.23),
- ReqString: scalar.String("hello"),
- ReqEnum: pb2.Enum_ONE.Enum(),
- },
- want: `{
- "reqBool": false,
- "reqSfixed64": "0",
- "reqDouble": 1.23,
- "reqString": "hello",
- "reqEnum": "ONE"
-}`,
- }, {
- desc: "required fields all set",
- input: &pb2.Requireds{
- ReqBool: scalar.Bool(false),
- ReqSfixed64: scalar.Int64(0),
- ReqDouble: scalar.Float64(1.23),
- ReqString: scalar.String("hello"),
- ReqEnum: pb2.Enum_ONE.Enum(),
- ReqNested: &pb2.Nested{},
- },
- want: `{
- "reqBool": false,
- "reqSfixed64": "0",
- "reqDouble": 1.23,
- "reqString": "hello",
- "reqEnum": "ONE",
- "reqNested": {}
-}`,
- }, {
- desc: "indirect required field",
- input: &pb2.IndirectRequired{
- OptNested: &pb2.NestedWithRequired{},
- },
- want: `{
- "optNested": {}
-}`,
- wantErr: true,
- }, {
- desc: "indirect required field with AllowPartial",
- mo: jsonpb.MarshalOptions{AllowPartial: true},
- input: &pb2.IndirectRequired{
- OptNested: &pb2.NestedWithRequired{},
- },
- want: `{
- "optNested": {}
-}`,
- }, {
- desc: "indirect required field in empty repeated",
- input: &pb2.IndirectRequired{
- RptNested: []*pb2.NestedWithRequired{},
- },
- want: `{}`,
- }, {
- desc: "indirect required field in repeated",
- input: &pb2.IndirectRequired{
- RptNested: []*pb2.NestedWithRequired{
- &pb2.NestedWithRequired{},
- },
- },
- want: `{
- "rptNested": [
- {}
- ]
-}`,
- wantErr: true,
- }, {
- desc: "indirect required field in repeated with AllowPartial",
- mo: jsonpb.MarshalOptions{AllowPartial: true},
- input: &pb2.IndirectRequired{
- RptNested: []*pb2.NestedWithRequired{
- &pb2.NestedWithRequired{},
- },
- },
- want: `{
- "rptNested": [
- {}
- ]
-}`,
- }, {
- desc: "indirect required field in empty map",
- input: &pb2.IndirectRequired{
- StrToNested: map[string]*pb2.NestedWithRequired{},
- },
- want: "{}",
- }, {
- desc: "indirect required field in map",
- input: &pb2.IndirectRequired{
- StrToNested: map[string]*pb2.NestedWithRequired{
- "fail": &pb2.NestedWithRequired{},
- },
- },
- want: `{
- "strToNested": {
- "fail": {}
- }
-}`,
- wantErr: true,
- }, {
- desc: "indirect required field in map with AllowPartial",
- mo: jsonpb.MarshalOptions{AllowPartial: true},
- input: &pb2.IndirectRequired{
- StrToNested: map[string]*pb2.NestedWithRequired{
- "fail": &pb2.NestedWithRequired{},
- },
- },
- want: `{
- "strToNested": {
- "fail": {}
- }
-}`,
- }, {
- desc: "indirect required field in oneof",
- input: &pb2.IndirectRequired{
- Union: &pb2.IndirectRequired_OneofNested{
- OneofNested: &pb2.NestedWithRequired{},
- },
- },
- want: `{
- "oneofNested": {}
-}`,
- wantErr: true,
- }, {
- desc: "indirect required field in oneof with AllowPartial",
- mo: jsonpb.MarshalOptions{AllowPartial: true},
- input: &pb2.IndirectRequired{
- Union: &pb2.IndirectRequired_OneofNested{
- OneofNested: &pb2.NestedWithRequired{},
- },
- },
- want: `{
- "oneofNested": {}
-}`,
- }, {
- desc: "unknown fields are ignored",
- input: &pb2.Scalars{
- OptString: scalar.String("no unknowns"),
- XXX_unrecognized: pack.Message{
- pack.Tag{101, pack.BytesType}, pack.String("hello world"),
- }.Marshal(),
- },
- want: `{
- "optString": "no unknowns"
-}`,
- }, {
- desc: "json_name",
- input: &pb3.JSONNames{
- SString: "json_name",
- },
- want: `{
- "foo_bar": "json_name"
-}`,
- }, {
- desc: "extensions of non-repeated fields",
- input: func() proto.Message {
- m := &pb2.Extensions{
- OptString: scalar.String("non-extension field"),
- OptBool: scalar.Bool(true),
- OptInt32: scalar.Int32(42),
- }
- setExtension(m, pb2.E_OptExtBool, true)
- setExtension(m, pb2.E_OptExtString, "extension field")
- setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN)
- setExtension(m, pb2.E_OptExtNested, &pb2.Nested{
- OptString: scalar.String("nested in an extension"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("another nested in an extension"),
- },
- })
- return m
- }(),
- want: `{
- "optString": "non-extension field",
- "optBool": true,
- "optInt32": 42,
- "[pb2.opt_ext_bool]": true,
- "[pb2.opt_ext_enum]": "TEN",
- "[pb2.opt_ext_nested]": {
- "optString": "nested in an extension",
- "optNested": {
- "optString": "another nested in an extension"
- }
- },
- "[pb2.opt_ext_string]": "extension field"
-}`,
- }, {
- desc: "extension message field set to nil",
- input: func() proto.Message {
- m := &pb2.Extensions{}
- setExtension(m, pb2.E_OptExtNested, nil)
- return m
- }(),
- want: "{}",
- }, {
- desc: "extensions of repeated fields",
- input: func() proto.Message {
- m := &pb2.Extensions{}
- setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
- setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
- setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
- &pb2.Nested{OptString: scalar.String("one")},
- &pb2.Nested{OptString: scalar.String("two")},
- &pb2.Nested{OptString: scalar.String("three")},
- })
- return m
- }(),
- want: `{
- "[pb2.rpt_ext_enum]": [
- "TEN",
- 101,
- "ONE"
- ],
- "[pb2.rpt_ext_fixed32]": [
- 42,
- 47
- ],
- "[pb2.rpt_ext_nested]": [
- {
- "optString": "one"
- },
- {
- "optString": "two"
- },
- {
- "optString": "three"
- }
- ]
-}`,
- }, {
- desc: "extensions of non-repeated fields in another message",
- input: func() proto.Message {
- m := &pb2.Extensions{}
- setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
- setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
- setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
- setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
- OptString: scalar.String("nested in an extension"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("another nested in an extension"),
- },
- })
- return m
- }(),
- want: `{
- "[pb2.ExtensionsContainer.opt_ext_bool]": true,
- "[pb2.ExtensionsContainer.opt_ext_enum]": "TEN",
- "[pb2.ExtensionsContainer.opt_ext_nested]": {
- "optString": "nested in an extension",
- "optNested": {
- "optString": "another nested in an extension"
- }
- },
- "[pb2.ExtensionsContainer.opt_ext_string]": "extension field"
-}`,
- }, {
- desc: "extensions of repeated fields in another message",
- input: func() proto.Message {
- m := &pb2.Extensions{
- OptString: scalar.String("non-extension field"),
- OptBool: scalar.Bool(true),
- OptInt32: scalar.Int32(42),
- }
- setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
- setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
- setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
- &pb2.Nested{OptString: scalar.String("one")},
- &pb2.Nested{OptString: scalar.String("two")},
- &pb2.Nested{OptString: scalar.String("three")},
- })
- return m
- }(),
- want: `{
- "optString": "non-extension field",
- "optBool": true,
- "optInt32": 42,
- "[pb2.ExtensionsContainer.rpt_ext_enum]": [
- "TEN",
- 101,
- "ONE"
- ],
- "[pb2.ExtensionsContainer.rpt_ext_nested]": [
- {
- "optString": "one"
- },
- {
- "optString": "two"
- },
- {
- "optString": "three"
- }
- ],
- "[pb2.ExtensionsContainer.rpt_ext_string]": [
- "hello",
- "world"
- ]
-}`,
- }, {
- desc: "MessageSet",
- input: func() proto.Message {
- m := &pb2.MessageSet{}
- setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
- OptString: scalar.String("a messageset extension"),
- })
- setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
- OptString: scalar.String("not a messageset extension"),
- })
- setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
- OptString: scalar.String("just a regular extension"),
- })
- return m
- }(),
- want: `{
- "[pb2.MessageSetExtension]": {
- "optString": "a messageset extension"
- },
- "[pb2.MessageSetExtension.ext_nested]": {
- "optString": "just a regular extension"
- },
- "[pb2.MessageSetExtension.not_message_set_extension]": {
- "optString": "not a messageset extension"
- }
-}`,
- }, {
- desc: "not real MessageSet 1",
- input: func() proto.Message {
- m := &pb2.FakeMessageSet{}
- setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
- OptString: scalar.String("not a messageset extension"),
- })
- return m
- }(),
- want: `{
- "[pb2.FakeMessageSetExtension.message_set_extension]": {
- "optString": "not a messageset extension"
- }
-}`,
- }, {
- desc: "not real MessageSet 2",
- input: func() proto.Message {
- m := &pb2.MessageSet{}
- setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
- OptString: scalar.String("another not a messageset extension"),
- })
- return m
- }(),
- want: `{
- "[pb2.message_set_extension]": {
- "optString": "another not a messageset extension"
- }
-}`,
- }, {
- desc: "BoolValue empty",
- input: &knownpb.BoolValue{},
- want: `false`,
- }, {
- desc: "BoolValue",
- input: &knownpb.BoolValue{Value: true},
- want: `true`,
- }, {
- desc: "Int32Value empty",
- input: &knownpb.Int32Value{},
- want: `0`,
- }, {
- desc: "Int32Value",
- input: &knownpb.Int32Value{Value: 42},
- want: `42`,
- }, {
- desc: "Int64Value",
- input: &knownpb.Int64Value{Value: 42},
- want: `"42"`,
- }, {
- desc: "UInt32Value",
- input: &knownpb.UInt32Value{Value: 42},
- want: `42`,
- }, {
- desc: "UInt64Value",
- input: &knownpb.UInt64Value{Value: 42},
- want: `"42"`,
- }, {
- desc: "FloatValue",
- input: &knownpb.FloatValue{Value: 1.02},
- want: `1.02`,
- }, {
- desc: "FloatValue Infinity",
- input: &knownpb.FloatValue{Value: float32(math.Inf(-1))},
- want: `"-Infinity"`,
- }, {
- desc: "DoubleValue",
- input: &knownpb.DoubleValue{Value: 1.02},
- want: `1.02`,
- }, {
- desc: "DoubleValue NaN",
- input: &knownpb.DoubleValue{Value: math.NaN()},
- want: `"NaN"`,
- }, {
- desc: "StringValue empty",
- input: &knownpb.StringValue{},
- want: `""`,
- }, {
- desc: "StringValue",
- input: &knownpb.StringValue{Value: "è°·æŒ"},
- want: `"è°·æŒ"`,
- }, {
- desc: "StringValue with invalid UTF8 error",
- input: &knownpb.StringValue{Value: "abc\xff"},
- want: "\"abc\xff\"",
- wantErr: true,
- }, {
- desc: "StringValue field with invalid UTF8 error",
- input: &pb2.KnownTypes{
- OptString: &knownpb.StringValue{Value: "abc\xff"},
- },
- want: "{\n \"optString\": \"abc\xff\"\n}",
- wantErr: true,
- }, {
- desc: "BytesValue",
- input: &knownpb.BytesValue{Value: []byte("hello")},
- want: `"aGVsbG8="`,
- }, {
- desc: "Empty",
- input: &knownpb.Empty{},
- want: `{}`,
- }, {
- desc: "NullValue field",
- input: &pb2.KnownTypes{OptNull: new(knownpb.NullValue)},
- want: `{
- "optNull": null
-}`,
- }, {
- desc: "Value empty",
- input: &knownpb.Value{},
- wantErr: true,
- }, {
- desc: "Value empty field",
- input: &pb2.KnownTypes{
- OptValue: &knownpb.Value{},
- },
- wantErr: true,
- }, {
- desc: "Value contains NullValue",
- input: &knownpb.Value{Kind: &knownpb.Value_NullValue{}},
- want: `null`,
- }, {
- desc: "Value contains BoolValue",
- input: &knownpb.Value{Kind: &knownpb.Value_BoolValue{}},
- want: `false`,
- }, {
- desc: "Value contains NumberValue",
- input: &knownpb.Value{Kind: &knownpb.Value_NumberValue{1.02}},
- want: `1.02`,
- }, {
- desc: "Value contains StringValue",
- input: &knownpb.Value{Kind: &knownpb.Value_StringValue{"hello"}},
- want: `"hello"`,
- }, {
- desc: "Value contains StringValue with invalid UTF8",
- input: &knownpb.Value{Kind: &knownpb.Value_StringValue{"\xff"}},
- want: "\"\xff\"",
- wantErr: true,
- }, {
- desc: "Value contains Struct",
- input: &knownpb.Value{
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "null": {Kind: &knownpb.Value_NullValue{}},
- "number": {Kind: &knownpb.Value_NumberValue{}},
- "string": {Kind: &knownpb.Value_StringValue{}},
- "struct": {Kind: &knownpb.Value_StructValue{}},
- "list": {Kind: &knownpb.Value_ListValue{}},
- "bool": {Kind: &knownpb.Value_BoolValue{}},
- },
- },
- },
- },
- want: `{
- "bool": false,
- "list": [],
- "null": null,
- "number": 0,
- "string": "",
- "struct": {}
-}`,
- }, {
- desc: "Value contains ListValue",
- input: &knownpb.Value{
- Kind: &knownpb.Value_ListValue{
- &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_BoolValue{}},
- {Kind: &knownpb.Value_NullValue{}},
- {Kind: &knownpb.Value_NumberValue{}},
- {Kind: &knownpb.Value_StringValue{}},
- {Kind: &knownpb.Value_StructValue{}},
- {Kind: &knownpb.Value_ListValue{}},
- },
- },
- },
- },
- want: `[
- false,
- null,
- 0,
- "",
- {},
- []
-]`,
- }, {
- desc: "Struct with nil map",
- input: &knownpb.Struct{},
- want: `{}`,
- }, {
- desc: "Struct with empty map",
- input: &knownpb.Struct{
- Fields: map[string]*knownpb.Value{},
- },
- want: `{}`,
- }, {
- desc: "Struct",
- input: &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "bool": {Kind: &knownpb.Value_BoolValue{true}},
- "null": {Kind: &knownpb.Value_NullValue{}},
- "number": {Kind: &knownpb.Value_NumberValue{3.1415}},
- "string": {Kind: &knownpb.Value_StringValue{"hello"}},
- "struct": {
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"world"}},
- },
- },
- },
- },
- "list": {
- Kind: &knownpb.Value_ListValue{
- &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_BoolValue{}},
- {Kind: &knownpb.Value_NullValue{}},
- {Kind: &knownpb.Value_NumberValue{}},
- },
- },
- },
- },
- },
- },
- want: `{
- "bool": true,
- "list": [
- false,
- null,
- 0
- ],
- "null": null,
- "number": 3.1415,
- "string": "hello",
- "struct": {
- "string": "world"
- }
-}`,
- }, {
- desc: "Struct message with invalid UTF8 string",
- input: &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"\xff"}},
- },
- },
- want: "{\n \"string\": \"\xff\"\n}",
- wantErr: true,
- }, {
- desc: "ListValue with nil values",
- input: &knownpb.ListValue{},
- want: `[]`,
- }, {
- desc: "ListValue with empty values",
- input: &knownpb.ListValue{
- Values: []*knownpb.Value{},
- },
- want: `[]`,
- }, {
- desc: "ListValue",
- input: &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_BoolValue{true}},
- {Kind: &knownpb.Value_NullValue{}},
- {Kind: &knownpb.Value_NumberValue{3.1415}},
- {Kind: &knownpb.Value_StringValue{"hello"}},
- {
- Kind: &knownpb.Value_ListValue{
- &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_BoolValue{}},
- {Kind: &knownpb.Value_NullValue{}},
- {Kind: &knownpb.Value_NumberValue{}},
- },
- },
- },
- },
- {
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"world"}},
- },
- },
- },
- },
- },
- },
- want: `[
- true,
- null,
- 3.1415,
- "hello",
- [
- false,
- null,
- 0
- ],
- {
- "string": "world"
- }
-]`,
- }, {
- desc: "ListValue with invalid UTF8 string",
- input: &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_StringValue{"\xff"}},
- },
- },
- want: "[\n \"\xff\"\n]",
- wantErr: true,
- }, {
- desc: "Duration empty",
- input: &knownpb.Duration{},
- want: `"0s"`,
- }, {
- desc: "Duration with secs",
- input: &knownpb.Duration{Seconds: 3},
- want: `"3s"`,
- }, {
- desc: "Duration with -secs",
- input: &knownpb.Duration{Seconds: -3},
- want: `"-3s"`,
- }, {
- desc: "Duration with nanos",
- input: &knownpb.Duration{Nanos: 1e6},
- want: `"0.001s"`,
- }, {
- desc: "Duration with -nanos",
- input: &knownpb.Duration{Nanos: -1e6},
- want: `"-0.001s"`,
- }, {
- desc: "Duration with large secs",
- input: &knownpb.Duration{Seconds: 1e10, Nanos: 1},
- want: `"10000000000.000000001s"`,
- }, {
- desc: "Duration with 6-digit nanos",
- input: &knownpb.Duration{Nanos: 1e4},
- want: `"0.000010s"`,
- }, {
- desc: "Duration with 3-digit nanos",
- input: &knownpb.Duration{Nanos: 1e6},
- want: `"0.001s"`,
- }, {
- desc: "Duration with -secs -nanos",
- input: &knownpb.Duration{Seconds: -123, Nanos: -450},
- want: `"-123.000000450s"`,
- }, {
- desc: "Duration max value",
- input: &knownpb.Duration{Seconds: 315576000000, Nanos: 999999999},
- want: `"315576000000.999999999s"`,
- }, {
- desc: "Duration min value",
- input: &knownpb.Duration{Seconds: -315576000000, Nanos: -999999999},
- want: `"-315576000000.999999999s"`,
- }, {
- desc: "Duration with +secs -nanos",
- input: &knownpb.Duration{Seconds: 1, Nanos: -1},
- wantErr: true,
- }, {
- desc: "Duration with -secs +nanos",
- input: &knownpb.Duration{Seconds: -1, Nanos: 1},
- wantErr: true,
- }, {
- desc: "Duration with +secs out of range",
- input: &knownpb.Duration{Seconds: 315576000001},
- wantErr: true,
- }, {
- desc: "Duration with -secs out of range",
- input: &knownpb.Duration{Seconds: -315576000001},
- wantErr: true,
- }, {
- desc: "Duration with +nanos out of range",
- input: &knownpb.Duration{Seconds: 0, Nanos: 1e9},
- wantErr: true,
- }, {
- desc: "Duration with -nanos out of range",
- input: &knownpb.Duration{Seconds: 0, Nanos: -1e9},
- wantErr: true,
- }, {
- desc: "Timestamp zero",
- input: &knownpb.Timestamp{},
- want: `"1970-01-01T00:00:00Z"`,
- }, {
- desc: "Timestamp",
- input: &knownpb.Timestamp{Seconds: 1553036601},
- want: `"2019-03-19T23:03:21Z"`,
- }, {
- desc: "Timestamp with nanos",
- input: &knownpb.Timestamp{Seconds: 1553036601, Nanos: 1},
- want: `"2019-03-19T23:03:21.000000001Z"`,
- }, {
- desc: "Timestamp with 6-digit nanos",
- input: &knownpb.Timestamp{Nanos: 1e3},
- want: `"1970-01-01T00:00:00.000001Z"`,
- }, {
- desc: "Timestamp with 3-digit nanos",
- input: &knownpb.Timestamp{Nanos: 1e7},
- want: `"1970-01-01T00:00:00.010Z"`,
- }, {
- desc: "Timestamp max value",
- input: &knownpb.Timestamp{Seconds: 253402300799, Nanos: 999999999},
- want: `"9999-12-31T23:59:59.999999999Z"`,
- }, {
- desc: "Timestamp min value",
- input: &knownpb.Timestamp{Seconds: -62135596800},
- want: `"0001-01-01T00:00:00Z"`,
- }, {
- desc: "Timestamp with +secs out of range",
- input: &knownpb.Timestamp{Seconds: 253402300800},
- wantErr: true,
- }, {
- desc: "Timestamp with -secs out of range",
- input: &knownpb.Timestamp{Seconds: -62135596801},
- wantErr: true,
- }, {
- desc: "Timestamp with -nanos",
- input: &knownpb.Timestamp{Nanos: -1},
- wantErr: true,
- }, {
- desc: "Timestamp with +nanos out of range",
- input: &knownpb.Timestamp{Nanos: 1e9},
- wantErr: true,
- }, {
- desc: "FieldMask empty",
- input: &knownpb.FieldMask{},
- want: `""`,
- }, {
- desc: "FieldMask",
- input: &knownpb.FieldMask{
- Paths: []string{
- "foo",
- "foo_bar",
- "foo.bar_qux",
- "_foo",
- },
- },
- want: `"foo,fooBar,foo.barQux,Foo"`,
- }, {
- desc: "FieldMask error 1",
- input: &knownpb.FieldMask{
- Paths: []string{"foo_"},
- },
- wantErr: true,
- }, {
- desc: "FieldMask error 2",
- input: &knownpb.FieldMask{
- Paths: []string{"foo__bar"},
- },
- wantErr: true,
- }, {
- desc: "Any empty",
- input: &knownpb.Any{},
- want: `{}`,
- }, {
- desc: "Any with non-custom message",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
- },
- input: func() proto.Message {
- m := &pb2.Nested{
- OptString: scalar.String("embedded inside Any"),
- OptNested: &pb2.Nested{
- OptString: scalar.String("inception"),
- },
- }
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "foo/pb2.Nested",
- Value: b,
- }
- }(),
- want: `{
- "@type": "foo/pb2.Nested",
- "optString": "embedded inside Any",
- "optNested": {
- "optString": "inception"
- }
-}`,
- }, {
- desc: "Any with empty embedded message",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
- },
- input: &knownpb.Any{TypeUrl: "foo/pb2.Nested"},
- want: `{
- "@type": "foo/pb2.Nested"
-}`,
- }, {
- desc: "Any without registered type",
- mo: jsonpb.MarshalOptions{Resolver: preg.NewTypes()},
- input: &knownpb.Any{TypeUrl: "foo/pb2.Nested"},
- wantErr: true,
- }, {
- desc: "Any with missing required error",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
- },
- input: func() proto.Message {
- m := &pb2.PartialRequired{
- OptString: scalar.String("embedded inside Any"),
- }
- b, err := proto.MarshalOptions{
- AllowPartial: true,
- Deterministic: true,
- }.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
- Value: b,
- }
- }(),
- want: `{
- "@type": "pb2.PartialRequired",
- "optString": "embedded inside Any"
-}`,
- wantErr: true,
- }, {
- desc: "Any with partial required and AllowPartial",
- mo: jsonpb.MarshalOptions{
- AllowPartial: true,
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
- },
- input: func() proto.Message {
- m := &pb2.PartialRequired{
- OptString: scalar.String("embedded inside Any"),
- }
- b, err := proto.MarshalOptions{
- AllowPartial: true,
- Deterministic: true,
- }.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
- Value: b,
- }
- }(),
- want: `{
- "@type": "pb2.PartialRequired",
- "optString": "embedded inside Any"
-}`,
- }, {
- desc: "Any with invalid UTF8",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
- },
- input: func() proto.Message {
- m := &pb2.Nested{
- OptString: scalar.String("abc\xff"),
- }
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "foo/pb2.Nested",
- Value: b,
- }
- }(),
- want: `{
- "@type": "foo/pb2.Nested",
- "optString": "` + "abc\xff" + `"
-}`,
- wantErr: true,
- }, {
- desc: "Any with invalid value",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
- },
- input: &knownpb.Any{
- TypeUrl: "foo/pb2.Nested",
- Value: dhex("80"),
- },
- wantErr: true,
- }, {
- desc: "Any with BoolValue",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.BoolValue{})),
- },
- input: func() proto.Message {
- m := &knownpb.BoolValue{Value: true}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.BoolValue",
- Value: b,
- }
- }(),
- want: `{
- "@type": "type.googleapis.com/google.protobuf.BoolValue",
- "value": true
-}`,
- }, {
- desc: "Any with Empty",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Empty{})),
- },
- input: func() proto.Message {
- m := &knownpb.Empty{}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.Empty",
- Value: b,
- }
- }(),
- want: `{
- "@type": "type.googleapis.com/google.protobuf.Empty",
- "value": {}
-}`,
- }, {
- desc: "Any with StringValue containing invalid UTF8",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.StringValue{})),
- },
- input: func() proto.Message {
- m := &knownpb.StringValue{Value: "abcd"}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "google.protobuf.StringValue",
- Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
- }
- }(),
- want: `{
- "@type": "google.protobuf.StringValue",
- "value": "` + "abc\xff" + `"
-}`,
- wantErr: true,
- }, {
- desc: "Any with Int64Value",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Int64Value{})),
- },
- input: func() proto.Message {
- m := &knownpb.Int64Value{Value: 42}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "google.protobuf.Int64Value",
- Value: b,
- }
- }(),
- want: `{
- "@type": "google.protobuf.Int64Value",
- "value": "42"
-}`,
- }, {
- desc: "Any with Duration",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Duration{})),
- },
- input: func() proto.Message {
- m := &knownpb.Duration{}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.Duration",
- Value: b,
- }
- }(),
- want: `{
- "@type": "type.googleapis.com/google.protobuf.Duration",
- "value": "0s"
-}`,
- }, {
- desc: "Any with empty Value",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Value{})),
- },
- input: func() proto.Message {
- m := &knownpb.Value{}
- b, err := proto.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.Value",
- Value: b,
- }
- }(),
- wantErr: true,
- }, {
- desc: "Any with Value of StringValue",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Value{})),
- },
- input: func() proto.Message {
- m := &knownpb.Value{Kind: &knownpb.Value_StringValue{"abcd"}}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.Value",
- Value: bytes.Replace(b, []byte("abcd"), []byte("abc\xff"), -1),
- }
- }(),
- want: `{
- "@type": "type.googleapis.com/google.protobuf.Value",
- "value": "` + "abc\xff" + `"
-}`,
- wantErr: true,
- }, {
- desc: "Any with Value of NullValue",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Value{})),
- },
- input: func() proto.Message {
- m := &knownpb.Value{Kind: &knownpb.Value_NullValue{}}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "type.googleapis.com/google.protobuf.Value",
- Value: b,
- }
- }(),
- want: `{
- "@type": "type.googleapis.com/google.protobuf.Value",
- "value": null
-}`,
- }, {
- desc: "Any with Struct",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(
- pimpl.Export{}.MessageTypeOf(&knownpb.Struct{}),
- pimpl.Export{}.MessageTypeOf(&knownpb.Value{}),
- pimpl.Export{}.MessageTypeOf(&knownpb.BoolValue{}),
- pimpl.Export{}.EnumTypeOf(knownpb.NullValue_NULL_VALUE),
- pimpl.Export{}.MessageTypeOf(&knownpb.StringValue{}),
- ),
- },
- input: func() proto.Message {
- m := &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "bool": {Kind: &knownpb.Value_BoolValue{true}},
- "null": {Kind: &knownpb.Value_NullValue{}},
- "string": {Kind: &knownpb.Value_StringValue{"hello"}},
- "struct": {
- Kind: &knownpb.Value_StructValue{
- &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"world"}},
- },
- },
- },
- },
- },
- }
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- TypeUrl: "google.protobuf.Struct",
- Value: b,
- }
- }(),
- want: `{
- "@type": "google.protobuf.Struct",
- "value": {
- "bool": true,
- "null": null,
- "string": "hello",
- "struct": {
- "string": "world"
- }
- }
-}`,
- }, {
- desc: "Any with missing type_url",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.BoolValue{})),
- },
- input: func() proto.Message {
- m := &knownpb.BoolValue{Value: true}
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if err != nil {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &knownpb.Any{
- Value: b,
- }
- }(),
- wantErr: true,
- }, {
- desc: "well known types as field values",
- mo: jsonpb.MarshalOptions{
- Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&knownpb.Empty{})),
- },
- input: &pb2.KnownTypes{
- OptBool: &knownpb.BoolValue{Value: false},
- OptInt32: &knownpb.Int32Value{Value: 42},
- OptInt64: &knownpb.Int64Value{Value: 42},
- OptUint32: &knownpb.UInt32Value{Value: 42},
- OptUint64: &knownpb.UInt64Value{Value: 42},
- OptFloat: &knownpb.FloatValue{Value: 1.23},
- OptDouble: &knownpb.DoubleValue{Value: 3.1415},
- OptString: &knownpb.StringValue{Value: "hello"},
- OptBytes: &knownpb.BytesValue{Value: []byte("hello")},
- OptDuration: &knownpb.Duration{Seconds: 123},
- OptTimestamp: &knownpb.Timestamp{Seconds: 1553036601},
- OptStruct: &knownpb.Struct{
- Fields: map[string]*knownpb.Value{
- "string": {Kind: &knownpb.Value_StringValue{"hello"}},
- },
- },
- OptList: &knownpb.ListValue{
- Values: []*knownpb.Value{
- {Kind: &knownpb.Value_NullValue{}},
- {Kind: &knownpb.Value_StringValue{}},
- {Kind: &knownpb.Value_StructValue{}},
- {Kind: &knownpb.Value_ListValue{}},
- },
- },
- OptValue: &knownpb.Value{
- Kind: &knownpb.Value_StringValue{"world"},
- },
- OptEmpty: &knownpb.Empty{},
- OptAny: &knownpb.Any{
- TypeUrl: "google.protobuf.Empty",
- },
- OptFieldmask: &knownpb.FieldMask{
- Paths: []string{"foo_bar", "bar_foo"},
- },
- },
- want: `{
- "optBool": false,
- "optInt32": 42,
- "optInt64": "42",
- "optUint32": 42,
- "optUint64": "42",
- "optFloat": 1.23,
- "optDouble": 3.1415,
- "optString": "hello",
- "optBytes": "aGVsbG8=",
- "optDuration": "123s",
- "optTimestamp": "2019-03-19T23:03:21Z",
- "optStruct": {
- "string": "hello"
- },
- "optList": [
- null,
- "",
- {},
- []
- ],
- "optValue": "world",
- "optEmpty": {},
- "optAny": {
- "@type": "google.protobuf.Empty",
- "value": {}
- },
- "optFieldmask": "fooBar,barFoo"
-}`,
- }}
-
- for _, tt := range tests {
- tt := tt
- t.Run(tt.desc, func(t *testing.T) {
- // Use 2-space indentation on all MarshalOptions.
- tt.mo.Indent = " "
- b, err := tt.mo.Marshal(tt.input)
- if err != nil && !tt.wantErr {
- t.Errorf("Marshal() returned error: %v\n", err)
- }
- if err == nil && tt.wantErr {
- t.Errorf("Marshal() got nil error, want error\n")
- }
- got := string(b)
- if got != tt.want {
- t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
- if diff := cmp.Diff(tt.want, got, splitLines); diff != "" {
- t.Errorf("Marshal() diff -want +got\n%v\n", diff)
- }
- }
- })
- }
-}
diff --git a/encoding/jsonpb/jsonpb.go b/encoding/jsonpb/jsonpb.go
new file mode 100644
index 0000000..43c28b5
--- /dev/null
+++ b/encoding/jsonpb/jsonpb.go
@@ -0,0 +1,14 @@
+// Package jsonpb is deprecated.
+package jsonpb
+
+import "google.golang.org/protobuf/encoding/protojson"
+
+var (
+ Marshal = protojson.Marshal
+ Unmarshal = protojson.Unmarshal
+)
+
+type (
+ MarshalOptions = protojson.MarshalOptions
+ UnmarshalOptions = protojson.UnmarshalOptions
+)
diff --git a/encoding/jsonpb/well_known_types.go b/encoding/jsonpb/well_known_types.go
deleted file mode 100644
index c3ca47e..0000000
--- a/encoding/jsonpb/well_known_types.go
+++ /dev/null
@@ -1,985 +0,0 @@
-// Copyright 2019 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package jsonpb
-
-import (
- "bytes"
- "fmt"
- "strconv"
- "strings"
- "time"
-
- "google.golang.org/protobuf/internal/encoding/json"
- "google.golang.org/protobuf/internal/errors"
- "google.golang.org/protobuf/internal/fieldnum"
- "google.golang.org/protobuf/proto"
- pref "google.golang.org/protobuf/reflect/protoreflect"
-)
-
-// isCustomType returns true if type name has special JSON conversion rules.
-// The list of custom types here has to match the ones in marshalCustomType and
-// unmarshalCustomType.
-func isCustomType(name pref.FullName) bool {
- switch name {
- case "google.protobuf.Any",
- "google.protobuf.BoolValue",
- "google.protobuf.DoubleValue",
- "google.protobuf.FloatValue",
- "google.protobuf.Int32Value",
- "google.protobuf.Int64Value",
- "google.protobuf.UInt32Value",
- "google.protobuf.UInt64Value",
- "google.protobuf.StringValue",
- "google.protobuf.BytesValue",
- "google.protobuf.Empty",
- "google.protobuf.Struct",
- "google.protobuf.ListValue",
- "google.protobuf.Value",
- "google.protobuf.Duration",
- "google.protobuf.Timestamp",
- "google.protobuf.FieldMask":
- return true
- }
- return false
-}
-
-// 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 (o MarshalOptions) marshalCustomType(m pref.Message) error {
- name := m.Descriptor().FullName()
- switch name {
- case "google.protobuf.Any":
- return o.marshalAny(m)
-
- case "google.protobuf.BoolValue",
- "google.protobuf.DoubleValue",
- "google.protobuf.FloatValue",
- "google.protobuf.Int32Value",
- "google.protobuf.Int64Value",
- "google.protobuf.UInt32Value",
- "google.protobuf.UInt64Value",
- "google.protobuf.StringValue",
- "google.protobuf.BytesValue":
- return o.marshalWrapperType(m)
-
- case "google.protobuf.Empty":
- return o.marshalEmpty(m)
-
- case "google.protobuf.Struct":
- return o.marshalStruct(m)
-
- case "google.protobuf.ListValue":
- return o.marshalListValue(m)
-
- case "google.protobuf.Value":
- return o.marshalKnownValue(m)
-
- case "google.protobuf.Duration":
- return o.marshalDuration(m)
-
- case "google.protobuf.Timestamp":
- return o.marshalTimestamp(m)
-
- case "google.protobuf.FieldMask":
- return o.marshalFieldMask(m)
- }
-
- panic(fmt.Sprintf("%q does not have a custom marshaler", name))
-}
-
-// 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 (o UnmarshalOptions) unmarshalCustomType(m pref.Message) error {
- name := m.Descriptor().FullName()
- switch name {
- case "google.protobuf.Any":
- return o.unmarshalAny(m)
-
- case "google.protobuf.BoolValue",
- "google.protobuf.DoubleValue",
- "google.protobuf.FloatValue",
- "google.protobuf.Int32Value",
- "google.protobuf.Int64Value",
- "google.protobuf.UInt32Value",
- "google.protobuf.UInt64Value",
- "google.protobuf.StringValue",
- "google.protobuf.BytesValue":
- return o.unmarshalWrapperType(m)
-
- case "google.protobuf.Empty":
- return o.unmarshalEmpty(m)
-
- case "google.protobuf.Struct":
- return o.unmarshalStruct(m)
-
- case "google.protobuf.ListValue":
- return o.unmarshalListValue(m)
-
- case "google.protobuf.Value":
- return o.unmarshalKnownValue(m)
-
- case "google.protobuf.Duration":
- return o.unmarshalDuration(m)
-
- case "google.protobuf.Timestamp":
- return o.unmarshalTimestamp(m)
-
- case "google.protobuf.FieldMask":
- return o.unmarshalFieldMask(m)
- }
-
- panic(fmt.Sprintf("%q does not have a custom unmarshaler", name))
-}
-
-// The JSON representation of an Any message uses the regular representation of
-// the deserialized, embedded message, with an additional field `@type` which
-// contains the type URL. If the embedded message type is well-known and has a
-// custom JSON representation, that representation will be embedded adding a
-// field `value` which holds the custom JSON in addition to the `@type` field.
-
-func (o MarshalOptions) marshalAny(m pref.Message) error {
- messageDesc := m.Descriptor()
- knownFields := m.KnownFields()
-
- // Start writing the JSON object.
- o.encoder.StartObject()
- defer o.encoder.EndObject()
-
- if !knownFields.Has(fieldnum.Any_TypeUrl) {
- if !knownFields.Has(fieldnum.Any_Value) {
- // If message is empty, marshal out empty JSON object.
- return nil
- } else {
- // Return error if type_url field is not set, but value is set.
- return errors.New("%s: type_url is not set", messageDesc.FullName())
- }
- }
-
- typeVal := knownFields.Get(fieldnum.Any_TypeUrl)
- valueVal := knownFields.Get(fieldnum.Any_Value)
-
- // Marshal out @type field.
- typeURL := typeVal.String()
- o.encoder.WriteName("@type")
- var nerr errors.NonFatal
- if err := o.encoder.WriteString(typeURL); !nerr.Merge(err) {
- return err
- }
-
- // Resolve the type in order to unmarshal value field.
- emt, err := o.Resolver.FindMessageByURL(typeURL)
- if !nerr.Merge(err) {
- return errors.New("%s: unable to resolve %q: %v", messageDesc.FullName(), typeURL, err)
- }
-
- em := emt.New()
- // TODO: Need to set types registry in binary unmarshaling.
- // TODO: If binary unmarshaling returns required not set error, need to
- // return another required not set error that contains both the path to this
- // field and the path inside the embedded message.
- err = proto.UnmarshalOptions{
- AllowPartial: o.AllowPartial,
- }.Unmarshal(valueVal.Bytes(), em.Interface())
- if !nerr.Merge(err) {
- return errors.New("%s: unable to unmarshal %q: %v", messageDesc.FullName(), typeURL, err)
- }
-
- // If type of value has custom JSON encoding, marshal out a field "value"
- // with corresponding custom JSON encoding of the embedded message as a
- // field.
- if isCustomType(emt.Descriptor().FullName()) {
- o.encoder.WriteName("value")
- return o.marshalCustomType(em)
- }
-
- // Else, marshal out the embedded message's fields in this Any object.
- if err := o.marshalFields(em); !nerr.Merge(err) {
- return err
- }
-
- return nerr.E
-}
-
-func (o UnmarshalOptions) unmarshalAny(m pref.Message) error {
- // Use Peek to check for json.StartObject to avoid advancing a read.
- if o.decoder.Peek() != json.StartObject {
- jval, _ := o.decoder.Read()
- return unexpectedJSONError{jval}
- }
-
- // Use another json.Decoder to parse the unread bytes from o.decoder for
- // @type field. This avoids advancing a read from o.decoder because the
- // current JSON object may contain the fields of the embedded type.
- dec := o.decoder.Clone()
- typeURL, err := findTypeURL(dec)
- if err == errEmptyObject {
- // An empty JSON object translates to an empty Any message.
- o.decoder.Read() // Read json.StartObject.
- o.decoder.Read() // Read json.EndObject.
- return nil
- }
- if o.DiscardUnknown && err == errMissingType {
- // Treat all fields as unknowns, similar to an empty object.
- return skipJSONValue(o.decoder)
- }
- var nerr errors.NonFatal
- if !nerr.Merge(err) {
- return errors.New("google.protobuf.Any: %v", err)
- }
-
- emt, err := o.Resolver.FindMessageByURL(typeURL)
- if err != nil {
- return errors.New("google.protobuf.Any: unable to resolve type %q: %v", typeURL, err)
- }
-
- // Create new message for the embedded message type and unmarshal into it.
- em := emt.New()
- if isCustomType(emt.Descriptor().FullName()) {
- // If embedded message is a custom type, unmarshal the JSON "value" field
- // into it.
- if err := o.unmarshalAnyValue(em); !nerr.Merge(err) {
- return errors.New("google.protobuf.Any: %v", err)
- }
- } else {
- // Else unmarshal the current JSON object into it.
- if err := o.unmarshalMessage(em, true); !nerr.Merge(err) {
- return errors.New("google.protobuf.Any: %v", err)
- }
- }
- // Serialize the embedded message and assign the resulting bytes to the
- // proto value field.
- // TODO: If binary marshaling returns required not set error, need to return
- // another required not set error that contains both the path to this field
- // and the path inside the embedded message.
- b, err := proto.MarshalOptions{
- AllowPartial: o.AllowPartial,
- Deterministic: true,
- }.Marshal(em.Interface())
- if !nerr.Merge(err) {
- return errors.New("google.protobuf.Any: %v", err)
- }
-
- knownFields := m.KnownFields()
- knownFields.Set(fieldnum.Any_TypeUrl, pref.ValueOf(typeURL))
- knownFields.Set(fieldnum.Any_Value, pref.ValueOf(b))
- return nerr.E
-}
-
-var errEmptyObject = errors.New(`empty object`)
-var errMissingType = errors.New(`missing "@type" field`)
-
-// findTypeURL returns the "@type" field value from the given JSON bytes. It is
-// expected that the given bytes start with json.StartObject. It returns
-// errEmptyObject if the JSON object is empty. It returns error if the object
-// does not contain the field or other decoding problems.
-func findTypeURL(dec *json.Decoder) (string, error) {
- var typeURL string
- var nerr errors.NonFatal
- numFields := 0
- // Skip start object.
- dec.Read()
-
-Loop:
- for {
- jval, err := dec.Read()
- if !nerr.Merge(err) {
- return "", err
- }
-
- switch jval.Type() {
- case json.EndObject:
- if typeURL == "" {
- // Did not find @type field.
- if numFields > 0 {
- return "", errMissingType
- }
- return "", errEmptyObject
- }
- break Loop
-
- case json.Name:
- numFields++
- name, err := jval.Name()
- if !nerr.Merge(err) {
- return "", err
- }
- if name != "@type" {
- // Skip value.
- if err := skipJSONValue(dec); !nerr.Merge(err) {
- return "", err
- }
- continue
- }
-
- // Return error if this was previously set already.
- if typeURL != "" {
- return "", errors.New(`duplicate "@type" field`)
- }
- // Read field value.
- jval, err := dec.Read()
- if !nerr.Merge(err) {
- return "", err
- }
- if jval.Type() != json.String {
- return "", unexpectedJSONError{jval}
- }
- typeURL = jval.String()
- if typeURL == "" {
- return "", errors.New(`"@type" field contains empty value`)
- }
- }
- }
-
- return typeURL, nerr.E
-}
-
-// skipJSONValue makes the given decoder parse a JSON value (null, boolean,
-// string, number, object and array) in order to advance the read to the next
-// JSON value. It relies on Decoder.Read returning an error if the types are
-// not in valid sequence.
-func skipJSONValue(dec *json.Decoder) error {
- var nerr errors.NonFatal
- jval, err := dec.Read()
- if !nerr.Merge(err) {
- return err
- }
- // Only need to continue reading for objects and arrays.
- switch jval.Type() {
- case json.StartObject:
- for {
- jval, err := dec.Read()
- if !nerr.Merge(err) {
- return err
- }
- switch jval.Type() {
- case json.EndObject:
- return nil
- case json.Name:
- // Skip object field value.
- if err := skipJSONValue(dec); !nerr.Merge(err) {
- return err
- }
- }
- }
-
- case json.StartArray:
- for {
- switch dec.Peek() {
- case json.EndArray:
- dec.Read()
- return nil
- case json.Invalid:
- _, err := dec.Read()
- return err
- default:
- // Skip array item.
- if err := skipJSONValue(dec); !nerr.Merge(err) {
- return err
- }
- }
- }
- }
- return nerr.E
-}
-
-// unmarshalAnyValue unmarshals the given custom-type message from the JSON
-// object's "value" field.
-func (o UnmarshalOptions) unmarshalAnyValue(m pref.Message) error {
- var nerr errors.NonFatal
- // Skip StartObject, and start reading the fields.
- o.decoder.Read()
-
- var found bool // Used for detecting duplicate "value".
- for {
- jval, err := o.decoder.Read()
- if !nerr.Merge(err) {
- return err
- }
- switch jval.Type() {
- case json.EndObject:
- if !found {
- return errors.New(`missing "value" field`)
- }
- return nerr.E
-
- case json.Name:
- name, err := jval.Name()
- if !nerr.Merge(err) {
- return err
- }
- switch name {
- default:
- if o.DiscardUnknown {
- if err := skipJSONValue(o.decoder); !nerr.Merge(err) {
- return err
- }
- continue
- }
- return errors.New("unknown field %q", name)
-
- case "@type":
- // Skip the value as this was previously parsed already.
- o.decoder.Read()
-
- case "value":
- if found {
- return errors.New(`duplicate "value" field`)
- }
- // Unmarshal the field value into the given message.
- if err := o.unmarshalCustomType(m); !nerr.Merge(err) {
- return err
- }
- found = true
- }
- }
- }
-}
-
-// Wrapper types are encoded as JSON primitives like string, number or boolean.
-
-// The "value" field has the same field number for all wrapper types.
-const wrapperFieldNumber = fieldnum.BoolValue_Value
-
-func (o MarshalOptions) marshalWrapperType(m pref.Message) error {
- fd := m.Descriptor().Fields().ByNumber(wrapperFieldNumber)
- val := m.KnownFields().Get(wrapperFieldNumber)
- return o.marshalSingular(val, fd)
-}
-
-func (o UnmarshalOptions) unmarshalWrapperType(m pref.Message) error {
- fd := m.Descriptor().Fields().ByNumber(wrapperFieldNumber)
- val, err := o.unmarshalScalar(fd)
- var nerr errors.NonFatal
- if !nerr.Merge(err) {
- return err
- }
- m.KnownFields().Set(wrapperFieldNumber, val)
- return nerr.E
-}
-
-// The JSON representation for Empty is an empty JSON object.
-
-func (o MarshalOptions) marshalEmpty(pref.Message) error {
- o.encoder.StartObject()
- o.encoder.EndObject()
- return nil
-}
-
-func (o UnmarshalOptions) unmarshalEmpty(pref.Message) error {
- var nerr errors.NonFatal
- jval, err := o.decoder.Read()
- if err != nil {
- return err
- }
- if jval.Type() != json.StartObject {
- return unexpectedJSONError{jval}
- }
-
- for {
- jval, err := o.decoder.Read()
- if !nerr.Merge(err) {
- return err
- }
- switch jval.Type() {
- case json.EndObject:
- return nerr.E
-
- case json.Name:
- if o.DiscardUnknown {
- if err := skipJSONValue(o.decoder); !nerr.Merge(err) {
- return err
- }
- continue
- }
- name, _ := jval.Name()
- return errors.New("unknown field %q", name)
-
- default:
- return unexpectedJSONError{jval}
- }
- }
-}
-
-// 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 (o MarshalOptions) marshalStruct(m pref.Message) error {
- fd := m.Descriptor().Fields().ByNumber(fieldnum.Struct_Fields)
- val := m.KnownFields().Get(fieldnum.Struct_Fields)
- return o.marshalMap(val.Map(), fd)
-}
-
-func (o UnmarshalOptions) unmarshalStruct(m pref.Message) error {
- fd := m.Descriptor().Fields().ByNumber(fieldnum.Struct_Fields)
- val := m.KnownFields().Get(fieldnum.Struct_Fields)
- 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 (o MarshalOptions) marshalListValue(m pref.Message) error {
- fd := m.Descriptor().Fields().ByNumber(fieldnum.ListValue_Values)
- val := m.KnownFields().Get(fieldnum.ListValue_Values)
- return o.marshalList(val.List(), fd)
-}
-
-func (o UnmarshalOptions) unmarshalListValue(m pref.Message) error {
- fd := m.Descriptor().Fields().ByNumber(fieldnum.ListValue_Values)
- val := m.KnownFields().Get(fieldnum.ListValue_Values)
- 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 (o MarshalOptions) marshalKnownValue(m pref.Message) error {
- messageDesc := m.Descriptor()
- knownFields := m.KnownFields()
- num := knownFields.WhichOneof("kind")
- if num == 0 {
- // Return error if none of the fields is set.
- return errors.New("%s: none of the oneof fields is set", messageDesc.FullName())
- }
-
- fd := messageDesc.Fields().ByNumber(num)
- val := knownFields.Get(num)
- return o.marshalSingular(val, fd)
-}
-
-func (o UnmarshalOptions) unmarshalKnownValue(m pref.Message) error {
- var nerr errors.NonFatal
- knownFields := m.KnownFields()
-
- switch o.decoder.Peek() {
- case json.Null:
- o.decoder.Read()
- knownFields.Set(fieldnum.Value_NullValue, pref.ValueOf(pref.EnumNumber(0)))
-
- case json.Bool:
- jval, err := o.decoder.Read()
- if err != nil {
- return err
- }
- val, err := unmarshalBool(jval)
- if err != nil {
- return err
- }
- knownFields.Set(fieldnum.Value_BoolValue, val)
-
- case json.Number:
- jval, err := o.decoder.Read()
- if err != nil {
- return err
- }
- val, err := unmarshalFloat(jval, 64)
- if err != nil {
- return err
- }
- knownFields.Set(fieldnum.Value_NumberValue, val)
-
- case json.String:
- // A JSON string may have been encoded from the number_value field,
- // e.g. "NaN", "Infinity", etc. Parsing a proto double type also allows
- // for it to be in JSON string form. Given this custom encoding spec,
- // 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 := o.decoder.Read()
- if !nerr.Merge(err) {
- return err
- }
- val, err := unmarshalString(jval)
- if !nerr.Merge(err) {
- return err
- }
- knownFields.Set(fieldnum.Value_StringValue, val)
-
- case json.StartObject:
- m := knownFields.NewMessage(fieldnum.Value_StructValue)
- 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 := o.unmarshalListValue(m); !nerr.Merge(err) {
- return err
- }
- knownFields.Set(fieldnum.Value_ListValue, pref.ValueOf(m))
-
- default:
- jval, err := o.decoder.Read()
- if err != nil {
- return err
- }
- return unexpectedJSONError{jval}
- }
-
- return nerr.E
-}
-
-// The JSON representation for a Duration is a JSON string that ends in the
-// suffix "s" (indicating seconds) and is preceded by the number of seconds,
-// with nanoseconds expressed as fractional seconds.
-//
-// Durations less than one second are represented with a 0 seconds field and a
-// positive or negative nanos field. For durations of one second or more, a
-// non-zero value for the nanos field must be of the same sign as the seconds
-// field.
-//
-// Duration.seconds must be from -315,576,000,000 to +315,576,000,000 inclusive.
-// Duration.nanos must be from -999,999,999 to +999,999,999 inclusive.
-
-const (
- secondsInNanos = 999999999
- maxSecondsInDuration = 315576000000
-)
-
-func (o MarshalOptions) marshalDuration(m pref.Message) error {
- messageDesc := m.Descriptor()
- knownFields := m.KnownFields()
-
- secsVal := knownFields.Get(fieldnum.Duration_Seconds)
- nanosVal := knownFields.Get(fieldnum.Duration_Nanos)
- secs := secsVal.Int()
- nanos := nanosVal.Int()
- if secs < -maxSecondsInDuration || secs > maxSecondsInDuration {
- return errors.New("%s: seconds out of range %v", messageDesc.FullName(), secs)
- }
- if nanos < -secondsInNanos || nanos > secondsInNanos {
- return errors.New("%s: nanos out of range %v", messageDesc.FullName(), nanos)
- }
- if (secs > 0 && nanos < 0) || (secs < 0 && nanos > 0) {
- return errors.New("%s: signs of seconds and nanos do not match", messageDesc.FullName())
- }
- // Generated output always contains 0, 3, 6, or 9 fractional digits,
- // depending on required precision, followed by the suffix "s".
- f := "%d.%09d"
- if nanos < 0 {
- nanos = -nanos
- if secs == 0 {
- f = "-%d.%09d"
- }
- }
- x := fmt.Sprintf(f, secs, nanos)
- x = strings.TrimSuffix(x, "000")
- x = strings.TrimSuffix(x, "000")
- x = strings.TrimSuffix(x, ".000")
- o.encoder.WriteString(x + "s")
- return nil
-}
-
-func (o UnmarshalOptions) unmarshalDuration(m pref.Message) error {
- var nerr errors.NonFatal
- jval, err := o.decoder.Read()
- if !nerr.Merge(err) {
- return err
- }
- if jval.Type() != json.String {
- return unexpectedJSONError{jval}
- }
-
- messageDesc := m.Descriptor()
- input := jval.String()
- secs, nanos, ok := parseDuration(input)
- if !ok {
- return errors.New("%s: invalid duration value %q", messageDesc.FullName(), input)
- }
- // Validate seconds. No need to validate nanos because parseDuration would
- // have covered that already.
- if secs < -maxSecondsInDuration || secs > maxSecondsInDuration {
- return errors.New("%s: out of range %q", messageDesc.FullName(), input)
- }
-
- knownFields := m.KnownFields()
- knownFields.Set(fieldnum.Duration_Seconds, pref.ValueOf(secs))
- knownFields.Set(fieldnum.Duration_Nanos, pref.ValueOf(nanos))
- return nerr.E
-}
-
-// parseDuration parses the given input string for seconds and nanoseconds value
-// for the Duration JSON format. The format is a decimal number with a suffix
-// 's'. It can have optional plus/minus sign. There needs to be at least an
-// integer or fractional part. Fractional part is limited to 9 digits only for
-// nanoseconds precision, regardless of whether there are trailing zero digits.
-// Example values are 1s, 0.1s, 1.s, .1s, +1s, -1s, -.1s.
-func parseDuration(input string) (int64, int32, bool) {
- b := []byte(input)
- size := len(b)
- if size < 2 {
- return 0, 0, false
- }
- if b[size-1] != 's' {
- return 0, 0, false
- }
- b = b[:size-1]
-
- // Read optional plus/minus symbol.
- var neg bool
- switch b[0] {
- case '-':
- neg = true
- b = b[1:]
- case '+':
- b = b[1:]
- }
- if len(b) == 0 {
- return 0, 0, false
- }
-
- // Read the integer part.
- var intp []byte
- switch {
- case b[0] == '0':
- b = b[1:]
-
- case '1' <= b[0] && b[0] <= '9':
- intp = b[0:]
- b = b[1:]
- n := 1
- for len(b) > 0 && '0' <= b[0] && b[0] <= '9' {
- n++
- b = b[1:]
- }
- intp = intp[:n]
-
- case b[0] == '.':
- // Continue below.
-
- default:
- return 0, 0, false
- }
-
- hasFrac := false
- var frac [9]byte
- if len(b) > 0 {
- if b[0] != '.' {
- return 0, 0, false
- }
- // Read the fractional part.
- b = b[1:]
- n := 0
- for len(b) > 0 && n < 9 && '0' <= b[0] && b[0] <= '9' {
- frac[n] = b[0]
- n++
- b = b[1:]
- }
- // It is not valid if there are more bytes left.
- if len(b) > 0 {
- return 0, 0, false
- }
- // Pad fractional part with 0s.
- for i := n; i < 9; i++ {
- frac[i] = '0'
- }
- hasFrac = true
- }
-
- var secs int64
- if len(intp) > 0 {
- var err error
- secs, err = strconv.ParseInt(string(intp), 10, 64)
- if err != nil {
- return 0, 0, false
- }
- }
-
- var nanos int64
- if hasFrac {
- nanob := bytes.TrimLeft(frac[:], "0")
- if len(nanob) > 0 {
- var err error
- nanos, err = strconv.ParseInt(string(nanob), 10, 32)
- if err != nil {
- return 0, 0, false
- }
- }
- }
-
- if neg {
- if secs > 0 {
- secs = -secs
- }
- if nanos > 0 {
- nanos = -nanos
- }
- }
- return secs, int32(nanos), true
-}
-
-// The JSON representation for a Timestamp is a JSON string in the RFC 3339
-// format, i.e. "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z" where
-// {year} is always expressed using four digits while {month}, {day}, {hour},
-// {min}, and {sec} are zero-padded to two digits each. The fractional seconds,
-// which can go up to 9 digits, up to 1 nanosecond resolution, is optional. The
-// "Z" suffix indicates the timezone ("UTC"); the timezone is required. Encoding
-// should always use UTC (as indicated by "Z") and a decoder should be able to
-// accept both UTC and other timezones (as indicated by an offset).
-//
-// Timestamp.seconds must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z
-// inclusive.
-// Timestamp.nanos must be from 0 to 999,999,999 inclusive.
-
-const (
- maxTimestampSeconds = 253402300799
- minTimestampSeconds = -62135596800
-)
-
-func (o MarshalOptions) marshalTimestamp(m pref.Message) error {
- messageDesc := m.Descriptor()
- knownFields := m.KnownFields()
-
- secsVal := knownFields.Get(fieldnum.Timestamp_Seconds)
- nanosVal := knownFields.Get(fieldnum.Timestamp_Nanos)
- secs := secsVal.Int()
- nanos := nanosVal.Int()
- if secs < minTimestampSeconds || secs > maxTimestampSeconds {
- return errors.New("%s: seconds out of range %v", messageDesc.FullName(), secs)
- }
- if nanos < 0 || nanos > secondsInNanos {
- return errors.New("%s: nanos out of range %v", messageDesc.FullName(), nanos)
- }
- // Uses RFC 3339, where generated output will be Z-normalized and uses 0, 3,
- // 6 or 9 fractional digits.
- t := time.Unix(secs, nanos).UTC()
- x := t.Format("2006-01-02T15:04:05.000000000")
- x = strings.TrimSuffix(x, "000")
- x = strings.TrimSuffix(x, "000")
- x = strings.TrimSuffix(x, ".000")
- o.encoder.WriteString(x + "Z")
- return nil
-}
-
-func (o UnmarshalOptions) unmarshalTimestamp(m pref.Message) error {
- var nerr errors.NonFatal
- jval, err := o.decoder.Read()
- if !nerr.Merge(err) {
- return err
- }
- if jval.Type() != json.String {
- return unexpectedJSONError{jval}
- }
-
- messageDesc := m.Descriptor()
- input := jval.String()
- t, err := time.Parse(time.RFC3339Nano, input)
- if err != nil {
- return errors.New("%s: invalid timestamp value %q", messageDesc.FullName(), input)
- }
- // Validate seconds. No need to validate nanos because time.Parse would have
- // covered that already.
- secs := t.Unix()
- if secs < minTimestampSeconds || secs > maxTimestampSeconds {
- return errors.New("%s: out of range %q", messageDesc.FullName(), input)
- }
-
- knownFields := m.KnownFields()
- knownFields.Set(fieldnum.Timestamp_Seconds, pref.ValueOf(secs))
- knownFields.Set(fieldnum.Timestamp_Nanos, pref.ValueOf(int32(t.Nanosecond())))
- return nerr.E
-}
-
-// The JSON representation for a FieldMask is a JSON string where paths are
-// separated by a comma. Fields name in each path are converted to/from
-// lower-camel naming conventions. Encoding should fail if the path name would
-// end up differently after a round-trip.
-
-func (o MarshalOptions) marshalFieldMask(m pref.Message) error {
- val := m.KnownFields().Get(fieldnum.FieldMask_Paths)
- list := val.List()
- paths := make([]string, 0, list.Len())
-
- for i := 0; i < list.Len(); i++ {
- s := list.Get(i).String()
- // Return error if conversion to camelCase is not reversible.
- cc := camelCase(s)
- if s != snakeCase(cc) {
- return errors.New("%s.paths contains irreversible value %q", m.Descriptor().FullName(), s)
- }
- paths = append(paths, cc)
- }
-
- o.encoder.WriteString(strings.Join(paths, ","))
- return nil
-}
-
-func (o UnmarshalOptions) unmarshalFieldMask(m pref.Message) error {
- var nerr errors.NonFatal
- jval, err := o.decoder.Read()
- if !nerr.Merge(err) {
- return err
- }
- if jval.Type() != json.String {
- return unexpectedJSONError{jval}
- }
- str := strings.TrimSpace(jval.String())
- if str == "" {
- return nil
- }
- paths := strings.Split(str, ",")
-
- val := m.KnownFields().Get(fieldnum.FieldMask_Paths)
- list := val.List()
-
- for _, s := range paths {
- s = strings.TrimSpace(s)
- // Convert to snake_case. Unlike encoding, no validation is done because
- // it is not possible to know the original path names.
- list.Append(pref.ValueOf(snakeCase(s)))
- }
- return nil
-}
-
-// camelCase converts given string into camelCase where ASCII character after _
-// is turned into uppercase and _'s are removed.
-func camelCase(s string) string {
- var b []byte
- var afterUnderscore bool
- for i := 0; i < len(s); i++ {
- c := s[i]
- if afterUnderscore {
- if isASCIILower(c) {
- c -= 'a' - 'A'
- }
- }
- if c == '_' {
- afterUnderscore = true
- continue
- }
- afterUnderscore = false
- b = append(b, c)
- }
- return string(b)
-}
-
-// snakeCase converts given string into snake_case where ASCII uppercase
-// character is turned into _ + lowercase.
-func snakeCase(s string) string {
- var b []byte
- for i := 0; i < len(s); i++ {
- c := s[i]
- if isASCIIUpper(c) {
- c += 'a' - 'A'
- b = append(b, '_', c)
- } else {
- b = append(b, c)
- }
- }
- return string(b)
-}
-
-func isASCIILower(c byte) bool {
- return 'a' <= c && c <= 'z'
-}
-
-func isASCIIUpper(c byte) bool {
- return 'A' <= c && c <= 'Z'
-}