Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 1 | // Copyright 2018 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 prototext |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "fmt" |
Herbie Ong | 0dcfb9a | 2019-01-14 15:32:26 -0800 | [diff] [blame] | 9 | "strings" |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 10 | "unicode/utf8" |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 11 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 12 | "google.golang.org/protobuf/internal/encoding/text" |
| 13 | "google.golang.org/protobuf/internal/errors" |
| 14 | "google.golang.org/protobuf/internal/fieldnum" |
| 15 | "google.golang.org/protobuf/internal/pragma" |
| 16 | "google.golang.org/protobuf/internal/set" |
| 17 | "google.golang.org/protobuf/proto" |
| 18 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 19 | "google.golang.org/protobuf/reflect/protoregistry" |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | // Unmarshal reads the given []byte into the given proto.Message. |
Joe Tsai | cdb7773 | 2019-05-14 16:05:06 -0700 | [diff] [blame] | 23 | func Unmarshal(b []byte, m proto.Message) error { |
| 24 | return UnmarshalOptions{}.Unmarshal(b, m) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 25 | } |
| 26 | |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 27 | // UnmarshalOptions is a configurable textproto format unmarshaler. |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 28 | type UnmarshalOptions struct { |
| 29 | pragma.NoUnkeyedLiterals |
Herbie Ong | c525c97 | 2018-12-18 18:04:31 -0800 | [diff] [blame] | 30 | |
Herbie Ong | 42577ea | 2019-03-26 16:26:22 -0700 | [diff] [blame] | 31 | // AllowPartial accepts input for messages that will result in missing |
| 32 | // required fields. If AllowPartial is false (the default), Unmarshal will |
| 33 | // return error if there are any missing required fields. |
| 34 | AllowPartial bool |
| 35 | |
Joe Tsai | 1c28304 | 2019-05-14 14:28:19 -0700 | [diff] [blame] | 36 | // Resolver is used for looking up types when unmarshaling |
| 37 | // google.protobuf.Any messages or extension fields. |
| 38 | // If nil, this defaults to using protoregistry.GlobalTypes. |
| 39 | Resolver interface { |
| 40 | protoregistry.MessageTypeResolver |
| 41 | protoregistry.ExtensionTypeResolver |
| 42 | } |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // Unmarshal reads the given []byte and populates the given proto.Message using options in |
| 46 | // UnmarshalOptions object. |
Joe Tsai | cdb7773 | 2019-05-14 16:05:06 -0700 | [diff] [blame] | 47 | func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 48 | var nerr errors.NonFatal |
| 49 | |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 50 | // Clear all fields before populating it. |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 51 | // TODO: Determine if this needs to be consistent with protojson and binary unmarshal where |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 52 | // behavior is to merge values into existing message. If decision is to not clear the fields |
| 53 | // ahead, code will need to be updated properly when merging nested messages. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 54 | proto.Reset(m) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 55 | |
| 56 | // Parse into text.Value of message type. |
| 57 | val, err := text.Unmarshal(b) |
| 58 | if !nerr.Merge(err) { |
| 59 | return err |
| 60 | } |
| 61 | |
Herbie Ong | c525c97 | 2018-12-18 18:04:31 -0800 | [diff] [blame] | 62 | if o.Resolver == nil { |
| 63 | o.Resolver = protoregistry.GlobalTypes |
| 64 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 65 | err = o.unmarshalMessage(val.Message(), m.ProtoReflect()) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 66 | if !nerr.Merge(err) { |
| 67 | return err |
| 68 | } |
| 69 | |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 70 | if !o.AllowPartial { |
| 71 | nerr.Merge(proto.IsInitialized(m)) |
| 72 | } |
| 73 | |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 74 | return nerr.E |
| 75 | } |
| 76 | |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 77 | // unmarshalMessage unmarshals a [][2]text.Value message into the given protoreflect.Message. |
| 78 | func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message) error { |
| 79 | var nerr errors.NonFatal |
| 80 | |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 81 | messageDesc := m.Descriptor() |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 82 | |
| 83 | // Handle expanded Any message. |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 84 | if messageDesc.FullName() == "google.protobuf.Any" && isExpandedAny(tmsg) { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 85 | return o.unmarshalAny(tmsg[0], m) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 88 | fieldDescs := messageDesc.Fields() |
| 89 | reservedNames := messageDesc.ReservedNames() |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 90 | var seenNums set.Ints |
Herbie Ong | 8a1d460 | 2019-04-02 20:19:36 -0700 | [diff] [blame] | 91 | var seenOneofs set.Ints |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 92 | |
| 93 | for _, tfield := range tmsg { |
| 94 | tkey := tfield[0] |
| 95 | tval := tfield[1] |
| 96 | |
| 97 | var fd pref.FieldDescriptor |
Herbie Ong | c525c97 | 2018-12-18 18:04:31 -0800 | [diff] [blame] | 98 | var name pref.Name |
| 99 | switch tkey.Type() { |
| 100 | case text.Name: |
| 101 | name, _ = tkey.Name() |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 102 | fd = fieldDescs.ByName(name) |
Herbie Ong | 0dcfb9a | 2019-01-14 15:32:26 -0800 | [diff] [blame] | 103 | if fd == nil { |
| 104 | // Check if this is a group field. |
| 105 | fd = fieldDescs.ByName(pref.Name(strings.ToLower(string(name)))) |
| 106 | } |
Herbie Ong | c525c97 | 2018-12-18 18:04:31 -0800 | [diff] [blame] | 107 | case text.String: |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 108 | // Handle extensions only. This code path is not for Any. |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 109 | if messageDesc.FullName() == "google.protobuf.Any" { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 110 | break |
| 111 | } |
| 112 | // Extensions have to be registered first in the message's |
Herbie Ong | c525c97 | 2018-12-18 18:04:31 -0800 | [diff] [blame] | 113 | // ExtensionTypes before setting a value to it. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 114 | extName := pref.FullName(tkey.String()) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 115 | // Check first if it is already registered. This is the case for |
| 116 | // repeated fields. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 117 | xt, err := o.findExtension(extName) |
| 118 | if err != nil && err != protoregistry.NotFound { |
| 119 | return errors.New("unable to resolve [%v]: %v", extName, err) |
Herbie Ong | c525c97 | 2018-12-18 18:04:31 -0800 | [diff] [blame] | 120 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 121 | fd = xt |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 122 | } |
Herbie Ong | c525c97 | 2018-12-18 18:04:31 -0800 | [diff] [blame] | 123 | |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 124 | if fd == nil { |
Herbie Ong | 7c624e2 | 2018-12-13 14:41:22 -0800 | [diff] [blame] | 125 | // Ignore reserved names. |
| 126 | if reservedNames.Has(name) { |
| 127 | continue |
| 128 | } |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 129 | // TODO: Can provide option to ignore unknown message fields. |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 130 | return errors.New("%v contains unknown field: %v", messageDesc.FullName(), tkey) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 133 | switch { |
| 134 | case fd.IsList(): |
| 135 | // If input is not a list, turn it into a list. |
| 136 | var items []text.Value |
| 137 | if tval.Type() != text.List { |
| 138 | items = []text.Value{tval} |
| 139 | } else { |
| 140 | items = tval.List() |
| 141 | } |
| 142 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 143 | list := m.Mutable(fd).List() |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 144 | if err := o.unmarshalList(items, fd, list); !nerr.Merge(err) { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 145 | return err |
| 146 | } |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 147 | case fd.IsMap(): |
| 148 | // If input is not a list, turn it into a list. |
| 149 | var items []text.Value |
| 150 | if tval.Type() != text.List { |
| 151 | items = []text.Value{tval} |
| 152 | } else { |
| 153 | items = tval.List() |
| 154 | } |
| 155 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 156 | mmap := m.Mutable(fd).Map() |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 157 | if err := o.unmarshalMap(items, fd, mmap); !nerr.Merge(err) { |
| 158 | return err |
| 159 | } |
| 160 | default: |
Herbie Ong | 8a1d460 | 2019-04-02 20:19:36 -0700 | [diff] [blame] | 161 | // If field is a oneof, check if it has already been set. |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 162 | if od := fd.ContainingOneof(); od != nil { |
Herbie Ong | 8a1d460 | 2019-04-02 20:19:36 -0700 | [diff] [blame] | 163 | idx := uint64(od.Index()) |
| 164 | if seenOneofs.Has(idx) { |
| 165 | return errors.New("oneof %v is already set", od.FullName()) |
| 166 | } |
| 167 | seenOneofs.Set(idx) |
| 168 | } |
| 169 | |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 170 | // Required or optional fields. |
| 171 | num := uint64(fd.Number()) |
| 172 | if seenNums.Has(num) { |
| 173 | return errors.New("non-repeated field %v is repeated", fd.FullName()) |
| 174 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 175 | if err := o.unmarshalSingular(tval, fd, m); !nerr.Merge(err) { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 176 | return err |
| 177 | } |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 178 | seenNums.Set(num) |
| 179 | } |
| 180 | } |
| 181 | |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 182 | return nerr.E |
| 183 | } |
| 184 | |
Herbie Ong | 6470ea6 | 2019-01-07 18:56:57 -0800 | [diff] [blame] | 185 | // findExtension returns protoreflect.ExtensionType from the Resolver if found. |
| 186 | func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) { |
| 187 | xt, err := o.Resolver.FindExtensionByName(xtName) |
| 188 | if err == nil { |
| 189 | return xt, nil |
| 190 | } |
| 191 | |
| 192 | // Check if this is a MessageSet extension field. |
| 193 | xt, err = o.Resolver.FindExtensionByName(xtName + ".message_set_extension") |
| 194 | if err == nil && isMessageSetExtension(xt) { |
| 195 | return xt, nil |
| 196 | } |
| 197 | return nil, protoregistry.NotFound |
| 198 | } |
| 199 | |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 200 | // unmarshalSingular unmarshals given text.Value into the non-repeated field. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 201 | func (o UnmarshalOptions) unmarshalSingular(input text.Value, fd pref.FieldDescriptor, m pref.Message) error { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 202 | var nerr errors.NonFatal |
| 203 | var val pref.Value |
| 204 | switch fd.Kind() { |
| 205 | case pref.MessageKind, pref.GroupKind: |
| 206 | if input.Type() != text.Message { |
| 207 | return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input) |
| 208 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 209 | m2 := m.NewMessage(fd) |
| 210 | if err := o.unmarshalMessage(input.Message(), m2); !nerr.Merge(err) { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 211 | return err |
| 212 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 213 | val = pref.ValueOf(m2) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 214 | default: |
| 215 | var err error |
| 216 | val, err = unmarshalScalar(input, fd) |
| 217 | if !nerr.Merge(err) { |
| 218 | return err |
| 219 | } |
| 220 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 221 | m.Set(fd, val) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 222 | |
| 223 | return nerr.E |
| 224 | } |
| 225 | |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 226 | // unmarshalScalar converts the given text.Value to a scalar/enum protoreflect.Value specified in |
| 227 | // the given FieldDescriptor. Caller should not pass in a FieldDescriptor for a message/group kind. |
| 228 | func unmarshalScalar(input text.Value, fd pref.FieldDescriptor) (pref.Value, error) { |
| 229 | const b32 = false |
| 230 | const b64 = true |
| 231 | |
| 232 | switch kind := fd.Kind(); kind { |
| 233 | case pref.BoolKind: |
| 234 | if b, ok := input.Bool(); ok { |
| 235 | return pref.ValueOf(bool(b)), nil |
| 236 | } |
| 237 | case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind: |
| 238 | if n, ok := input.Int(b32); ok { |
| 239 | return pref.ValueOf(int32(n)), nil |
| 240 | } |
| 241 | case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind: |
| 242 | if n, ok := input.Int(b64); ok { |
| 243 | return pref.ValueOf(int64(n)), nil |
| 244 | } |
| 245 | case pref.Uint32Kind, pref.Fixed32Kind: |
| 246 | if n, ok := input.Uint(b32); ok { |
| 247 | return pref.ValueOf(uint32(n)), nil |
| 248 | } |
| 249 | case pref.Uint64Kind, pref.Fixed64Kind: |
| 250 | if n, ok := input.Uint(b64); ok { |
| 251 | return pref.ValueOf(uint64(n)), nil |
| 252 | } |
| 253 | case pref.FloatKind: |
Herbie Ong | 250c6ea | 2019-03-12 20:55:10 -0700 | [diff] [blame] | 254 | if n, ok := input.Float(b32); ok { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 255 | return pref.ValueOf(float32(n)), nil |
| 256 | } |
| 257 | case pref.DoubleKind: |
Herbie Ong | 250c6ea | 2019-03-12 20:55:10 -0700 | [diff] [blame] | 258 | if n, ok := input.Float(b64); ok { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 259 | return pref.ValueOf(float64(n)), nil |
| 260 | } |
| 261 | case pref.StringKind: |
| 262 | if input.Type() == text.String { |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 263 | s := input.String() |
| 264 | if utf8.ValidString(s) { |
| 265 | return pref.ValueOf(s), nil |
| 266 | } |
| 267 | var nerr errors.NonFatal |
| 268 | nerr.AppendInvalidUTF8(string(fd.FullName())) |
| 269 | return pref.ValueOf(s), nerr.E |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 270 | } |
| 271 | case pref.BytesKind: |
| 272 | if input.Type() == text.String { |
| 273 | return pref.ValueOf([]byte(input.String())), nil |
| 274 | } |
| 275 | case pref.EnumKind: |
| 276 | // If input is int32, use directly. |
| 277 | if n, ok := input.Int(b32); ok { |
| 278 | return pref.ValueOf(pref.EnumNumber(n)), nil |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 279 | } |
| 280 | if name, ok := input.Name(); ok { |
| 281 | // Lookup EnumNumber based on name. |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 282 | if enumVal := fd.Enum().Values().ByName(name); enumVal != nil { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 283 | return pref.ValueOf(enumVal.Number()), nil |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | default: |
| 287 | panic(fmt.Sprintf("invalid scalar kind %v", kind)) |
| 288 | } |
| 289 | |
| 290 | return pref.Value{}, errors.New("%v contains invalid scalar value: %v", fd.FullName(), input) |
| 291 | } |
| 292 | |
| 293 | // unmarshalList unmarshals given []text.Value into given protoreflect.List. |
| 294 | func (o UnmarshalOptions) unmarshalList(inputList []text.Value, fd pref.FieldDescriptor, list pref.List) error { |
| 295 | var nerr errors.NonFatal |
| 296 | |
| 297 | switch fd.Kind() { |
| 298 | case pref.MessageKind, pref.GroupKind: |
| 299 | for _, input := range inputList { |
| 300 | if input.Type() != text.Message { |
| 301 | return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input) |
| 302 | } |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 303 | m := list.NewMessage() |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 304 | if err := o.unmarshalMessage(input.Message(), m); !nerr.Merge(err) { |
| 305 | return err |
| 306 | } |
| 307 | list.Append(pref.ValueOf(m)) |
| 308 | } |
| 309 | default: |
| 310 | for _, input := range inputList { |
| 311 | val, err := unmarshalScalar(input, fd) |
| 312 | if !nerr.Merge(err) { |
| 313 | return err |
| 314 | } |
| 315 | list.Append(val) |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | return nerr.E |
| 320 | } |
| 321 | |
| 322 | // unmarshalMap unmarshals given []text.Value into given protoreflect.Map. |
| 323 | func (o UnmarshalOptions) unmarshalMap(input []text.Value, fd pref.FieldDescriptor, mmap pref.Map) error { |
| 324 | var nerr errors.NonFatal |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 325 | |
| 326 | // Determine ahead whether map entry is a scalar type or a message type in order to call the |
| 327 | // appropriate unmarshalMapValue func inside the for loop below. |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 328 | unmarshalMapValue := unmarshalMapScalarValue |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 329 | switch fd.MapValue().Kind() { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 330 | case pref.MessageKind, pref.GroupKind: |
| 331 | unmarshalMapValue = o.unmarshalMapMessageValue |
| 332 | } |
| 333 | |
| 334 | for _, entry := range input { |
| 335 | if entry.Type() != text.Message { |
| 336 | return errors.New("%v contains invalid map entry: %v", fd.FullName(), entry) |
| 337 | } |
| 338 | tkey, tval, err := parseMapEntry(entry.Message(), fd.FullName()) |
| 339 | if !nerr.Merge(err) { |
| 340 | return err |
| 341 | } |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 342 | pkey, err := unmarshalMapKey(tkey, fd.MapKey()) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 343 | if !nerr.Merge(err) { |
| 344 | return err |
| 345 | } |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 346 | err = unmarshalMapValue(tval, pkey, fd.MapValue(), mmap) |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 347 | if !nerr.Merge(err) { |
| 348 | return err |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | return nerr.E |
| 353 | } |
| 354 | |
| 355 | // parseMapEntry parses [][2]text.Value for field names key and value, and return corresponding |
| 356 | // field values. If there are duplicate field names, the value for the last field is returned. If |
| 357 | // the field name does not exist, it will return the zero value of text.Value. It will return an |
| 358 | // error if there are unknown field names. |
| 359 | func parseMapEntry(mapEntry [][2]text.Value, name pref.FullName) (key text.Value, value text.Value, err error) { |
| 360 | for _, field := range mapEntry { |
| 361 | keyStr, ok := field[0].Name() |
| 362 | if ok { |
| 363 | switch keyStr { |
| 364 | case "key": |
| 365 | if key.Type() != 0 { |
| 366 | return key, value, errors.New("%v contains duplicate key field", name) |
| 367 | } |
| 368 | key = field[1] |
| 369 | case "value": |
| 370 | if value.Type() != 0 { |
| 371 | return key, value, errors.New("%v contains duplicate value field", name) |
| 372 | } |
| 373 | value = field[1] |
| 374 | default: |
| 375 | ok = false |
| 376 | } |
| 377 | } |
| 378 | if !ok { |
| 379 | // TODO: Do not return error if ignore unknown option is added and enabled. |
| 380 | return key, value, errors.New("%v contains unknown map entry name: %v", name, field[0]) |
| 381 | } |
| 382 | } |
| 383 | return key, value, nil |
| 384 | } |
| 385 | |
| 386 | // unmarshalMapKey converts given text.Value into a protoreflect.MapKey. A map key type is any |
| 387 | // integral or string type. |
| 388 | func unmarshalMapKey(input text.Value, fd pref.FieldDescriptor) (pref.MapKey, error) { |
| 389 | // If input is not set, use the zero value. |
| 390 | if input.Type() == 0 { |
| 391 | return fd.Default().MapKey(), nil |
| 392 | } |
| 393 | |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 394 | var nerr errors.NonFatal |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 395 | val, err := unmarshalScalar(input, fd) |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 396 | if !nerr.Merge(err) { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 397 | return pref.MapKey{}, errors.New("%v contains invalid key: %v", fd.FullName(), input) |
| 398 | } |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 399 | return val.MapKey(), nerr.E |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // unmarshalMapMessageValue unmarshals given message-type text.Value into a protoreflect.Map for |
| 403 | // the given MapKey. |
| 404 | func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, _ pref.FieldDescriptor, mmap pref.Map) error { |
| 405 | var nerr errors.NonFatal |
| 406 | var value [][2]text.Value |
| 407 | if input.Type() != 0 { |
| 408 | value = input.Message() |
| 409 | } |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 410 | m := mmap.NewMessage() |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 411 | if err := o.unmarshalMessage(value, m); !nerr.Merge(err) { |
| 412 | return err |
| 413 | } |
| 414 | mmap.Set(pkey, pref.ValueOf(m)) |
| 415 | return nerr.E |
| 416 | } |
| 417 | |
| 418 | // unmarshalMapScalarValue unmarshals given scalar-type text.Value into a protoreflect.Map |
| 419 | // for the given MapKey. |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 420 | func unmarshalMapScalarValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error { |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 421 | var nerr errors.NonFatal |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 422 | var val pref.Value |
| 423 | if input.Type() == 0 { |
| 424 | val = fd.Default() |
| 425 | } else { |
| 426 | var err error |
| 427 | val, err = unmarshalScalar(input, fd) |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 428 | if !nerr.Merge(err) { |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 429 | return err |
| 430 | } |
| 431 | } |
| 432 | mmap.Set(pkey, val) |
Herbie Ong | 21a3974 | 2019-04-08 17:32:44 -0700 | [diff] [blame] | 433 | return nerr.E |
Herbie Ong | 800c990 | 2018-12-06 15:28:53 -0800 | [diff] [blame] | 434 | } |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 435 | |
| 436 | // isExpandedAny returns true if given [][2]text.Value may be an expanded Any that contains only one |
| 437 | // field with key type of text.String type and value type of text.Message. |
| 438 | func isExpandedAny(tmsg [][2]text.Value) bool { |
| 439 | if len(tmsg) != 1 { |
| 440 | return false |
| 441 | } |
| 442 | |
| 443 | field := tmsg[0] |
| 444 | return field[0].Type() == text.String && field[1].Type() == text.Message |
| 445 | } |
| 446 | |
| 447 | // unmarshalAny unmarshals an expanded Any textproto. This method assumes that the given |
| 448 | // tfield has key type of text.String and value type of text.Message. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 449 | func (o UnmarshalOptions) unmarshalAny(tfield [2]text.Value, m pref.Message) error { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 450 | var nerr errors.NonFatal |
| 451 | |
| 452 | typeURL := tfield[0].String() |
| 453 | value := tfield[1].Message() |
| 454 | |
| 455 | mt, err := o.Resolver.FindMessageByURL(typeURL) |
| 456 | if !nerr.Merge(err) { |
| 457 | return errors.New("unable to resolve message [%v]: %v", typeURL, err) |
| 458 | } |
| 459 | // Create new message for the embedded message type and unmarshal the |
| 460 | // value into it. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 461 | m2 := mt.New() |
| 462 | if err := o.unmarshalMessage(value, m2); !nerr.Merge(err) { |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 463 | return err |
| 464 | } |
| 465 | // Serialize the embedded message and assign the resulting bytes to the value field. |
Herbie Ong | 9c10045 | 2019-05-06 18:45:33 -0700 | [diff] [blame] | 466 | // TODO: If binary marshaling returns required not set error, need to |
| 467 | // return another required not set error that contains both the path to this |
| 468 | // field and the path inside the embedded message. |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 469 | b, err := proto.MarshalOptions{ |
| 470 | AllowPartial: o.AllowPartial, |
| 471 | Deterministic: true, |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 472 | }.Marshal(m2.Interface()) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 473 | if !nerr.Merge(err) { |
| 474 | return err |
| 475 | } |
| 476 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame^] | 477 | fds := m.Descriptor().Fields() |
| 478 | fdType := fds.ByNumber(fieldnum.Any_TypeUrl) |
| 479 | fdValue := fds.ByNumber(fieldnum.Any_Value) |
| 480 | |
| 481 | m.Set(fdType, pref.ValueOf(typeURL)) |
| 482 | m.Set(fdValue, pref.ValueOf(b)) |
Herbie Ong | 66c365c | 2019-01-04 14:08:41 -0800 | [diff] [blame] | 483 | |
| 484 | return nerr.E |
| 485 | } |