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