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