blob: 656133f44b219039419afa9a4539f2bff94636aa [file] [log] [blame]
Herbie Ong800c9902018-12-06 15:28:53 -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 textpb
6
7import (
8 "fmt"
9
10 "github.com/golang/protobuf/v2/internal/encoding/text"
11 "github.com/golang/protobuf/v2/internal/errors"
12 "github.com/golang/protobuf/v2/internal/pragma"
13 "github.com/golang/protobuf/v2/internal/set"
14 "github.com/golang/protobuf/v2/proto"
15 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
16)
17
18// Unmarshal reads the given []byte into the given proto.Message.
19// TODO: may want to describe when Unmarshal returns error.
20func Unmarshal(m proto.Message, b []byte) error {
21 return UnmarshalOptions{}.Unmarshal(m, b)
22}
23
24// UnmarshalOptions is a configurable textproto format parser.
25type UnmarshalOptions struct {
26 pragma.NoUnkeyedLiterals
27}
28
29// Unmarshal reads the given []byte and populates the given proto.Message using options in
30// UnmarshalOptions object.
31func (o UnmarshalOptions) Unmarshal(m proto.Message, b []byte) error {
32 var nerr errors.NonFatal
33
34 mr := m.ProtoReflect()
35 // Clear all fields before populating it.
36 // TODO: Determine if this needs to be consistent with jsonpb and binary unmarshal where
37 // behavior is to merge values into existing message. If decision is to not clear the fields
38 // ahead, code will need to be updated properly when merging nested messages.
39 resetMessage(mr)
40
41 // Parse into text.Value of message type.
42 val, err := text.Unmarshal(b)
43 if !nerr.Merge(err) {
44 return err
45 }
46
47 err = o.unmarshalMessage(val.Message(), mr)
48 if !nerr.Merge(err) {
49 return err
50 }
51
52 return nerr.E
53}
54
55// resetMessage clears all fields of given protoreflect.Message.
56// TODO: This should go into the proto package.
57func resetMessage(m pref.Message) {
58 knownFields := m.KnownFields()
59 knownFields.Range(func(num pref.FieldNumber, _ pref.Value) bool {
60 knownFields.Clear(num)
61 return true
62 })
63 unknownFields := m.UnknownFields()
64 unknownFields.Range(func(num pref.FieldNumber, _ pref.RawFields) bool {
65 unknownFields.Set(num, nil)
66 return true
67 })
68
69 extTypes := knownFields.ExtensionTypes()
70 extTypes.Range(func(xt pref.ExtensionType) bool {
71 extTypes.Remove(xt)
72 return true
73 })
74}
75
76// unmarshalMessage unmarshals a [][2]text.Value message into the given protoreflect.Message.
77func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message) error {
78 var nerr errors.NonFatal
79
80 msgType := m.Type()
81 fieldDescs := msgType.Fields()
82 knownFields := m.KnownFields()
83 var reqNums set.Ints
84 var seenNums set.Ints
85
86 for _, tfield := range tmsg {
87 tkey := tfield[0]
88 tval := tfield[1]
89
90 var fd pref.FieldDescriptor
91 if name, ok := tkey.Name(); ok {
92 fd = fieldDescs.ByName(name)
93 }
94 if fd == nil {
95 // TODO: Can provide option to ignore unknown message fields.
96 // TODO: Simply ignore and skip reserved field names.
97 return errors.New("%v contains unknown field: %v", msgType.FullName(), tkey)
98 }
99
100 if cardinality := fd.Cardinality(); cardinality == pref.Repeated {
101 // Map or list fields have cardinality of repeated.
102 if err := o.unmarshalRepeated(tval, fd, knownFields); !nerr.Merge(err) {
103 return err
104 }
105 } else {
106 // Required or optional fields.
107 num := uint64(fd.Number())
108 if seenNums.Has(num) {
109 return errors.New("non-repeated field %v is repeated", fd.FullName())
110 }
111 if err := o.unmarshalSingular(tval, fd, knownFields); !nerr.Merge(err) {
112 return err
113 }
114 if cardinality == pref.Required {
115 reqNums.Set(num)
116 }
117 seenNums.Set(num)
118 }
119 }
120
121 // Check for any missing required fields.
122 allReqNums := msgType.RequiredNumbers()
123 if reqNums.Len() != allReqNums.Len() {
124 for i := 0; i < allReqNums.Len(); i++ {
125 if num := allReqNums.Get(i); !reqNums.Has(uint64(num)) {
126 nerr.AppendRequiredNotSet(string(fieldDescs.ByNumber(num).FullName()))
127 }
128 }
129 }
130
131 return nerr.E
132}
133
134// unmarshalSingular unmarshals given text.Value into the non-repeated field.
135func (o UnmarshalOptions) unmarshalSingular(input text.Value, fd pref.FieldDescriptor, knownFields pref.KnownFields) error {
136 num := fd.Number()
137
138 var nerr errors.NonFatal
139 var val pref.Value
140 switch fd.Kind() {
141 case pref.MessageKind, pref.GroupKind:
142 if input.Type() != text.Message {
143 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
144 }
145 m := knownFields.NewMessage(num).ProtoReflect()
146 if err := o.unmarshalMessage(input.Message(), m); !nerr.Merge(err) {
147 return err
148 }
149 val = pref.ValueOf(m)
150 default:
151 var err error
152 val, err = unmarshalScalar(input, fd)
153 if !nerr.Merge(err) {
154 return err
155 }
156 }
157 knownFields.Set(num, val)
158
159 return nerr.E
160}
161
162// unmarshalRepeated unmarshals given text.Value into a repeated field. Caller should only
163// call this for cardinality=repeated.
164func (o UnmarshalOptions) unmarshalRepeated(input text.Value, fd pref.FieldDescriptor, knownFields pref.KnownFields) error {
165 var items []text.Value
166 // If input is not a list, turn it into a list.
167 if input.Type() != text.List {
168 items = []text.Value{input}
169 } else {
170 items = input.List()
171 }
172
173 var nerr errors.NonFatal
174 num := fd.Number()
175 val := knownFields.Get(num)
176 if !fd.IsMap() {
177 if err := o.unmarshalList(items, fd, val.List()); !nerr.Merge(err) {
178 return err
179 }
180 } else {
181 if err := o.unmarshalMap(items, fd, val.Map()); !nerr.Merge(err) {
182 return err
183 }
184 }
185
186 return nerr.E
187}
188
189// unmarshalScalar converts the given text.Value to a scalar/enum protoreflect.Value specified in
190// the given FieldDescriptor. Caller should not pass in a FieldDescriptor for a message/group kind.
191func unmarshalScalar(input text.Value, fd pref.FieldDescriptor) (pref.Value, error) {
192 const b32 = false
193 const b64 = true
194
195 switch kind := fd.Kind(); kind {
196 case pref.BoolKind:
197 if b, ok := input.Bool(); ok {
198 return pref.ValueOf(bool(b)), nil
199 }
200 case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
201 if n, ok := input.Int(b32); ok {
202 return pref.ValueOf(int32(n)), nil
203 }
204 case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
205 if n, ok := input.Int(b64); ok {
206 return pref.ValueOf(int64(n)), nil
207 }
208 case pref.Uint32Kind, pref.Fixed32Kind:
209 if n, ok := input.Uint(b32); ok {
210 return pref.ValueOf(uint32(n)), nil
211 }
212 case pref.Uint64Kind, pref.Fixed64Kind:
213 if n, ok := input.Uint(b64); ok {
214 return pref.ValueOf(uint64(n)), nil
215 }
216 case pref.FloatKind:
217 if n, ok := input.Float(b32); ok {
218 return pref.ValueOf(float32(n)), nil
219 }
220 case pref.DoubleKind:
221 if n, ok := input.Float(b64); ok {
222 return pref.ValueOf(float64(n)), nil
223 }
224 case pref.StringKind:
225 if input.Type() == text.String {
226 return pref.ValueOf(string(input.String())), nil
227 }
228 case pref.BytesKind:
229 if input.Type() == text.String {
230 return pref.ValueOf([]byte(input.String())), nil
231 }
232 case pref.EnumKind:
233 // If input is int32, use directly.
234 if n, ok := input.Int(b32); ok {
235 return pref.ValueOf(pref.EnumNumber(n)), nil
236 } else {
237 if name, ok := input.Name(); ok {
238 // Lookup EnumNumber based on name.
239 if enumVal := fd.EnumType().Values().ByName(name); enumVal != nil {
240 return pref.ValueOf(enumVal.Number()), nil
241 }
242 }
243 }
244 default:
245 panic(fmt.Sprintf("invalid scalar kind %v", kind))
246 }
247
248 return pref.Value{}, errors.New("%v contains invalid scalar value: %v", fd.FullName(), input)
249}
250
251// unmarshalList unmarshals given []text.Value into given protoreflect.List.
252func (o UnmarshalOptions) unmarshalList(inputList []text.Value, fd pref.FieldDescriptor, list pref.List) error {
253 var nerr errors.NonFatal
254
255 switch fd.Kind() {
256 case pref.MessageKind, pref.GroupKind:
257 for _, input := range inputList {
258 if input.Type() != text.Message {
259 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
260 }
261 m := list.NewMessage().ProtoReflect()
262 if err := o.unmarshalMessage(input.Message(), m); !nerr.Merge(err) {
263 return err
264 }
265 list.Append(pref.ValueOf(m))
266 }
267 default:
268 for _, input := range inputList {
269 val, err := unmarshalScalar(input, fd)
270 if !nerr.Merge(err) {
271 return err
272 }
273 list.Append(val)
274 }
275 }
276
277 return nerr.E
278}
279
280// unmarshalMap unmarshals given []text.Value into given protoreflect.Map.
281func (o UnmarshalOptions) unmarshalMap(input []text.Value, fd pref.FieldDescriptor, mmap pref.Map) error {
282 var nerr errors.NonFatal
283 fields := fd.MessageType().Fields()
284 keyDesc := fields.ByNumber(1)
285 valDesc := fields.ByNumber(2)
286
287 // Determine ahead whether map entry is a scalar type or a message type in order to call the
288 // appropriate unmarshalMapValue func inside the for loop below.
289 unmarshalMapValue := o.unmarshalMapScalarValue
290 switch valDesc.Kind() {
291 case pref.MessageKind, pref.GroupKind:
292 unmarshalMapValue = o.unmarshalMapMessageValue
293 }
294
295 for _, entry := range input {
296 if entry.Type() != text.Message {
297 return errors.New("%v contains invalid map entry: %v", fd.FullName(), entry)
298 }
299 tkey, tval, err := parseMapEntry(entry.Message(), fd.FullName())
300 if !nerr.Merge(err) {
301 return err
302 }
303 pkey, err := unmarshalMapKey(tkey, keyDesc)
304 if !nerr.Merge(err) {
305 return err
306 }
307 err = unmarshalMapValue(tval, pkey, valDesc, mmap)
308 if !nerr.Merge(err) {
309 return err
310 }
311 }
312
313 return nerr.E
314}
315
316// parseMapEntry parses [][2]text.Value for field names key and value, and return corresponding
317// field values. If there are duplicate field names, the value for the last field is returned. If
318// the field name does not exist, it will return the zero value of text.Value. It will return an
319// error if there are unknown field names.
320func parseMapEntry(mapEntry [][2]text.Value, name pref.FullName) (key text.Value, value text.Value, err error) {
321 for _, field := range mapEntry {
322 keyStr, ok := field[0].Name()
323 if ok {
324 switch keyStr {
325 case "key":
326 if key.Type() != 0 {
327 return key, value, errors.New("%v contains duplicate key field", name)
328 }
329 key = field[1]
330 case "value":
331 if value.Type() != 0 {
332 return key, value, errors.New("%v contains duplicate value field", name)
333 }
334 value = field[1]
335 default:
336 ok = false
337 }
338 }
339 if !ok {
340 // TODO: Do not return error if ignore unknown option is added and enabled.
341 return key, value, errors.New("%v contains unknown map entry name: %v", name, field[0])
342 }
343 }
344 return key, value, nil
345}
346
347// unmarshalMapKey converts given text.Value into a protoreflect.MapKey. A map key type is any
348// integral or string type.
349func unmarshalMapKey(input text.Value, fd pref.FieldDescriptor) (pref.MapKey, error) {
350 // If input is not set, use the zero value.
351 if input.Type() == 0 {
352 return fd.Default().MapKey(), nil
353 }
354
355 val, err := unmarshalScalar(input, fd)
356 if err != nil {
357 return pref.MapKey{}, errors.New("%v contains invalid key: %v", fd.FullName(), input)
358 }
359 return val.MapKey(), nil
360}
361
362// unmarshalMapMessageValue unmarshals given message-type text.Value into a protoreflect.Map for
363// the given MapKey.
364func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, _ pref.FieldDescriptor, mmap pref.Map) error {
365 var nerr errors.NonFatal
366 var value [][2]text.Value
367 if input.Type() != 0 {
368 value = input.Message()
369 }
370 m := mmap.NewMessage().ProtoReflect()
371 if err := o.unmarshalMessage(value, m); !nerr.Merge(err) {
372 return err
373 }
374 mmap.Set(pkey, pref.ValueOf(m))
375 return nerr.E
376}
377
378// unmarshalMapScalarValue unmarshals given scalar-type text.Value into a protoreflect.Map
379// for the given MapKey.
380func (o UnmarshalOptions) unmarshalMapScalarValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error {
381 var val pref.Value
382 if input.Type() == 0 {
383 val = fd.Default()
384 } else {
385 var err error
386 val, err = unmarshalScalar(input, fd)
387 if err != nil {
388 return err
389 }
390 }
391 mmap.Set(pkey, val)
392 return nil
393}