initial commit
[govpp.git] / vendor / gopkg.in / yaml.v2 / decode.go
1 package yaml
2
3 import (
4         "encoding"
5         "encoding/base64"
6         "fmt"
7         "math"
8         "reflect"
9         "strconv"
10         "time"
11 )
12
13 const (
14         documentNode = 1 << iota
15         mappingNode
16         sequenceNode
17         scalarNode
18         aliasNode
19 )
20
21 type node struct {
22         kind         int
23         line, column int
24         tag          string
25         value        string
26         implicit     bool
27         children     []*node
28         anchors      map[string]*node
29 }
30
31 // ----------------------------------------------------------------------------
32 // Parser, produces a node tree out of a libyaml event stream.
33
34 type parser struct {
35         parser yaml_parser_t
36         event  yaml_event_t
37         doc    *node
38 }
39
40 func newParser(b []byte) *parser {
41         p := parser{}
42         if !yaml_parser_initialize(&p.parser) {
43                 panic("failed to initialize YAML emitter")
44         }
45
46         if len(b) == 0 {
47                 b = []byte{'\n'}
48         }
49
50         yaml_parser_set_input_string(&p.parser, b)
51
52         p.skip()
53         if p.event.typ != yaml_STREAM_START_EVENT {
54                 panic("expected stream start event, got " + strconv.Itoa(int(p.event.typ)))
55         }
56         p.skip()
57         return &p
58 }
59
60 func (p *parser) destroy() {
61         if p.event.typ != yaml_NO_EVENT {
62                 yaml_event_delete(&p.event)
63         }
64         yaml_parser_delete(&p.parser)
65 }
66
67 func (p *parser) skip() {
68         if p.event.typ != yaml_NO_EVENT {
69                 if p.event.typ == yaml_STREAM_END_EVENT {
70                         failf("attempted to go past the end of stream; corrupted value?")
71                 }
72                 yaml_event_delete(&p.event)
73         }
74         if !yaml_parser_parse(&p.parser, &p.event) {
75                 p.fail()
76         }
77 }
78
79 func (p *parser) fail() {
80         var where string
81         var line int
82         if p.parser.problem_mark.line != 0 {
83                 line = p.parser.problem_mark.line
84         } else if p.parser.context_mark.line != 0 {
85                 line = p.parser.context_mark.line
86         }
87         if line != 0 {
88                 where = "line " + strconv.Itoa(line) + ": "
89         }
90         var msg string
91         if len(p.parser.problem) > 0 {
92                 msg = p.parser.problem
93         } else {
94                 msg = "unknown problem parsing YAML content"
95         }
96         failf("%s%s", where, msg)
97 }
98
99 func (p *parser) anchor(n *node, anchor []byte) {
100         if anchor != nil {
101                 p.doc.anchors[string(anchor)] = n
102         }
103 }
104
105 func (p *parser) parse() *node {
106         switch p.event.typ {
107         case yaml_SCALAR_EVENT:
108                 return p.scalar()
109         case yaml_ALIAS_EVENT:
110                 return p.alias()
111         case yaml_MAPPING_START_EVENT:
112                 return p.mapping()
113         case yaml_SEQUENCE_START_EVENT:
114                 return p.sequence()
115         case yaml_DOCUMENT_START_EVENT:
116                 return p.document()
117         case yaml_STREAM_END_EVENT:
118                 // Happens when attempting to decode an empty buffer.
119                 return nil
120         default:
121                 panic("attempted to parse unknown event: " + strconv.Itoa(int(p.event.typ)))
122         }
123 }
124
125 func (p *parser) node(kind int) *node {
126         return &node{
127                 kind:   kind,
128                 line:   p.event.start_mark.line,
129                 column: p.event.start_mark.column,
130         }
131 }
132
133 func (p *parser) document() *node {
134         n := p.node(documentNode)
135         n.anchors = make(map[string]*node)
136         p.doc = n
137         p.skip()
138         n.children = append(n.children, p.parse())
139         if p.event.typ != yaml_DOCUMENT_END_EVENT {
140                 panic("expected end of document event but got " + strconv.Itoa(int(p.event.typ)))
141         }
142         p.skip()
143         return n
144 }
145
146 func (p *parser) alias() *node {
147         n := p.node(aliasNode)
148         n.value = string(p.event.anchor)
149         p.skip()
150         return n
151 }
152
153 func (p *parser) scalar() *node {
154         n := p.node(scalarNode)
155         n.value = string(p.event.value)
156         n.tag = string(p.event.tag)
157         n.implicit = p.event.implicit
158         p.anchor(n, p.event.anchor)
159         p.skip()
160         return n
161 }
162
163 func (p *parser) sequence() *node {
164         n := p.node(sequenceNode)
165         p.anchor(n, p.event.anchor)
166         p.skip()
167         for p.event.typ != yaml_SEQUENCE_END_EVENT {
168                 n.children = append(n.children, p.parse())
169         }
170         p.skip()
171         return n
172 }
173
174 func (p *parser) mapping() *node {
175         n := p.node(mappingNode)
176         p.anchor(n, p.event.anchor)
177         p.skip()
178         for p.event.typ != yaml_MAPPING_END_EVENT {
179                 n.children = append(n.children, p.parse(), p.parse())
180         }
181         p.skip()
182         return n
183 }
184
185 // ----------------------------------------------------------------------------
186 // Decoder, unmarshals a node into a provided value.
187
188 type decoder struct {
189         doc     *node
190         aliases map[string]bool
191         mapType reflect.Type
192         terrors []string
193 }
194
195 var (
196         mapItemType    = reflect.TypeOf(MapItem{})
197         durationType   = reflect.TypeOf(time.Duration(0))
198         defaultMapType = reflect.TypeOf(map[interface{}]interface{}{})
199         ifaceType      = defaultMapType.Elem()
200 )
201
202 func newDecoder() *decoder {
203         d := &decoder{mapType: defaultMapType}
204         d.aliases = make(map[string]bool)
205         return d
206 }
207
208 func (d *decoder) terror(n *node, tag string, out reflect.Value) {
209         if n.tag != "" {
210                 tag = n.tag
211         }
212         value := n.value
213         if tag != yaml_SEQ_TAG && tag != yaml_MAP_TAG {
214                 if len(value) > 10 {
215                         value = " `" + value[:7] + "...`"
216                 } else {
217                         value = " `" + value + "`"
218                 }
219         }
220         d.terrors = append(d.terrors, fmt.Sprintf("line %d: cannot unmarshal %s%s into %s", n.line+1, shortTag(tag), value, out.Type()))
221 }
222
223 func (d *decoder) callUnmarshaler(n *node, u Unmarshaler) (good bool) {
224         terrlen := len(d.terrors)
225         err := u.UnmarshalYAML(func(v interface{}) (err error) {
226                 defer handleErr(&err)
227                 d.unmarshal(n, reflect.ValueOf(v))
228                 if len(d.terrors) > terrlen {
229                         issues := d.terrors[terrlen:]
230                         d.terrors = d.terrors[:terrlen]
231                         return &TypeError{issues}
232                 }
233                 return nil
234         })
235         if e, ok := err.(*TypeError); ok {
236                 d.terrors = append(d.terrors, e.Errors...)
237                 return false
238         }
239         if err != nil {
240                 fail(err)
241         }
242         return true
243 }
244
245 // d.prepare initializes and dereferences pointers and calls UnmarshalYAML
246 // if a value is found to implement it.
247 // It returns the initialized and dereferenced out value, whether
248 // unmarshalling was already done by UnmarshalYAML, and if so whether
249 // its types unmarshalled appropriately.
250 //
251 // If n holds a null value, prepare returns before doing anything.
252 func (d *decoder) prepare(n *node, out reflect.Value) (newout reflect.Value, unmarshaled, good bool) {
253         if n.tag == yaml_NULL_TAG || n.kind == scalarNode && n.tag == "" && (n.value == "null" || n.value == "" && n.implicit) {
254                 return out, false, false
255         }
256         again := true
257         for again {
258                 again = false
259                 if out.Kind() == reflect.Ptr {
260                         if out.IsNil() {
261                                 out.Set(reflect.New(out.Type().Elem()))
262                         }
263                         out = out.Elem()
264                         again = true
265                 }
266                 if out.CanAddr() {
267                         if u, ok := out.Addr().Interface().(Unmarshaler); ok {
268                                 good = d.callUnmarshaler(n, u)
269                                 return out, true, good
270                         }
271                 }
272         }
273         return out, false, false
274 }
275
276 func (d *decoder) unmarshal(n *node, out reflect.Value) (good bool) {
277         switch n.kind {
278         case documentNode:
279                 return d.document(n, out)
280         case aliasNode:
281                 return d.alias(n, out)
282         }
283         out, unmarshaled, good := d.prepare(n, out)
284         if unmarshaled {
285                 return good
286         }
287         switch n.kind {
288         case scalarNode:
289                 good = d.scalar(n, out)
290         case mappingNode:
291                 good = d.mapping(n, out)
292         case sequenceNode:
293                 good = d.sequence(n, out)
294         default:
295                 panic("internal error: unknown node kind: " + strconv.Itoa(n.kind))
296         }
297         return good
298 }
299
300 func (d *decoder) document(n *node, out reflect.Value) (good bool) {
301         if len(n.children) == 1 {
302                 d.doc = n
303                 d.unmarshal(n.children[0], out)
304                 return true
305         }
306         return false
307 }
308
309 func (d *decoder) alias(n *node, out reflect.Value) (good bool) {
310         an, ok := d.doc.anchors[n.value]
311         if !ok {
312                 failf("unknown anchor '%s' referenced", n.value)
313         }
314         if d.aliases[n.value] {
315                 failf("anchor '%s' value contains itself", n.value)
316         }
317         d.aliases[n.value] = true
318         good = d.unmarshal(an, out)
319         delete(d.aliases, n.value)
320         return good
321 }
322
323 var zeroValue reflect.Value
324
325 func resetMap(out reflect.Value) {
326         for _, k := range out.MapKeys() {
327                 out.SetMapIndex(k, zeroValue)
328         }
329 }
330
331 func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
332         var tag string
333         var resolved interface{}
334         if n.tag == "" && !n.implicit {
335                 tag = yaml_STR_TAG
336                 resolved = n.value
337         } else {
338                 tag, resolved = resolve(n.tag, n.value)
339                 if tag == yaml_BINARY_TAG {
340                         data, err := base64.StdEncoding.DecodeString(resolved.(string))
341                         if err != nil {
342                                 failf("!!binary value contains invalid base64 data")
343                         }
344                         resolved = string(data)
345                 }
346         }
347         if resolved == nil {
348                 if out.Kind() == reflect.Map && !out.CanAddr() {
349                         resetMap(out)
350                 } else {
351                         out.Set(reflect.Zero(out.Type()))
352                 }
353                 return true
354         }
355         if s, ok := resolved.(string); ok && out.CanAddr() {
356                 if u, ok := out.Addr().Interface().(encoding.TextUnmarshaler); ok {
357                         err := u.UnmarshalText([]byte(s))
358                         if err != nil {
359                                 fail(err)
360                         }
361                         return true
362                 }
363         }
364         switch out.Kind() {
365         case reflect.String:
366                 if tag == yaml_BINARY_TAG {
367                         out.SetString(resolved.(string))
368                         good = true
369                 } else if resolved != nil {
370                         out.SetString(n.value)
371                         good = true
372                 }
373         case reflect.Interface:
374                 if resolved == nil {
375                         out.Set(reflect.Zero(out.Type()))
376                 } else {
377                         out.Set(reflect.ValueOf(resolved))
378                 }
379                 good = true
380         case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
381                 switch resolved := resolved.(type) {
382                 case int:
383                         if !out.OverflowInt(int64(resolved)) {
384                                 out.SetInt(int64(resolved))
385                                 good = true
386                         }
387                 case int64:
388                         if !out.OverflowInt(resolved) {
389                                 out.SetInt(resolved)
390                                 good = true
391                         }
392                 case uint64:
393                         if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
394                                 out.SetInt(int64(resolved))
395                                 good = true
396                         }
397                 case float64:
398                         if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
399                                 out.SetInt(int64(resolved))
400                                 good = true
401                         }
402                 case string:
403                         if out.Type() == durationType {
404                                 d, err := time.ParseDuration(resolved)
405                                 if err == nil {
406                                         out.SetInt(int64(d))
407                                         good = true
408                                 }
409                         }
410                 }
411         case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
412                 switch resolved := resolved.(type) {
413                 case int:
414                         if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
415                                 out.SetUint(uint64(resolved))
416                                 good = true
417                         }
418                 case int64:
419                         if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
420                                 out.SetUint(uint64(resolved))
421                                 good = true
422                         }
423                 case uint64:
424                         if !out.OverflowUint(uint64(resolved)) {
425                                 out.SetUint(uint64(resolved))
426                                 good = true
427                         }
428                 case float64:
429                         if resolved <= math.MaxUint64 && !out.OverflowUint(uint64(resolved)) {
430                                 out.SetUint(uint64(resolved))
431                                 good = true
432                         }
433                 }
434         case reflect.Bool:
435                 switch resolved := resolved.(type) {
436                 case bool:
437                         out.SetBool(resolved)
438                         good = true
439                 }
440         case reflect.Float32, reflect.Float64:
441                 switch resolved := resolved.(type) {
442                 case int:
443                         out.SetFloat(float64(resolved))
444                         good = true
445                 case int64:
446                         out.SetFloat(float64(resolved))
447                         good = true
448                 case uint64:
449                         out.SetFloat(float64(resolved))
450                         good = true
451                 case float64:
452                         out.SetFloat(resolved)
453                         good = true
454                 }
455         case reflect.Ptr:
456                 if out.Type().Elem() == reflect.TypeOf(resolved) {
457                         // TODO DOes this make sense? When is out a Ptr except when decoding a nil value?
458                         elem := reflect.New(out.Type().Elem())
459                         elem.Elem().Set(reflect.ValueOf(resolved))
460                         out.Set(elem)
461                         good = true
462                 }
463         }
464         if !good {
465                 d.terror(n, tag, out)
466         }
467         return good
468 }
469
470 func settableValueOf(i interface{}) reflect.Value {
471         v := reflect.ValueOf(i)
472         sv := reflect.New(v.Type()).Elem()
473         sv.Set(v)
474         return sv
475 }
476
477 func (d *decoder) sequence(n *node, out reflect.Value) (good bool) {
478         l := len(n.children)
479
480         var iface reflect.Value
481         switch out.Kind() {
482         case reflect.Slice:
483                 out.Set(reflect.MakeSlice(out.Type(), l, l))
484         case reflect.Interface:
485                 // No type hints. Will have to use a generic sequence.
486                 iface = out
487                 out = settableValueOf(make([]interface{}, l))
488         default:
489                 d.terror(n, yaml_SEQ_TAG, out)
490                 return false
491         }
492         et := out.Type().Elem()
493
494         j := 0
495         for i := 0; i < l; i++ {
496                 e := reflect.New(et).Elem()
497                 if ok := d.unmarshal(n.children[i], e); ok {
498                         out.Index(j).Set(e)
499                         j++
500                 }
501         }
502         out.Set(out.Slice(0, j))
503         if iface.IsValid() {
504                 iface.Set(out)
505         }
506         return true
507 }
508
509 func (d *decoder) mapping(n *node, out reflect.Value) (good bool) {
510         switch out.Kind() {
511         case reflect.Struct:
512                 return d.mappingStruct(n, out)
513         case reflect.Slice:
514                 return d.mappingSlice(n, out)
515         case reflect.Map:
516                 // okay
517         case reflect.Interface:
518                 if d.mapType.Kind() == reflect.Map {
519                         iface := out
520                         out = reflect.MakeMap(d.mapType)
521                         iface.Set(out)
522                 } else {
523                         slicev := reflect.New(d.mapType).Elem()
524                         if !d.mappingSlice(n, slicev) {
525                                 return false
526                         }
527                         out.Set(slicev)
528                         return true
529                 }
530         default:
531                 d.terror(n, yaml_MAP_TAG, out)
532                 return false
533         }
534         outt := out.Type()
535         kt := outt.Key()
536         et := outt.Elem()
537
538         mapType := d.mapType
539         if outt.Key() == ifaceType && outt.Elem() == ifaceType {
540                 d.mapType = outt
541         }
542
543         if out.IsNil() {
544                 out.Set(reflect.MakeMap(outt))
545         }
546         l := len(n.children)
547         for i := 0; i < l; i += 2 {
548                 if isMerge(n.children[i]) {
549                         d.merge(n.children[i+1], out)
550                         continue
551                 }
552                 k := reflect.New(kt).Elem()
553                 if d.unmarshal(n.children[i], k) {
554                         kkind := k.Kind()
555                         if kkind == reflect.Interface {
556                                 kkind = k.Elem().Kind()
557                         }
558                         if kkind == reflect.Map || kkind == reflect.Slice {
559                                 failf("invalid map key: %#v", k.Interface())
560                         }
561                         e := reflect.New(et).Elem()
562                         if d.unmarshal(n.children[i+1], e) {
563                                 out.SetMapIndex(k, e)
564                         }
565                 }
566         }
567         d.mapType = mapType
568         return true
569 }
570
571 func (d *decoder) mappingSlice(n *node, out reflect.Value) (good bool) {
572         outt := out.Type()
573         if outt.Elem() != mapItemType {
574                 d.terror(n, yaml_MAP_TAG, out)
575                 return false
576         }
577
578         mapType := d.mapType
579         d.mapType = outt
580
581         var slice []MapItem
582         var l = len(n.children)
583         for i := 0; i < l; i += 2 {
584                 if isMerge(n.children[i]) {
585                         d.merge(n.children[i+1], out)
586                         continue
587                 }
588                 item := MapItem{}
589                 k := reflect.ValueOf(&item.Key).Elem()
590                 if d.unmarshal(n.children[i], k) {
591                         v := reflect.ValueOf(&item.Value).Elem()
592                         if d.unmarshal(n.children[i+1], v) {
593                                 slice = append(slice, item)
594                         }
595                 }
596         }
597         out.Set(reflect.ValueOf(slice))
598         d.mapType = mapType
599         return true
600 }
601
602 func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) {
603         sinfo, err := getStructInfo(out.Type())
604         if err != nil {
605                 panic(err)
606         }
607         name := settableValueOf("")
608         l := len(n.children)
609
610         var inlineMap reflect.Value
611         var elemType reflect.Type
612         if sinfo.InlineMap != -1 {
613                 inlineMap = out.Field(sinfo.InlineMap)
614                 inlineMap.Set(reflect.New(inlineMap.Type()).Elem())
615                 elemType = inlineMap.Type().Elem()
616         }
617
618         for i := 0; i < l; i += 2 {
619                 ni := n.children[i]
620                 if isMerge(ni) {
621                         d.merge(n.children[i+1], out)
622                         continue
623                 }
624                 if !d.unmarshal(ni, name) {
625                         continue
626                 }
627                 if info, ok := sinfo.FieldsMap[name.String()]; ok {
628                         var field reflect.Value
629                         if info.Inline == nil {
630                                 field = out.Field(info.Num)
631                         } else {
632                                 field = out.FieldByIndex(info.Inline)
633                         }
634                         d.unmarshal(n.children[i+1], field)
635                 } else if sinfo.InlineMap != -1 {
636                         if inlineMap.IsNil() {
637                                 inlineMap.Set(reflect.MakeMap(inlineMap.Type()))
638                         }
639                         value := reflect.New(elemType).Elem()
640                         d.unmarshal(n.children[i+1], value)
641                         inlineMap.SetMapIndex(name, value)
642                 }
643         }
644         return true
645 }
646
647 func failWantMap() {
648         failf("map merge requires map or sequence of maps as the value")
649 }
650
651 func (d *decoder) merge(n *node, out reflect.Value) {
652         switch n.kind {
653         case mappingNode:
654                 d.unmarshal(n, out)
655         case aliasNode:
656                 an, ok := d.doc.anchors[n.value]
657                 if ok && an.kind != mappingNode {
658                         failWantMap()
659                 }
660                 d.unmarshal(n, out)
661         case sequenceNode:
662                 // Step backwards as earlier nodes take precedence.
663                 for i := len(n.children) - 1; i >= 0; i-- {
664                         ni := n.children[i]
665                         if ni.kind == aliasNode {
666                                 an, ok := d.doc.anchors[ni.value]
667                                 if ok && an.kind != mappingNode {
668                                         failWantMap()
669                                 }
670                         } else if ni.kind != mappingNode {
671                                 failWantMap()
672                         }
673                         d.unmarshal(ni, out)
674                 }
675         default:
676                 failWantMap()
677         }
678 }
679
680 func isMerge(n *node) bool {
681         return n.kind == scalarNode && n.value == "<<" && (n.implicit == true || n.tag == yaml_MERGE_TAG)
682 }