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