Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -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 | |
| 5 | package proto |
| 6 | |
| 7 | import ( |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 8 | "google.golang.org/protobuf/internal/encoding/wire" |
| 9 | "google.golang.org/protobuf/internal/errors" |
| 10 | "google.golang.org/protobuf/internal/pragma" |
| 11 | "google.golang.org/protobuf/reflect/protoreflect" |
| 12 | "google.golang.org/protobuf/reflect/protoregistry" |
| 13 | "google.golang.org/protobuf/runtime/protoiface" |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | // UnmarshalOptions configures the unmarshaler. |
| 17 | // |
| 18 | // Example usage: |
| 19 | // err := UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, m) |
| 20 | type UnmarshalOptions struct { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 21 | // AllowPartial accepts input for messages that will result in missing |
| 22 | // required fields. If AllowPartial is false (the default), Unmarshal will |
| 23 | // return an error if there are any missing required fields. |
| 24 | AllowPartial bool |
| 25 | |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 26 | // If DiscardUnknown is set, unknown fields are ignored. |
| 27 | DiscardUnknown bool |
| 28 | |
Joe Tsai | db38ddd | 2019-05-07 15:14:40 -0700 | [diff] [blame] | 29 | // Resolver is used for looking up types when unmarshaling extension fields. |
| 30 | // If nil, this defaults to using protoregistry.GlobalTypes. |
Joe Tsai | 1c28304 | 2019-05-14 14:28:19 -0700 | [diff] [blame] | 31 | Resolver interface { |
| 32 | protoregistry.ExtensionTypeResolver |
| 33 | } |
Joe Tsai | db38ddd | 2019-05-07 15:14:40 -0700 | [diff] [blame] | 34 | |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 35 | pragma.NoUnkeyedLiterals |
| 36 | } |
| 37 | |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 38 | var _ = protoiface.UnmarshalOptions(UnmarshalOptions{}) |
| 39 | |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 40 | // Unmarshal parses the wire-format message in b and places the result in m. |
| 41 | func Unmarshal(b []byte, m Message) error { |
| 42 | return UnmarshalOptions{}.Unmarshal(b, m) |
| 43 | } |
| 44 | |
| 45 | // Unmarshal parses the wire-format message in b and places the result in m. |
| 46 | func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error { |
Joe Tsai | db38ddd | 2019-05-07 15:14:40 -0700 | [diff] [blame] | 47 | if o.Resolver == nil { |
| 48 | o.Resolver = protoregistry.GlobalTypes |
| 49 | } |
| 50 | |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 51 | // TODO: Reset m? |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 52 | err := o.unmarshalMessageFast(b, m) |
| 53 | if err == errInternalNoFast { |
| 54 | err = o.unmarshalMessage(b, m.ProtoReflect()) |
| 55 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 56 | if err != nil { |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 57 | return err |
| 58 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 59 | if o.AllowPartial { |
| 60 | return nil |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 61 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 62 | return IsInitialized(m) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 65 | func (o UnmarshalOptions) unmarshalMessageFast(b []byte, m Message) error { |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 66 | methods := protoMethods(m) |
| 67 | if methods == nil || methods.Unmarshal == nil { |
| 68 | return errInternalNoFast |
| 69 | } |
| 70 | return methods.Unmarshal(b, m, protoiface.UnmarshalOptions(o)) |
| 71 | } |
| 72 | |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 73 | func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error { |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 74 | messageDesc := m.Descriptor() |
| 75 | fieldDescs := messageDesc.Fields() |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 76 | for len(b) > 0 { |
| 77 | // Parse the tag (field number and wire type). |
| 78 | num, wtyp, tagLen := wire.ConsumeTag(b) |
| 79 | if tagLen < 0 { |
| 80 | return wire.ParseError(tagLen) |
| 81 | } |
| 82 | |
| 83 | // Parse the field value. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 84 | fd := fieldDescs.ByNumber(num) |
| 85 | if fd == nil && messageDesc.ExtensionRanges().Has(num) { |
| 86 | extType, err := o.Resolver.FindExtensionByNumber(messageDesc.FullName(), num) |
| 87 | if err != nil && err != protoregistry.NotFound { |
| 88 | return err |
Joe Tsai | db38ddd | 2019-05-07 15:14:40 -0700 | [diff] [blame] | 89 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 90 | fd = extType |
Damien Neil | d068d30 | 2018-12-17 14:06:08 -0800 | [diff] [blame] | 91 | } |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 92 | var err error |
| 93 | var valLen int |
| 94 | switch { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 95 | case fd == nil: |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 96 | err = errUnknown |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 97 | case fd.IsList(): |
| 98 | valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd) |
| 99 | case fd.IsMap(): |
| 100 | valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 101 | default: |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 102 | valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 103 | } |
| 104 | if err == errUnknown { |
| 105 | valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:]) |
| 106 | if valLen < 0 { |
| 107 | return wire.ParseError(valLen) |
| 108 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 109 | m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...)) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 110 | } else if err != nil { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 111 | return err |
| 112 | } |
| 113 | b = b[tagLen+valLen:] |
| 114 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 115 | return nil |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 118 | func (o UnmarshalOptions) unmarshalSingular(b []byte, wtyp wire.Type, m protoreflect.Message, fd protoreflect.FieldDescriptor) (n int, err error) { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 119 | v, n, err := o.unmarshalScalar(b, wtyp, fd) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 120 | if err != nil { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 121 | return 0, err |
| 122 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 123 | switch fd.Kind() { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 124 | case protoreflect.GroupKind, protoreflect.MessageKind: |
| 125 | // Messages are merged with any existing message value, |
| 126 | // unless the message is part of a oneof. |
| 127 | // |
| 128 | // TODO: C++ merges into oneofs, while v1 does not. |
| 129 | // Evaluate which behavior to pick. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 130 | var m2 protoreflect.Message |
| 131 | if m.Has(fd) && fd.ContainingOneof() == nil { |
| 132 | m2 = m.Mutable(fd).Message() |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 133 | } else { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 134 | m2 = m.NewMessage(fd) |
| 135 | m.Set(fd, protoreflect.ValueOf(m2)) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 136 | } |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 137 | // Pass up errors (fatal and otherwise). |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 138 | if err := o.unmarshalMessage(v.Bytes(), m2); err != nil { |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 139 | return n, err |
| 140 | } |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 141 | default: |
| 142 | // Non-message scalars replace the previous value. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 143 | m.Set(fd, v) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 144 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 145 | return n, nil |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 148 | func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp wire.Type, mapv protoreflect.Map, fd protoreflect.FieldDescriptor) (n int, err error) { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 149 | if wtyp != wire.BytesType { |
| 150 | return 0, errUnknown |
| 151 | } |
| 152 | b, n = wire.ConsumeBytes(b) |
| 153 | if n < 0 { |
| 154 | return 0, wire.ParseError(n) |
| 155 | } |
| 156 | var ( |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 157 | keyField = fd.MapKey() |
| 158 | valField = fd.MapValue() |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 159 | key protoreflect.Value |
| 160 | val protoreflect.Value |
| 161 | haveKey bool |
| 162 | haveVal bool |
| 163 | ) |
| 164 | switch valField.Kind() { |
| 165 | case protoreflect.GroupKind, protoreflect.MessageKind: |
Joe Tsai | 3bc7d6f | 2019-01-09 02:57:13 -0800 | [diff] [blame] | 166 | val = protoreflect.ValueOf(mapv.NewMessage()) |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 167 | } |
| 168 | // Map entries are represented as a two-element message with fields |
| 169 | // containing the key and value. |
| 170 | for len(b) > 0 { |
| 171 | num, wtyp, n := wire.ConsumeTag(b) |
| 172 | if n < 0 { |
| 173 | return 0, wire.ParseError(n) |
| 174 | } |
| 175 | b = b[n:] |
| 176 | err = errUnknown |
| 177 | switch num { |
| 178 | case 1: |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 179 | key, n, err = o.unmarshalScalar(b, wtyp, keyField) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 180 | if err != nil { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 181 | break |
| 182 | } |
| 183 | haveKey = true |
| 184 | case 2: |
| 185 | var v protoreflect.Value |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 186 | v, n, err = o.unmarshalScalar(b, wtyp, valField) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 187 | if err != nil { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 188 | break |
| 189 | } |
| 190 | switch valField.Kind() { |
| 191 | case protoreflect.GroupKind, protoreflect.MessageKind: |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 192 | if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 193 | return 0, err |
| 194 | } |
| 195 | default: |
| 196 | val = v |
| 197 | } |
| 198 | haveVal = true |
| 199 | } |
| 200 | if err == errUnknown { |
| 201 | n = wire.ConsumeFieldValue(num, wtyp, b) |
| 202 | if n < 0 { |
| 203 | return 0, wire.ParseError(n) |
| 204 | } |
Damien Neil | bc310b5 | 2019-04-11 11:46:55 -0700 | [diff] [blame] | 205 | } else if err != nil { |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 206 | return 0, err |
| 207 | } |
| 208 | b = b[n:] |
| 209 | } |
| 210 | // Every map entry should have entries for key and value, but this is not strictly required. |
| 211 | if !haveKey { |
| 212 | key = keyField.Default() |
| 213 | } |
| 214 | if !haveVal { |
| 215 | switch valField.Kind() { |
| 216 | case protoreflect.GroupKind, protoreflect.MessageKind: |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 217 | default: |
| 218 | val = valField.Default() |
| 219 | } |
| 220 | } |
| 221 | mapv.Set(key.MapKey(), val) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 222 | return n, nil |
Damien Neil | ba23aa5 | 2018-12-07 14:38:17 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // errUnknown is used internally to indicate fields which should be added |
| 226 | // to the unknown field set of a message. It is never returned from an exported |
| 227 | // function. |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 228 | var errUnknown = errors.New("BUG: internal error (unknown)") |