blob: 6c2b9b7b491185a270fd223862eef8199201604a [file] [log] [blame]
Damien Neilba23aa52018-12-07 14:38:17 -08001// 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
5package proto
6
7import (
Damien Neile89e6242019-05-13 23:55:40 -07008 "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 Neilba23aa52018-12-07 14:38:17 -080014)
15
16// UnmarshalOptions configures the unmarshaler.
17//
18// Example usage:
19// err := UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, m)
20type UnmarshalOptions struct {
Damien Neil96c229a2019-04-03 12:17:24 -070021 // 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 Neilba23aa52018-12-07 14:38:17 -080026 // If DiscardUnknown is set, unknown fields are ignored.
27 DiscardUnknown bool
28
Joe Tsaidb38ddd2019-05-07 15:14:40 -070029 // Resolver is used for looking up types when unmarshaling extension fields.
30 // If nil, this defaults to using protoregistry.GlobalTypes.
Joe Tsai1c283042019-05-14 14:28:19 -070031 Resolver interface {
32 protoregistry.ExtensionTypeResolver
33 }
Joe Tsaidb38ddd2019-05-07 15:14:40 -070034
Damien Neilba23aa52018-12-07 14:38:17 -080035 pragma.NoUnkeyedLiterals
36}
37
Damien Neil0d3e8cc2019-04-01 13:31:55 -070038var _ = protoiface.UnmarshalOptions(UnmarshalOptions{})
39
Damien Neilba23aa52018-12-07 14:38:17 -080040// Unmarshal parses the wire-format message in b and places the result in m.
41func 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.
46func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error {
Joe Tsaidb38ddd2019-05-07 15:14:40 -070047 if o.Resolver == nil {
48 o.Resolver = protoregistry.GlobalTypes
49 }
50
Damien Neilba23aa52018-12-07 14:38:17 -080051 // TODO: Reset m?
Joe Tsai0f81b382019-07-10 23:14:31 -070052 err := o.unmarshalMessage(b, m.ProtoReflect())
Damien Neil8c86fc52019-06-19 09:28:29 -070053 if err != nil {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070054 return err
55 }
Damien Neil8c86fc52019-06-19 09:28:29 -070056 if o.AllowPartial {
57 return nil
Damien Neil4686e232019-04-05 13:31:40 -070058 }
Damien Neil8c86fc52019-06-19 09:28:29 -070059 return IsInitialized(m)
Damien Neilba23aa52018-12-07 14:38:17 -080060}
61
Joe Tsai0f81b382019-07-10 23:14:31 -070062func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
63 if methods := protoMethods(m); methods != nil && methods.Unmarshal != nil {
64 return methods.Unmarshal(b, m, protoiface.UnmarshalOptions(o))
Damien Neil0d3e8cc2019-04-01 13:31:55 -070065 }
Joe Tsai0f81b382019-07-10 23:14:31 -070066 return o.unmarshalMessageSlow(b, m)
Damien Neil0d3e8cc2019-04-01 13:31:55 -070067}
68
Joe Tsai0f81b382019-07-10 23:14:31 -070069func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message) error {
Joe Tsai0fc49f82019-05-01 12:29:25 -070070 messageDesc := m.Descriptor()
71 fieldDescs := messageDesc.Fields()
Damien Neilba23aa52018-12-07 14:38:17 -080072 for len(b) > 0 {
73 // Parse the tag (field number and wire type).
74 num, wtyp, tagLen := wire.ConsumeTag(b)
75 if tagLen < 0 {
76 return wire.ParseError(tagLen)
77 }
78
79 // Parse the field value.
Joe Tsai378c1322019-04-25 23:48:08 -070080 fd := fieldDescs.ByNumber(num)
81 if fd == nil && messageDesc.ExtensionRanges().Has(num) {
82 extType, err := o.Resolver.FindExtensionByNumber(messageDesc.FullName(), num)
83 if err != nil && err != protoregistry.NotFound {
84 return err
Joe Tsaidb38ddd2019-05-07 15:14:40 -070085 }
Joe Tsai378c1322019-04-25 23:48:08 -070086 fd = extType
Damien Neild068d302018-12-17 14:06:08 -080087 }
Damien Neilba23aa52018-12-07 14:38:17 -080088 var err error
89 var valLen int
90 switch {
Joe Tsai378c1322019-04-25 23:48:08 -070091 case fd == nil:
Damien Neilba23aa52018-12-07 14:38:17 -080092 err = errUnknown
Joe Tsai378c1322019-04-25 23:48:08 -070093 case fd.IsList():
94 valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
95 case fd.IsMap():
96 valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
Joe Tsaiac31a352019-05-13 14:32:56 -070097 default:
Joe Tsai378c1322019-04-25 23:48:08 -070098 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
Damien Neilba23aa52018-12-07 14:38:17 -080099 }
100 if err == errUnknown {
101 valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
102 if valLen < 0 {
103 return wire.ParseError(valLen)
104 }
Joe Tsai378c1322019-04-25 23:48:08 -0700105 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
Damien Neil8c86fc52019-06-19 09:28:29 -0700106 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800107 return err
108 }
109 b = b[tagLen+valLen:]
110 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700111 return nil
Damien Neilba23aa52018-12-07 14:38:17 -0800112}
113
Joe Tsai378c1322019-04-25 23:48:08 -0700114func (o UnmarshalOptions) unmarshalSingular(b []byte, wtyp wire.Type, m protoreflect.Message, fd protoreflect.FieldDescriptor) (n int, err error) {
Joe Tsai378c1322019-04-25 23:48:08 -0700115 v, n, err := o.unmarshalScalar(b, wtyp, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700116 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800117 return 0, err
118 }
Joe Tsai378c1322019-04-25 23:48:08 -0700119 switch fd.Kind() {
Damien Neilba23aa52018-12-07 14:38:17 -0800120 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai6c286742019-07-11 23:15:05 -0700121 m2 := m.Mutable(fd).Message()
Damien Neil8c86fc52019-06-19 09:28:29 -0700122 if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
Damien Neilbc310b52019-04-11 11:46:55 -0700123 return n, err
124 }
Damien Neilba23aa52018-12-07 14:38:17 -0800125 default:
126 // Non-message scalars replace the previous value.
Joe Tsai378c1322019-04-25 23:48:08 -0700127 m.Set(fd, v)
Damien Neilba23aa52018-12-07 14:38:17 -0800128 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700129 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800130}
131
Joe Tsai378c1322019-04-25 23:48:08 -0700132func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp wire.Type, mapv protoreflect.Map, fd protoreflect.FieldDescriptor) (n int, err error) {
Damien Neilba23aa52018-12-07 14:38:17 -0800133 if wtyp != wire.BytesType {
134 return 0, errUnknown
135 }
136 b, n = wire.ConsumeBytes(b)
137 if n < 0 {
138 return 0, wire.ParseError(n)
139 }
140 var (
Joe Tsai378c1322019-04-25 23:48:08 -0700141 keyField = fd.MapKey()
142 valField = fd.MapValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800143 key protoreflect.Value
144 val protoreflect.Value
145 haveKey bool
146 haveVal bool
147 )
148 switch valField.Kind() {
149 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800150 val = protoreflect.ValueOf(mapv.NewMessage())
Damien Neilba23aa52018-12-07 14:38:17 -0800151 }
152 // Map entries are represented as a two-element message with fields
153 // containing the key and value.
154 for len(b) > 0 {
155 num, wtyp, n := wire.ConsumeTag(b)
156 if n < 0 {
157 return 0, wire.ParseError(n)
158 }
159 b = b[n:]
160 err = errUnknown
161 switch num {
162 case 1:
Joe Tsai378c1322019-04-25 23:48:08 -0700163 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700164 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800165 break
166 }
167 haveKey = true
168 case 2:
169 var v protoreflect.Value
Joe Tsai378c1322019-04-25 23:48:08 -0700170 v, n, err = o.unmarshalScalar(b, wtyp, valField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700171 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800172 break
173 }
174 switch valField.Kind() {
175 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neil8c86fc52019-06-19 09:28:29 -0700176 if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800177 return 0, err
178 }
179 default:
180 val = v
181 }
182 haveVal = true
183 }
184 if err == errUnknown {
185 n = wire.ConsumeFieldValue(num, wtyp, b)
186 if n < 0 {
187 return 0, wire.ParseError(n)
188 }
Damien Neilbc310b52019-04-11 11:46:55 -0700189 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800190 return 0, err
191 }
192 b = b[n:]
193 }
194 // Every map entry should have entries for key and value, but this is not strictly required.
195 if !haveKey {
196 key = keyField.Default()
197 }
198 if !haveVal {
199 switch valField.Kind() {
200 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neilba23aa52018-12-07 14:38:17 -0800201 default:
202 val = valField.Default()
203 }
204 }
205 mapv.Set(key.MapKey(), val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700206 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800207}
208
209// errUnknown is used internally to indicate fields which should be added
210// to the unknown field set of a message. It is never returned from an exported
211// function.
Damien Neil96c229a2019-04-03 12:17:24 -0700212var errUnknown = errors.New("BUG: internal error (unknown)")