Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 5 | package protojson |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "encoding/base64" |
| 9 | "fmt" |
| 10 | "math" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 14 | "google.golang.org/protobuf/internal/encoding/json" |
Joe Tsai | 5ae10aa | 2019-07-11 18:23:08 -0700 | [diff] [blame] | 15 | "google.golang.org/protobuf/internal/encoding/messageset" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 16 | "google.golang.org/protobuf/internal/errors" |
Joe Tsai | 5ae10aa | 2019-07-11 18:23:08 -0700 | [diff] [blame] | 17 | "google.golang.org/protobuf/internal/flags" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 18 | "google.golang.org/protobuf/internal/pragma" |
| 19 | "google.golang.org/protobuf/internal/set" |
| 20 | "google.golang.org/protobuf/proto" |
| 21 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 22 | "google.golang.org/protobuf/reflect/protoregistry" |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | // Unmarshal reads the given []byte into the given proto.Message. |
Joe Tsai | cdb7773 | 2019-05-14 16:05:06 -0700 | [diff] [blame] | 26 | func Unmarshal(b []byte, m proto.Message) error { |
| 27 | return UnmarshalOptions{}.Unmarshal(b, m) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // UnmarshalOptions is a configurable JSON format parser. |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 31 | type UnmarshalOptions struct { |
| 32 | pragma.NoUnkeyedLiterals |
| 33 | |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 34 | // If AllowPartial is set, input for messages that will result in missing |
| 35 | // required fields will not return an error. |
Herbie Ong | 329be5b | 2019-03-27 14:47:59 -0700 | [diff] [blame] | 36 | AllowPartial bool |
| 37 | |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 38 | // If DiscardUnknown is set, unknown fields are ignored. |
| 39 | DiscardUnknown bool |
| 40 | |
Joe Tsai | 1c28304 | 2019-05-14 14:28:19 -0700 | [diff] [blame] | 41 | // Resolver is used for looking up types when unmarshaling |
| 42 | // google.protobuf.Any messages or extension fields. |
| 43 | // If nil, this defaults to using protoregistry.GlobalTypes. |
| 44 | Resolver interface { |
| 45 | protoregistry.MessageTypeResolver |
| 46 | protoregistry.ExtensionTypeResolver |
| 47 | } |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 48 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 49 | |
| 50 | // Unmarshal reads the given []byte and populates the given proto.Message using |
| 51 | // options in UnmarshalOptions object. It will clear the message first before |
| 52 | // setting the fields. If it returns an error, the given message may be |
| 53 | // partially set. |
Joe Tsai | cdb7773 | 2019-05-14 16:05:06 -0700 | [diff] [blame] | 54 | func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 55 | proto.Reset(m) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 56 | |
Herbie Ong | 822de2d | 2019-03-27 13:16:23 -0700 | [diff] [blame] | 57 | if o.Resolver == nil { |
| 58 | o.Resolver = protoregistry.GlobalTypes |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 61 | dec := decoder{json.NewDecoder(b), o} |
| 62 | if err := dec.unmarshalMessage(m.ProtoReflect(), false); err != nil { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 63 | return err |
| 64 | } |
| 65 | |
| 66 | // Check for EOF. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 67 | tok, err := dec.Read() |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 68 | if err != nil { |
| 69 | return err |
| 70 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 71 | if tok.Kind() != json.EOF { |
| 72 | return dec.unexpectedTokenError(tok) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 73 | } |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 74 | |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 75 | if o.AllowPartial { |
| 76 | return nil |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 77 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 78 | return proto.IsInitialized(m) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 81 | type decoder struct { |
| 82 | *json.Decoder |
| 83 | opts UnmarshalOptions |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 86 | // newError returns an error object with position info. |
| 87 | func (d decoder) newError(pos int, f string, x ...interface{}) error { |
| 88 | line, column := d.Position(pos) |
| 89 | head := fmt.Sprintf("(line %d:%d): ", line, column) |
| 90 | return errors.New(head+f, x...) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 93 | // unexpectedTokenError returns a syntax error for the given unexpected token. |
| 94 | func (d decoder) unexpectedTokenError(tok json.Token) error { |
| 95 | return d.syntaxError(tok.Pos(), "unexpected token %s", tok.RawString()) |
| 96 | } |
| 97 | |
| 98 | // syntaxError returns a syntax error for given position. |
| 99 | func (d decoder) syntaxError(pos int, f string, x ...interface{}) error { |
| 100 | line, column := d.Position(pos) |
| 101 | head := fmt.Sprintf("syntax error (line %d:%d): ", line, column) |
| 102 | return errors.New(head+f, x...) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 105 | // unmarshalMessage unmarshals a message into the given protoreflect.Message. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 106 | func (d decoder) unmarshalMessage(m pref.Message, skipTypeURL bool) error { |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 107 | if isCustomType(m.Descriptor().FullName()) { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 108 | return d.unmarshalCustomType(m) |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 109 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 110 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 111 | tok, err := d.Read() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 112 | if err != nil { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 113 | return err |
| 114 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 115 | if tok.Kind() != json.ObjectOpen { |
| 116 | return d.unexpectedTokenError(tok) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 119 | if err := d.unmarshalFields(m, skipTypeURL); err != nil { |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 120 | return err |
| 121 | } |
| 122 | |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 123 | return nil |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // unmarshalFields unmarshals the fields into the given protoreflect.Message. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 127 | func (d decoder) unmarshalFields(m pref.Message, skipTypeURL bool) error { |
Joe Tsai | 5ae10aa | 2019-07-11 18:23:08 -0700 | [diff] [blame] | 128 | messageDesc := m.Descriptor() |
Joe Tsai | 1799d11 | 2019-08-08 13:31:59 -0700 | [diff] [blame] | 129 | if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) { |
Joe Tsai | 5ae10aa | 2019-07-11 18:23:08 -0700 | [diff] [blame] | 130 | return errors.New("no support for proto1 MessageSets") |
| 131 | } |
| 132 | |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 133 | var seenNums set.Ints |
Herbie Ong | 8a1d460 | 2019-04-02 20:19:36 -0700 | [diff] [blame] | 134 | var seenOneofs set.Ints |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 135 | fieldDescs := messageDesc.Fields() |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 136 | for { |
| 137 | // Read field name. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 138 | tok, err := d.Read() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 139 | if err != nil { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 140 | return err |
| 141 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 142 | switch tok.Kind() { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 143 | default: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 144 | return d.unexpectedTokenError(tok) |
| 145 | case json.ObjectClose: |
Joe Tsai | 5ae10aa | 2019-07-11 18:23:08 -0700 | [diff] [blame] | 146 | return nil |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 147 | case json.Name: |
| 148 | // Continue below. |
| 149 | } |
| 150 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 151 | name := tok.Name() |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 152 | // Unmarshaling a non-custom embedded message in Any will contain the |
| 153 | // JSON field "@type" which should be skipped because it is not a field |
| 154 | // of the embedded message, but simply an artifact of the Any format. |
| 155 | if skipTypeURL && name == "@type" { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 156 | d.Read() |
Herbie Ong | 8ac9dd2 | 2019-03-27 12:20:50 -0700 | [diff] [blame] | 157 | continue |
| 158 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 159 | |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 160 | // Get the FieldDescriptor. |
| 161 | var fd pref.FieldDescriptor |
| 162 | if strings.HasPrefix(name, "[") && strings.HasSuffix(name, "]") { |
| 163 | // Only extension names are in [name] format. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 164 | extName := pref.FullName(name[1 : len(name)-1]) |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 165 | extType, err := d.findExtension(extName) |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 166 | if err != nil && err != protoregistry.NotFound { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 167 | return d.newError(tok.Pos(), "unable to resolve %s: %v", tok.RawString(), err) |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 168 | } |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 169 | if extType != nil { |
Damien Neil | 79bfdbe | 2019-08-28 11:08:22 -0700 | [diff] [blame] | 170 | fd = extType.TypeDescriptor() |
Joe Tsai | 0bf4113 | 2019-09-05 21:46:36 -0700 | [diff] [blame] | 171 | if !messageDesc.ExtensionRanges().Has(fd.Number()) || fd.ContainingMessage().FullName() != messageDesc.FullName() { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 172 | return d.newError(tok.Pos(), "message %v cannot be extended by %v", messageDesc.FullName(), fd.FullName()) |
Joe Tsai | 0bf4113 | 2019-09-05 21:46:36 -0700 | [diff] [blame] | 173 | } |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 174 | } |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 175 | } else { |
| 176 | // The name can either be the JSON name or the proto field name. |
| 177 | fd = fieldDescs.ByJSONName(name) |
| 178 | if fd == nil { |
| 179 | fd = fieldDescs.ByName(pref.Name(name)) |
Joe Tsai | 7fa1ee5 | 2019-09-15 00:32:55 -0700 | [diff] [blame] | 180 | if fd == nil { |
| 181 | // The proto name of a group field is in all lowercase, |
| 182 | // while the textual field name is the group message name. |
| 183 | gd := fieldDescs.ByName(pref.Name(strings.ToLower(name))) |
| 184 | if gd != nil && gd.Kind() == pref.GroupKind && gd.Message().Name() == pref.Name(name) { |
| 185 | fd = gd |
| 186 | } |
| 187 | } else if fd.Kind() == pref.GroupKind && fd.Message().Name() != pref.Name(name) { |
| 188 | fd = nil // reset since field name is actually the message name |
| 189 | } |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 190 | } |
Joe Tsai | 6e09599 | 2019-08-10 13:56:36 -0700 | [diff] [blame] | 191 | } |
| 192 | if flags.ProtoLegacy { |
Joe Tsai | d47ea19 | 2019-07-09 22:38:15 -0700 | [diff] [blame] | 193 | if fd != nil && fd.IsWeak() && fd.Message().IsPlaceholder() { |
| 194 | fd = nil // reset since the weak reference is not linked in |
| 195 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if fd == nil { |
| 199 | // Field is unknown. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 200 | if d.opts.DiscardUnknown { |
| 201 | if err := d.skipJSONValue(); err != nil { |
Herbie Ong | 4f0be71 | 2019-04-25 17:57:12 -0700 | [diff] [blame] | 202 | return err |
| 203 | } |
| 204 | continue |
| 205 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 206 | return d.newError(tok.Pos(), "unknown field %v", tok.RawString()) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // Do not allow duplicate fields. |
| 210 | num := uint64(fd.Number()) |
| 211 | if seenNums.Has(num) { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 212 | return d.newError(tok.Pos(), "duplicate field %v", tok.RawString()) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 213 | } |
| 214 | seenNums.Set(num) |
| 215 | |
Herbie Ong | e63c4c4 | 2019-03-22 22:20:22 -0700 | [diff] [blame] | 216 | // No need to set values for JSON null unless the field type is |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame] | 217 | // google.protobuf.Value or google.protobuf.NullValue. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 218 | if tok, _ := d.Peek(); tok.Kind() == json.Null && !isKnownValue(fd) && !isNullValue(fd) { |
| 219 | d.Read() |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 220 | continue |
| 221 | } |
| 222 | |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 223 | switch { |
| 224 | case fd.IsList(): |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 225 | list := m.Mutable(fd).List() |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 226 | if err := d.unmarshalList(list, fd); err != nil { |
| 227 | return err |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 228 | } |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 229 | case fd.IsMap(): |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 230 | mmap := m.Mutable(fd).Map() |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 231 | if err := d.unmarshalMap(mmap, fd); err != nil { |
| 232 | return err |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 233 | } |
| 234 | default: |
Herbie Ong | 8a1d460 | 2019-04-02 20:19:36 -0700 | [diff] [blame] | 235 | // If field is a oneof, check if it has already been set. |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 236 | if od := fd.ContainingOneof(); od != nil { |
Herbie Ong | 8a1d460 | 2019-04-02 20:19:36 -0700 | [diff] [blame] | 237 | idx := uint64(od.Index()) |
| 238 | if seenOneofs.Has(idx) { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 239 | return d.newError(tok.Pos(), "error parsing %s, oneof %v is already set", tok.RawString(), od.FullName()) |
Herbie Ong | 8a1d460 | 2019-04-02 20:19:36 -0700 | [diff] [blame] | 240 | } |
| 241 | seenOneofs.Set(idx) |
| 242 | } |
| 243 | |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 244 | // Required or optional fields. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 245 | if err := d.unmarshalSingular(m, fd); err != nil { |
| 246 | return err |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 247 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 252 | // findExtension returns protoreflect.ExtensionType from the resolver if found. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 253 | func (d decoder) findExtension(xtName pref.FullName) (pref.ExtensionType, error) { |
| 254 | xt, err := d.opts.Resolver.FindExtensionByName(xtName) |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 255 | if err == nil { |
| 256 | return xt, nil |
| 257 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 258 | return messageset.FindMessageSetExtension(d.opts.Resolver, xtName) |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame] | 261 | func isKnownValue(fd pref.FieldDescriptor) bool { |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 262 | md := fd.Message() |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame] | 263 | return md != nil && md.FullName() == "google.protobuf.Value" |
| 264 | } |
| 265 | |
| 266 | func isNullValue(fd pref.FieldDescriptor) bool { |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 267 | ed := fd.Enum() |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame] | 268 | return ed != nil && ed.FullName() == "google.protobuf.NullValue" |
| 269 | } |
| 270 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 271 | // unmarshalSingular unmarshals to the non-repeated field specified |
| 272 | // by the given FieldDescriptor. |
| 273 | func (d decoder) unmarshalSingular(m pref.Message, fd pref.FieldDescriptor) error { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 274 | var val pref.Value |
| 275 | var err error |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 276 | switch fd.Kind() { |
| 277 | case pref.MessageKind, pref.GroupKind: |
Damien Neil | d91c422 | 2019-09-04 10:46:00 -0700 | [diff] [blame] | 278 | val = m.NewField(fd) |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 279 | err = d.unmarshalMessage(val.Message(), false) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 280 | default: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 281 | val, err = d.unmarshalScalar(fd) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 284 | if err != nil { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 285 | return err |
| 286 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 287 | m.Set(fd, val) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 288 | return nil |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // unmarshalScalar unmarshals to a scalar/enum protoreflect.Value specified by |
| 292 | // the given FieldDescriptor. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 293 | func (d decoder) unmarshalScalar(fd pref.FieldDescriptor) (pref.Value, error) { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 294 | const b32 int = 32 |
| 295 | const b64 int = 64 |
| 296 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 297 | tok, err := d.Read() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 298 | if err != nil { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 299 | return pref.Value{}, err |
| 300 | } |
| 301 | |
| 302 | kind := fd.Kind() |
| 303 | switch kind { |
| 304 | case pref.BoolKind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 305 | if tok.Kind() == json.Bool { |
| 306 | return pref.ValueOfBool(tok.Bool()), nil |
| 307 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 308 | |
| 309 | case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 310 | if v, ok := unmarshalInt(tok, b32); ok { |
| 311 | return v, nil |
| 312 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 313 | |
| 314 | case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 315 | if v, ok := unmarshalInt(tok, b64); ok { |
| 316 | return v, nil |
| 317 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 318 | |
| 319 | case pref.Uint32Kind, pref.Fixed32Kind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 320 | if v, ok := unmarshalUint(tok, b32); ok { |
| 321 | return v, nil |
| 322 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 323 | |
| 324 | case pref.Uint64Kind, pref.Fixed64Kind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 325 | if v, ok := unmarshalUint(tok, b64); ok { |
| 326 | return v, nil |
| 327 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 328 | |
| 329 | case pref.FloatKind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 330 | if v, ok := unmarshalFloat(tok, b32); ok { |
| 331 | return v, nil |
| 332 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 333 | |
| 334 | case pref.DoubleKind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 335 | if v, ok := unmarshalFloat(tok, b64); ok { |
| 336 | return v, nil |
| 337 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 338 | |
| 339 | case pref.StringKind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 340 | if tok.Kind() == json.String { |
| 341 | return pref.ValueOfString(tok.ParsedString()), nil |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 342 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 343 | |
| 344 | case pref.BytesKind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 345 | if v, ok := unmarshalBytes(tok); ok { |
| 346 | return v, nil |
| 347 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 348 | |
| 349 | case pref.EnumKind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 350 | if v, ok := unmarshalEnum(tok, fd); ok { |
| 351 | return v, nil |
| 352 | } |
| 353 | |
| 354 | default: |
| 355 | panic(fmt.Sprintf("unmarshalScalar: invalid scalar kind %v", kind)) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 358 | return pref.Value{}, d.newError(tok.Pos(), "invalid value for %v type: %v", kind, tok.RawString()) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 361 | func unmarshalInt(tok json.Token, bitSize int) (pref.Value, bool) { |
| 362 | switch tok.Kind() { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 363 | case json.Number: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 364 | return getInt(tok, bitSize) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 365 | |
| 366 | case json.String: |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 367 | // Decode number from string. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 368 | s := strings.TrimSpace(tok.ParsedString()) |
| 369 | if len(s) != len(tok.ParsedString()) { |
| 370 | return pref.Value{}, false |
Herbie Ong | 06a6b0b | 2019-04-24 21:15:45 -0700 | [diff] [blame] | 371 | } |
| 372 | dec := json.NewDecoder([]byte(s)) |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 373 | tok, err := dec.Read() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 374 | if err != nil { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 375 | return pref.Value{}, false |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 376 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 377 | return getInt(tok, bitSize) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 378 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 379 | return pref.Value{}, false |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 382 | func getInt(tok json.Token, bitSize int) (pref.Value, bool) { |
| 383 | n, ok := tok.Int(bitSize) |
| 384 | if !ok { |
| 385 | return pref.Value{}, false |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 386 | } |
| 387 | if bitSize == 32 { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 388 | return pref.ValueOfInt32(int32(n)), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 389 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 390 | return pref.ValueOfInt64(n), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 393 | func unmarshalUint(tok json.Token, bitSize int) (pref.Value, bool) { |
| 394 | switch tok.Kind() { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 395 | case json.Number: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 396 | return getUint(tok, bitSize) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 397 | |
| 398 | case json.String: |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 399 | // Decode number from string. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 400 | s := strings.TrimSpace(tok.ParsedString()) |
| 401 | if len(s) != len(tok.ParsedString()) { |
| 402 | return pref.Value{}, false |
Herbie Ong | 06a6b0b | 2019-04-24 21:15:45 -0700 | [diff] [blame] | 403 | } |
| 404 | dec := json.NewDecoder([]byte(s)) |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 405 | tok, err := dec.Read() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 406 | if err != nil { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 407 | return pref.Value{}, false |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 408 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 409 | return getUint(tok, bitSize) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 410 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 411 | return pref.Value{}, false |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 414 | func getUint(tok json.Token, bitSize int) (pref.Value, bool) { |
| 415 | n, ok := tok.Uint(bitSize) |
| 416 | if !ok { |
| 417 | return pref.Value{}, false |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 418 | } |
| 419 | if bitSize == 32 { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 420 | return pref.ValueOfUint32(uint32(n)), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 421 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 422 | return pref.ValueOfUint64(n), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 425 | func unmarshalFloat(tok json.Token, bitSize int) (pref.Value, bool) { |
| 426 | switch tok.Kind() { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 427 | case json.Number: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 428 | return getFloat(tok, bitSize) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 429 | |
| 430 | case json.String: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 431 | s := tok.ParsedString() |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 432 | switch s { |
| 433 | case "NaN": |
| 434 | if bitSize == 32 { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 435 | return pref.ValueOfFloat32(float32(math.NaN())), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 436 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 437 | return pref.ValueOfFloat64(math.NaN()), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 438 | case "Infinity": |
| 439 | if bitSize == 32 { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 440 | return pref.ValueOfFloat32(float32(math.Inf(+1))), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 441 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 442 | return pref.ValueOfFloat64(math.Inf(+1)), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 443 | case "-Infinity": |
| 444 | if bitSize == 32 { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 445 | return pref.ValueOfFloat32(float32(math.Inf(-1))), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 446 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 447 | return pref.ValueOfFloat64(math.Inf(-1)), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 448 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 449 | |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 450 | // Decode number from string. |
Herbie Ong | 06a6b0b | 2019-04-24 21:15:45 -0700 | [diff] [blame] | 451 | if len(s) != len(strings.TrimSpace(s)) { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 452 | return pref.Value{}, false |
Herbie Ong | 06a6b0b | 2019-04-24 21:15:45 -0700 | [diff] [blame] | 453 | } |
Herbie Ong | e52379a | 2019-03-15 18:00:19 -0700 | [diff] [blame] | 454 | dec := json.NewDecoder([]byte(s)) |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 455 | tok, err := dec.Read() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 456 | if err != nil { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 457 | return pref.Value{}, false |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 458 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 459 | return getFloat(tok, bitSize) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 460 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 461 | return pref.Value{}, false |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 464 | func getFloat(tok json.Token, bitSize int) (pref.Value, bool) { |
| 465 | n, ok := tok.Float(bitSize) |
| 466 | if !ok { |
| 467 | return pref.Value{}, false |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 468 | } |
| 469 | if bitSize == 32 { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 470 | return pref.ValueOfFloat32(float32(n)), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 471 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 472 | return pref.ValueOfFloat64(n), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 475 | func unmarshalBytes(tok json.Token) (pref.Value, bool) { |
| 476 | if tok.Kind() != json.String { |
| 477 | return pref.Value{}, false |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 480 | s := tok.ParsedString() |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 481 | enc := base64.StdEncoding |
| 482 | if strings.ContainsAny(s, "-_") { |
| 483 | enc = base64.URLEncoding |
| 484 | } |
| 485 | if len(s)%4 != 0 { |
| 486 | enc = enc.WithPadding(base64.NoPadding) |
| 487 | } |
| 488 | b, err := enc.DecodeString(s) |
| 489 | if err != nil { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 490 | return pref.Value{}, false |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 491 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 492 | return pref.ValueOfBytes(b), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 495 | func unmarshalEnum(tok json.Token, fd pref.FieldDescriptor) (pref.Value, bool) { |
| 496 | switch tok.Kind() { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 497 | case json.String: |
| 498 | // Lookup EnumNumber based on name. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 499 | s := tok.ParsedString() |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 500 | if enumVal := fd.Enum().Values().ByName(pref.Name(s)); enumVal != nil { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 501 | return pref.ValueOfEnum(enumVal.Number()), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 502 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 503 | |
| 504 | case json.Number: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 505 | if n, ok := tok.Int(32); ok { |
| 506 | return pref.ValueOfEnum(pref.EnumNumber(n)), true |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 507 | } |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame] | 508 | |
| 509 | case json.Null: |
| 510 | // This is only valid for google.protobuf.NullValue. |
| 511 | if isNullValue(fd) { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 512 | return pref.ValueOfEnum(0), true |
Herbie Ong | 300b9fe | 2019-03-29 15:42:20 -0700 | [diff] [blame] | 513 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 516 | return pref.Value{}, false |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 519 | func (d decoder) unmarshalList(list pref.List, fd pref.FieldDescriptor) error { |
| 520 | tok, err := d.Read() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 521 | if err != nil { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 522 | return err |
| 523 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 524 | if tok.Kind() != json.ArrayOpen { |
| 525 | return d.unexpectedTokenError(tok) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | switch fd.Kind() { |
| 529 | case pref.MessageKind, pref.GroupKind: |
| 530 | for { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 531 | tok, err := d.Peek() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 532 | if err != nil { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 533 | return err |
| 534 | } |
| 535 | |
| 536 | if tok.Kind() == json.ArrayClose { |
| 537 | d.Read() |
| 538 | return nil |
| 539 | } |
| 540 | |
| 541 | val := list.NewElement() |
| 542 | if err := d.unmarshalMessage(val.Message(), false); err != nil { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 543 | return err |
| 544 | } |
Damien Neil | d91c422 | 2019-09-04 10:46:00 -0700 | [diff] [blame] | 545 | list.Append(val) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 546 | } |
| 547 | default: |
| 548 | for { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 549 | tok, err := d.Peek() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 550 | if err != nil { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 551 | return err |
| 552 | } |
| 553 | |
| 554 | if tok.Kind() == json.ArrayClose { |
| 555 | d.Read() |
| 556 | return nil |
| 557 | } |
| 558 | |
| 559 | val, err := d.unmarshalScalar(fd) |
| 560 | if err != nil { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 561 | return err |
| 562 | } |
| 563 | list.Append(val) |
| 564 | } |
| 565 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 566 | |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 567 | return nil |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 570 | func (d decoder) unmarshalMap(mmap pref.Map, fd pref.FieldDescriptor) error { |
| 571 | tok, err := d.Read() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 572 | if err != nil { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 573 | return err |
| 574 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 575 | if tok.Kind() != json.ObjectOpen { |
| 576 | return d.unexpectedTokenError(tok) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 579 | // Determine ahead whether map entry is a scalar type or a message type in |
| 580 | // order to call the appropriate unmarshalMapValue func inside the for loop |
| 581 | // below. |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 582 | var unmarshalMapValue func() (pref.Value, error) |
| 583 | switch fd.MapValue().Kind() { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 584 | case pref.MessageKind, pref.GroupKind: |
| 585 | unmarshalMapValue = func() (pref.Value, error) { |
Damien Neil | d91c422 | 2019-09-04 10:46:00 -0700 | [diff] [blame] | 586 | val := mmap.NewValue() |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 587 | if err := d.unmarshalMessage(val.Message(), false); err != nil { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 588 | return pref.Value{}, err |
| 589 | } |
Damien Neil | d91c422 | 2019-09-04 10:46:00 -0700 | [diff] [blame] | 590 | return val, nil |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 591 | } |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 592 | default: |
| 593 | unmarshalMapValue = func() (pref.Value, error) { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 594 | return d.unmarshalScalar(fd.MapValue()) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 595 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | Loop: |
| 599 | for { |
| 600 | // Read field name. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 601 | tok, err := d.Read() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 602 | if err != nil { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 603 | return err |
| 604 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 605 | switch tok.Kind() { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 606 | default: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 607 | return d.unexpectedTokenError(tok) |
| 608 | case json.ObjectClose: |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 609 | break Loop |
| 610 | case json.Name: |
| 611 | // Continue. |
| 612 | } |
| 613 | |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 614 | // Unmarshal field name. |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 615 | pkey, err := d.unmarshalMapKey(tok, fd.MapKey()) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 616 | if err != nil { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 617 | return err |
| 618 | } |
| 619 | |
| 620 | // Check for duplicate field name. |
| 621 | if mmap.Has(pkey) { |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 622 | return d.newError(tok.Pos(), "duplicate map key %v", tok.RawString()) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | // Read and unmarshal field value. |
| 626 | pval, err := unmarshalMapValue() |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 627 | if err != nil { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 628 | return err |
| 629 | } |
| 630 | |
| 631 | mmap.Set(pkey, pval) |
| 632 | } |
| 633 | |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 634 | return nil |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 637 | // unmarshalMapKey converts given token of Name kind into a protoreflect.MapKey. |
| 638 | // A map key type is any integral or string type. |
| 639 | func (d decoder) unmarshalMapKey(tok json.Token, fd pref.FieldDescriptor) (pref.MapKey, error) { |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 640 | const b32 = 32 |
| 641 | const b64 = 64 |
| 642 | const base10 = 10 |
| 643 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 644 | name := tok.Name() |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 645 | kind := fd.Kind() |
| 646 | switch kind { |
| 647 | case pref.StringKind: |
Joe Tsai | 84177c9 | 2019-09-17 13:38:48 -0700 | [diff] [blame] | 648 | return pref.ValueOfString(name).MapKey(), nil |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 649 | |
| 650 | case pref.BoolKind: |
| 651 | switch name { |
| 652 | case "true": |
Joe Tsai | 84177c9 | 2019-09-17 13:38:48 -0700 | [diff] [blame] | 653 | return pref.ValueOfBool(true).MapKey(), nil |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 654 | case "false": |
Joe Tsai | 84177c9 | 2019-09-17 13:38:48 -0700 | [diff] [blame] | 655 | return pref.ValueOfBool(false).MapKey(), nil |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 656 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 657 | |
| 658 | case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 659 | if n, err := strconv.ParseInt(name, base10, b32); err == nil { |
| 660 | return pref.ValueOfInt32(int32(n)).MapKey(), nil |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 661 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 662 | |
| 663 | case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 664 | if n, err := strconv.ParseInt(name, base10, b64); err == nil { |
| 665 | return pref.ValueOfInt64(int64(n)).MapKey(), nil |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 666 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 667 | |
| 668 | case pref.Uint32Kind, pref.Fixed32Kind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 669 | if n, err := strconv.ParseUint(name, base10, b32); err == nil { |
| 670 | return pref.ValueOfUint32(uint32(n)).MapKey(), nil |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 671 | } |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 672 | |
| 673 | case pref.Uint64Kind, pref.Fixed64Kind: |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 674 | if n, err := strconv.ParseUint(name, base10, b64); err == nil { |
| 675 | return pref.ValueOfUint64(uint64(n)).MapKey(), nil |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 676 | } |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 677 | |
| 678 | default: |
| 679 | panic(fmt.Sprintf("invalid kind for map key: %v", kind)) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Herbie Ong | d2ece13 | 2020-01-07 16:45:24 -0800 | [diff] [blame^] | 682 | return pref.MapKey{}, d.newError(tok.Pos(), "invalid value for %v key: %s", kind, tok.RawString()) |
Herbie Ong | c96a79d | 2019-03-08 10:49:17 -0800 | [diff] [blame] | 683 | } |