blob: beba80bc598c5f3f72269052120a54620ce68983 [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?
Damien Neil4686e232019-04-05 13:31:40 -070052 err := o.unmarshalMessageFast(b, m)
53 if err == errInternalNoFast {
54 err = o.unmarshalMessage(b, m.ProtoReflect())
55 }
Damien Neil8c86fc52019-06-19 09:28:29 -070056 if err != nil {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070057 return err
58 }
Damien Neil8c86fc52019-06-19 09:28:29 -070059 if o.AllowPartial {
60 return nil
Damien Neil4686e232019-04-05 13:31:40 -070061 }
Damien Neil8c86fc52019-06-19 09:28:29 -070062 return IsInitialized(m)
Damien Neilba23aa52018-12-07 14:38:17 -080063}
64
Damien Neil0d3e8cc2019-04-01 13:31:55 -070065func (o UnmarshalOptions) unmarshalMessageFast(b []byte, m Message) error {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070066 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 Neilba23aa52018-12-07 14:38:17 -080073func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
Joe Tsai0fc49f82019-05-01 12:29:25 -070074 messageDesc := m.Descriptor()
75 fieldDescs := messageDesc.Fields()
Damien Neilba23aa52018-12-07 14:38:17 -080076 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 Tsai378c1322019-04-25 23:48:08 -070084 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 Tsaidb38ddd2019-05-07 15:14:40 -070089 }
Joe Tsai378c1322019-04-25 23:48:08 -070090 fd = extType
Damien Neild068d302018-12-17 14:06:08 -080091 }
Damien Neilba23aa52018-12-07 14:38:17 -080092 var err error
93 var valLen int
94 switch {
Joe Tsai378c1322019-04-25 23:48:08 -070095 case fd == nil:
Damien Neilba23aa52018-12-07 14:38:17 -080096 err = errUnknown
Joe Tsai378c1322019-04-25 23:48:08 -070097 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 Tsaiac31a352019-05-13 14:32:56 -0700101 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700102 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
Damien Neilba23aa52018-12-07 14:38:17 -0800103 }
104 if err == errUnknown {
105 valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
106 if valLen < 0 {
107 return wire.ParseError(valLen)
108 }
Joe Tsai378c1322019-04-25 23:48:08 -0700109 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
Damien Neil8c86fc52019-06-19 09:28:29 -0700110 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800111 return err
112 }
113 b = b[tagLen+valLen:]
114 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700115 return nil
Damien Neilba23aa52018-12-07 14:38:17 -0800116}
117
Joe Tsai378c1322019-04-25 23:48:08 -0700118func (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 -0700119 v, n, err := o.unmarshalScalar(b, wtyp, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700120 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800121 return 0, err
122 }
Joe Tsai378c1322019-04-25 23:48:08 -0700123 switch fd.Kind() {
Damien Neilba23aa52018-12-07 14:38:17 -0800124 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai6c286742019-07-11 23:15:05 -0700125 m2 := m.Mutable(fd).Message()
Damien Neil8c86fc52019-06-19 09:28:29 -0700126 if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
Damien Neilbc310b52019-04-11 11:46:55 -0700127 return n, err
128 }
Damien Neilba23aa52018-12-07 14:38:17 -0800129 default:
130 // Non-message scalars replace the previous value.
Joe Tsai378c1322019-04-25 23:48:08 -0700131 m.Set(fd, v)
Damien Neilba23aa52018-12-07 14:38:17 -0800132 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700133 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800134}
135
Joe Tsai378c1322019-04-25 23:48:08 -0700136func (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 -0800137 if wtyp != wire.BytesType {
138 return 0, errUnknown
139 }
140 b, n = wire.ConsumeBytes(b)
141 if n < 0 {
142 return 0, wire.ParseError(n)
143 }
144 var (
Joe Tsai378c1322019-04-25 23:48:08 -0700145 keyField = fd.MapKey()
146 valField = fd.MapValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800147 key protoreflect.Value
148 val protoreflect.Value
149 haveKey bool
150 haveVal bool
151 )
152 switch valField.Kind() {
153 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800154 val = protoreflect.ValueOf(mapv.NewMessage())
Damien Neilba23aa52018-12-07 14:38:17 -0800155 }
156 // Map entries are represented as a two-element message with fields
157 // containing the key and value.
158 for len(b) > 0 {
159 num, wtyp, n := wire.ConsumeTag(b)
160 if n < 0 {
161 return 0, wire.ParseError(n)
162 }
163 b = b[n:]
164 err = errUnknown
165 switch num {
166 case 1:
Joe Tsai378c1322019-04-25 23:48:08 -0700167 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700168 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800169 break
170 }
171 haveKey = true
172 case 2:
173 var v protoreflect.Value
Joe Tsai378c1322019-04-25 23:48:08 -0700174 v, n, err = o.unmarshalScalar(b, wtyp, valField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700175 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800176 break
177 }
178 switch valField.Kind() {
179 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neil8c86fc52019-06-19 09:28:29 -0700180 if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800181 return 0, err
182 }
183 default:
184 val = v
185 }
186 haveVal = true
187 }
188 if err == errUnknown {
189 n = wire.ConsumeFieldValue(num, wtyp, b)
190 if n < 0 {
191 return 0, wire.ParseError(n)
192 }
Damien Neilbc310b52019-04-11 11:46:55 -0700193 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800194 return 0, err
195 }
196 b = b[n:]
197 }
198 // Every map entry should have entries for key and value, but this is not strictly required.
199 if !haveKey {
200 key = keyField.Default()
201 }
202 if !haveVal {
203 switch valField.Kind() {
204 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neilba23aa52018-12-07 14:38:17 -0800205 default:
206 val = valField.Default()
207 }
208 }
209 mapv.Set(key.MapKey(), val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700210 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800211}
212
213// errUnknown is used internally to indicate fields which should be added
214// to the unknown field set of a message. It is never returned from an exported
215// function.
Damien Neil96c229a2019-04-03 12:17:24 -0700216var errUnknown = errors.New("BUG: internal error (unknown)")