blob: 89741398e7f9bb8c5c560311ce187350a61fdf38 [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 }
Damien Neil92f76182019-08-02 16:58:08 -070091 if extType != nil {
Damien Neil79bfdbe2019-08-28 11:08:22 -070092 fd = extType.TypeDescriptor()
Damien Neil92f76182019-08-02 16:58:08 -070093 }
Damien Neild068d302018-12-17 14:06:08 -080094 }
Damien Neilba23aa52018-12-07 14:38:17 -080095 var err error
96 var valLen int
97 switch {
Joe Tsai378c1322019-04-25 23:48:08 -070098 case fd == nil:
Damien Neilba23aa52018-12-07 14:38:17 -080099 err = errUnknown
Joe Tsai378c1322019-04-25 23:48:08 -0700100 case fd.IsList():
101 valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
102 case fd.IsMap():
103 valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
Joe Tsaiac31a352019-05-13 14:32:56 -0700104 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700105 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
Damien Neilba23aa52018-12-07 14:38:17 -0800106 }
107 if err == errUnknown {
108 valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
109 if valLen < 0 {
110 return wire.ParseError(valLen)
111 }
Joe Tsai378c1322019-04-25 23:48:08 -0700112 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
Damien Neil8c86fc52019-06-19 09:28:29 -0700113 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800114 return err
115 }
116 b = b[tagLen+valLen:]
117 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700118 return nil
Damien Neilba23aa52018-12-07 14:38:17 -0800119}
120
Joe Tsai378c1322019-04-25 23:48:08 -0700121func (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 -0700122 v, n, err := o.unmarshalScalar(b, wtyp, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700123 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800124 return 0, err
125 }
Joe Tsai378c1322019-04-25 23:48:08 -0700126 switch fd.Kind() {
Damien Neilba23aa52018-12-07 14:38:17 -0800127 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai6c286742019-07-11 23:15:05 -0700128 m2 := m.Mutable(fd).Message()
Damien Neil8c86fc52019-06-19 09:28:29 -0700129 if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
Damien Neilbc310b52019-04-11 11:46:55 -0700130 return n, err
131 }
Damien Neilba23aa52018-12-07 14:38:17 -0800132 default:
133 // Non-message scalars replace the previous value.
Joe Tsai378c1322019-04-25 23:48:08 -0700134 m.Set(fd, v)
Damien Neilba23aa52018-12-07 14:38:17 -0800135 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700136 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800137}
138
Joe Tsai378c1322019-04-25 23:48:08 -0700139func (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 -0800140 if wtyp != wire.BytesType {
141 return 0, errUnknown
142 }
143 b, n = wire.ConsumeBytes(b)
144 if n < 0 {
145 return 0, wire.ParseError(n)
146 }
147 var (
Joe Tsai378c1322019-04-25 23:48:08 -0700148 keyField = fd.MapKey()
149 valField = fd.MapValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800150 key protoreflect.Value
151 val protoreflect.Value
152 haveKey bool
153 haveVal bool
154 )
155 switch valField.Kind() {
156 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neild91c4222019-09-04 10:46:00 -0700157 val = mapv.NewValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800158 }
159 // Map entries are represented as a two-element message with fields
160 // containing the key and value.
161 for len(b) > 0 {
162 num, wtyp, n := wire.ConsumeTag(b)
163 if n < 0 {
164 return 0, wire.ParseError(n)
165 }
166 b = b[n:]
167 err = errUnknown
168 switch num {
169 case 1:
Joe Tsai378c1322019-04-25 23:48:08 -0700170 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700171 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800172 break
173 }
174 haveKey = true
175 case 2:
176 var v protoreflect.Value
Joe Tsai378c1322019-04-25 23:48:08 -0700177 v, n, err = o.unmarshalScalar(b, wtyp, valField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700178 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800179 break
180 }
181 switch valField.Kind() {
182 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neil8c86fc52019-06-19 09:28:29 -0700183 if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800184 return 0, err
185 }
186 default:
187 val = v
188 }
189 haveVal = true
190 }
191 if err == errUnknown {
192 n = wire.ConsumeFieldValue(num, wtyp, b)
193 if n < 0 {
194 return 0, wire.ParseError(n)
195 }
Damien Neilbc310b52019-04-11 11:46:55 -0700196 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800197 return 0, err
198 }
199 b = b[n:]
200 }
201 // Every map entry should have entries for key and value, but this is not strictly required.
202 if !haveKey {
203 key = keyField.Default()
204 }
205 if !haveVal {
206 switch valField.Kind() {
207 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neilba23aa52018-12-07 14:38:17 -0800208 default:
209 val = valField.Default()
210 }
211 }
212 mapv.Set(key.MapKey(), val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700213 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800214}
215
216// errUnknown is used internally to indicate fields which should be added
217// to the unknown field set of a message. It is never returned from an exported
218// function.
Damien Neil96c229a2019-04-03 12:17:24 -0700219var errUnknown = errors.New("BUG: internal error (unknown)")