blob: f147e68f68ff27826ea8ee952de1e777945f9caf [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 Neil302cb322019-06-19 15:22:13 -07008 "google.golang.org/protobuf/internal/encoding/messageset"
Damien Neile89e6242019-05-13 23:55:40 -07009 "google.golang.org/protobuf/internal/encoding/wire"
10 "google.golang.org/protobuf/internal/errors"
11 "google.golang.org/protobuf/internal/pragma"
12 "google.golang.org/protobuf/reflect/protoreflect"
13 "google.golang.org/protobuf/reflect/protoregistry"
14 "google.golang.org/protobuf/runtime/protoiface"
Damien Neilba23aa52018-12-07 14:38:17 -080015)
16
17// UnmarshalOptions configures the unmarshaler.
18//
19// Example usage:
20// err := UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, m)
21type UnmarshalOptions struct {
Joe Tsaif8b855d2019-07-12 13:37:59 -070022 pragma.NoUnkeyedLiterals
23
Damien Neil96c229a2019-04-03 12:17:24 -070024 // AllowPartial accepts input for messages that will result in missing
25 // required fields. If AllowPartial is false (the default), Unmarshal will
26 // return an error if there are any missing required fields.
27 AllowPartial bool
28
Damien Neilba23aa52018-12-07 14:38:17 -080029 // If DiscardUnknown is set, unknown fields are ignored.
30 DiscardUnknown bool
31
Joe Tsaidb38ddd2019-05-07 15:14:40 -070032 // Resolver is used for looking up types when unmarshaling extension fields.
33 // If nil, this defaults to using protoregistry.GlobalTypes.
Joe Tsai1c283042019-05-14 14:28:19 -070034 Resolver interface {
35 protoregistry.ExtensionTypeResolver
36 }
Damien Neilba23aa52018-12-07 14:38:17 -080037}
38
Damien Neil0d3e8cc2019-04-01 13:31:55 -070039var _ = protoiface.UnmarshalOptions(UnmarshalOptions{})
40
Damien Neilba23aa52018-12-07 14:38:17 -080041// Unmarshal parses the wire-format message in b and places the result in m.
42func Unmarshal(b []byte, m Message) error {
43 return UnmarshalOptions{}.Unmarshal(b, m)
44}
45
46// Unmarshal parses the wire-format message in b and places the result in m.
47func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error {
Joe Tsaidb38ddd2019-05-07 15:14:40 -070048 if o.Resolver == nil {
49 o.Resolver = protoregistry.GlobalTypes
50 }
51
Damien Neilba23aa52018-12-07 14:38:17 -080052 // TODO: Reset m?
Joe Tsai0f81b382019-07-10 23:14:31 -070053 err := o.unmarshalMessage(b, m.ProtoReflect())
Damien Neil8c86fc52019-06-19 09:28:29 -070054 if err != nil {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070055 return err
56 }
Damien Neil8c86fc52019-06-19 09:28:29 -070057 if o.AllowPartial {
58 return nil
Damien Neil4686e232019-04-05 13:31:40 -070059 }
Damien Neil8c86fc52019-06-19 09:28:29 -070060 return IsInitialized(m)
Damien Neilba23aa52018-12-07 14:38:17 -080061}
62
Joe Tsai0f81b382019-07-10 23:14:31 -070063func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
Joe Tsaif8b855d2019-07-12 13:37:59 -070064 if methods := protoMethods(m); methods != nil && methods.Unmarshal != nil &&
65 !(o.DiscardUnknown && methods.Flags&protoiface.SupportUnmarshalDiscardUnknown == 0) {
Joe Tsai0f81b382019-07-10 23:14:31 -070066 return methods.Unmarshal(b, m, protoiface.UnmarshalOptions(o))
Damien Neil0d3e8cc2019-04-01 13:31:55 -070067 }
Joe Tsai0f81b382019-07-10 23:14:31 -070068 return o.unmarshalMessageSlow(b, m)
Damien Neil0d3e8cc2019-04-01 13:31:55 -070069}
70
Joe Tsai0f81b382019-07-10 23:14:31 -070071func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message) error {
Damien Neil302cb322019-06-19 15:22:13 -070072 md := m.Descriptor()
73 if messageset.IsMessageSet(md) {
74 return unmarshalMessageSet(b, m, o)
75 }
76 fields := md.Fields()
Damien Neilba23aa52018-12-07 14:38:17 -080077 for len(b) > 0 {
78 // Parse the tag (field number and wire type).
79 num, wtyp, tagLen := wire.ConsumeTag(b)
80 if tagLen < 0 {
81 return wire.ParseError(tagLen)
82 }
83
84 // Parse the field value.
Damien Neil302cb322019-06-19 15:22:13 -070085 fd := fields.ByNumber(num)
86 if fd == nil && md.ExtensionRanges().Has(num) {
87 extType, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
Joe Tsai378c1322019-04-25 23:48:08 -070088 if err != nil && err != protoregistry.NotFound {
89 return err
Joe Tsaidb38ddd2019-05-07 15:14:40 -070090 }
Joe Tsai378c1322019-04-25 23:48:08 -070091 fd = extType
Damien Neild068d302018-12-17 14:06:08 -080092 }
Damien Neilba23aa52018-12-07 14:38:17 -080093 var err error
94 var valLen int
95 switch {
Joe Tsai378c1322019-04-25 23:48:08 -070096 case fd == nil:
Damien Neilba23aa52018-12-07 14:38:17 -080097 err = errUnknown
Joe Tsai378c1322019-04-25 23:48:08 -070098 case fd.IsList():
99 valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
100 case fd.IsMap():
101 valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
Joe Tsaiac31a352019-05-13 14:32:56 -0700102 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700103 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
Damien Neilba23aa52018-12-07 14:38:17 -0800104 }
105 if err == errUnknown {
106 valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
107 if valLen < 0 {
108 return wire.ParseError(valLen)
109 }
Joe Tsai378c1322019-04-25 23:48:08 -0700110 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
Damien Neil8c86fc52019-06-19 09:28:29 -0700111 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800112 return err
113 }
114 b = b[tagLen+valLen:]
115 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700116 return nil
Damien Neilba23aa52018-12-07 14:38:17 -0800117}
118
Joe Tsai378c1322019-04-25 23:48:08 -0700119func (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 -0700120 v, n, err := o.unmarshalScalar(b, wtyp, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700121 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800122 return 0, err
123 }
Joe Tsai378c1322019-04-25 23:48:08 -0700124 switch fd.Kind() {
Damien Neilba23aa52018-12-07 14:38:17 -0800125 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai6c286742019-07-11 23:15:05 -0700126 m2 := m.Mutable(fd).Message()
Damien Neil8c86fc52019-06-19 09:28:29 -0700127 if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
Damien Neilbc310b52019-04-11 11:46:55 -0700128 return n, err
129 }
Damien Neilba23aa52018-12-07 14:38:17 -0800130 default:
131 // Non-message scalars replace the previous value.
Joe Tsai378c1322019-04-25 23:48:08 -0700132 m.Set(fd, v)
Damien Neilba23aa52018-12-07 14:38:17 -0800133 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700134 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800135}
136
Joe Tsai378c1322019-04-25 23:48:08 -0700137func (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 -0800138 if wtyp != wire.BytesType {
139 return 0, errUnknown
140 }
141 b, n = wire.ConsumeBytes(b)
142 if n < 0 {
143 return 0, wire.ParseError(n)
144 }
145 var (
Joe Tsai378c1322019-04-25 23:48:08 -0700146 keyField = fd.MapKey()
147 valField = fd.MapValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800148 key protoreflect.Value
149 val protoreflect.Value
150 haveKey bool
151 haveVal bool
152 )
153 switch valField.Kind() {
154 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800155 val = protoreflect.ValueOf(mapv.NewMessage())
Damien Neilba23aa52018-12-07 14:38:17 -0800156 }
157 // Map entries are represented as a two-element message with fields
158 // containing the key and value.
159 for len(b) > 0 {
160 num, wtyp, n := wire.ConsumeTag(b)
161 if n < 0 {
162 return 0, wire.ParseError(n)
163 }
164 b = b[n:]
165 err = errUnknown
166 switch num {
167 case 1:
Joe Tsai378c1322019-04-25 23:48:08 -0700168 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700169 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800170 break
171 }
172 haveKey = true
173 case 2:
174 var v protoreflect.Value
Joe Tsai378c1322019-04-25 23:48:08 -0700175 v, n, err = o.unmarshalScalar(b, wtyp, valField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700176 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800177 break
178 }
179 switch valField.Kind() {
180 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neil8c86fc52019-06-19 09:28:29 -0700181 if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800182 return 0, err
183 }
184 default:
185 val = v
186 }
187 haveVal = true
188 }
189 if err == errUnknown {
190 n = wire.ConsumeFieldValue(num, wtyp, b)
191 if n < 0 {
192 return 0, wire.ParseError(n)
193 }
Damien Neilbc310b52019-04-11 11:46:55 -0700194 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800195 return 0, err
196 }
197 b = b[n:]
198 }
199 // Every map entry should have entries for key and value, but this is not strictly required.
200 if !haveKey {
201 key = keyField.Default()
202 }
203 if !haveVal {
204 switch valField.Kind() {
205 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neilba23aa52018-12-07 14:38:17 -0800206 default:
207 val = valField.Default()
208 }
209 }
210 mapv.Set(key.MapKey(), val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700211 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800212}
213
214// errUnknown is used internally to indicate fields which should be added
215// to the unknown field set of a message. It is never returned from an exported
216// function.
Damien Neil96c229a2019-04-03 12:17:24 -0700217var errUnknown = errors.New("BUG: internal error (unknown)")