blob: 531087655559e3fba07998030deba3500b872ee1 [file] [log] [blame]
Scott Baker1fe72732019-10-21 10:58:51 -07001package yaml
2
3import (
4 "encoding"
5 "encoding/base64"
6 "fmt"
7 "io"
8 "math"
9 "reflect"
10 "strconv"
11 "time"
12)
13
14const (
15 documentNode = 1 << iota
16 mappingNode
17 sequenceNode
18 scalarNode
19 aliasNode
20)
21
22type node struct {
23 kind int
24 line, column int
25 tag string
26 // For an alias node, alias holds the resolved alias.
27 alias *node
28 value string
29 implicit bool
30 children []*node
31 anchors map[string]*node
32}
33
34// ----------------------------------------------------------------------------
35// Parser, produces a node tree out of a libyaml event stream.
36
37type parser struct {
38 parser yaml_parser_t
39 event yaml_event_t
40 doc *node
41 doneInit bool
42}
43
44func newParser(b []byte) *parser {
45 p := parser{}
46 if !yaml_parser_initialize(&p.parser) {
47 panic("failed to initialize YAML emitter")
48 }
49 if len(b) == 0 {
50 b = []byte{'\n'}
51 }
52 yaml_parser_set_input_string(&p.parser, b)
53 return &p
54}
55
56func newParserFromReader(r io.Reader) *parser {
57 p := parser{}
58 if !yaml_parser_initialize(&p.parser) {
59 panic("failed to initialize YAML emitter")
60 }
61 yaml_parser_set_input_reader(&p.parser, r)
62 return &p
63}
64
65func (p *parser) init() {
66 if p.doneInit {
67 return
68 }
69 p.expect(yaml_STREAM_START_EVENT)
70 p.doneInit = true
71}
72
73func (p *parser) destroy() {
74 if p.event.typ != yaml_NO_EVENT {
75 yaml_event_delete(&p.event)
76 }
77 yaml_parser_delete(&p.parser)
78}
79
80// expect consumes an event from the event stream and
81// checks that it's of the expected type.
82func (p *parser) expect(e yaml_event_type_t) {
83 if p.event.typ == yaml_NO_EVENT {
84 if !yaml_parser_parse(&p.parser, &p.event) {
85 p.fail()
86 }
87 }
88 if p.event.typ == yaml_STREAM_END_EVENT {
89 failf("attempted to go past the end of stream; corrupted value?")
90 }
91 if p.event.typ != e {
92 p.parser.problem = fmt.Sprintf("expected %s event but got %s", e, p.event.typ)
93 p.fail()
94 }
95 yaml_event_delete(&p.event)
96 p.event.typ = yaml_NO_EVENT
97}
98
99// peek peeks at the next event in the event stream,
100// puts the results into p.event and returns the event type.
101func (p *parser) peek() yaml_event_type_t {
102 if p.event.typ != yaml_NO_EVENT {
103 return p.event.typ
104 }
105 if !yaml_parser_parse(&p.parser, &p.event) {
106 p.fail()
107 }
108 return p.event.typ
109}
110
111func (p *parser) fail() {
112 var where string
113 var line int
114 if p.parser.problem_mark.line != 0 {
115 line = p.parser.problem_mark.line
116 // Scanner errors don't iterate line before returning error
117 if p.parser.error == yaml_SCANNER_ERROR {
118 line++
119 }
120 } else if p.parser.context_mark.line != 0 {
121 line = p.parser.context_mark.line
122 }
123 if line != 0 {
124 where = "line " + strconv.Itoa(line) + ": "
125 }
126 var msg string
127 if len(p.parser.problem) > 0 {
128 msg = p.parser.problem
129 } else {
130 msg = "unknown problem parsing YAML content"
131 }
132 failf("%s%s", where, msg)
133}
134
135func (p *parser) anchor(n *node, anchor []byte) {
136 if anchor != nil {
137 p.doc.anchors[string(anchor)] = n
138 }
139}
140
141func (p *parser) parse() *node {
142 p.init()
143 switch p.peek() {
144 case yaml_SCALAR_EVENT:
145 return p.scalar()
146 case yaml_ALIAS_EVENT:
147 return p.alias()
148 case yaml_MAPPING_START_EVENT:
149 return p.mapping()
150 case yaml_SEQUENCE_START_EVENT:
151 return p.sequence()
152 case yaml_DOCUMENT_START_EVENT:
153 return p.document()
154 case yaml_STREAM_END_EVENT:
155 // Happens when attempting to decode an empty buffer.
156 return nil
157 default:
158 panic("attempted to parse unknown event: " + p.event.typ.String())
159 }
160}
161
162func (p *parser) node(kind int) *node {
163 return &node{
164 kind: kind,
165 line: p.event.start_mark.line,
166 column: p.event.start_mark.column,
167 }
168}
169
170func (p *parser) document() *node {
171 n := p.node(documentNode)
172 n.anchors = make(map[string]*node)
173 p.doc = n
174 p.expect(yaml_DOCUMENT_START_EVENT)
175 n.children = append(n.children, p.parse())
176 p.expect(yaml_DOCUMENT_END_EVENT)
177 return n
178}
179
180func (p *parser) alias() *node {
181 n := p.node(aliasNode)
182 n.value = string(p.event.anchor)
183 n.alias = p.doc.anchors[n.value]
184 if n.alias == nil {
185 failf("unknown anchor '%s' referenced", n.value)
186 }
187 p.expect(yaml_ALIAS_EVENT)
188 return n
189}
190
191func (p *parser) scalar() *node {
192 n := p.node(scalarNode)
193 n.value = string(p.event.value)
194 n.tag = string(p.event.tag)
195 n.implicit = p.event.implicit
196 p.anchor(n, p.event.anchor)
197 p.expect(yaml_SCALAR_EVENT)
198 return n
199}
200
201func (p *parser) sequence() *node {
202 n := p.node(sequenceNode)
203 p.anchor(n, p.event.anchor)
204 p.expect(yaml_SEQUENCE_START_EVENT)
205 for p.peek() != yaml_SEQUENCE_END_EVENT {
206 n.children = append(n.children, p.parse())
207 }
208 p.expect(yaml_SEQUENCE_END_EVENT)
209 return n
210}
211
212func (p *parser) mapping() *node {
213 n := p.node(mappingNode)
214 p.anchor(n, p.event.anchor)
215 p.expect(yaml_MAPPING_START_EVENT)
216 for p.peek() != yaml_MAPPING_END_EVENT {
217 n.children = append(n.children, p.parse(), p.parse())
218 }
219 p.expect(yaml_MAPPING_END_EVENT)
220 return n
221}
222
223// ----------------------------------------------------------------------------
224// Decoder, unmarshals a node into a provided value.
225
226type decoder struct {
227 doc *node
228 aliases map[*node]bool
229 mapType reflect.Type
230 terrors []string
231 strict bool
232
233 decodeCount int
234 aliasCount int
235 aliasDepth int
236}
237
238var (
239 mapItemType = reflect.TypeOf(MapItem{})
240 durationType = reflect.TypeOf(time.Duration(0))
241 defaultMapType = reflect.TypeOf(map[interface{}]interface{}{})
242 ifaceType = defaultMapType.Elem()
243 timeType = reflect.TypeOf(time.Time{})
244 ptrTimeType = reflect.TypeOf(&time.Time{})
245)
246
247func newDecoder(strict bool) *decoder {
248 d := &decoder{mapType: defaultMapType, strict: strict}
249 d.aliases = make(map[*node]bool)
250 return d
251}
252
253func (d *decoder) terror(n *node, tag string, out reflect.Value) {
254 if n.tag != "" {
255 tag = n.tag
256 }
257 value := n.value
258 if tag != yaml_SEQ_TAG && tag != yaml_MAP_TAG {
259 if len(value) > 10 {
260 value = " `" + value[:7] + "...`"
261 } else {
262 value = " `" + value + "`"
263 }
264 }
265 d.terrors = append(d.terrors, fmt.Sprintf("line %d: cannot unmarshal %s%s into %s", n.line+1, shortTag(tag), value, out.Type()))
266}
267
268func (d *decoder) callUnmarshaler(n *node, u Unmarshaler) (good bool) {
269 terrlen := len(d.terrors)
270 err := u.UnmarshalYAML(func(v interface{}) (err error) {
271 defer handleErr(&err)
272 d.unmarshal(n, reflect.ValueOf(v))
273 if len(d.terrors) > terrlen {
274 issues := d.terrors[terrlen:]
275 d.terrors = d.terrors[:terrlen]
276 return &TypeError{issues}
277 }
278 return nil
279 })
280 if e, ok := err.(*TypeError); ok {
281 d.terrors = append(d.terrors, e.Errors...)
282 return false
283 }
284 if err != nil {
285 fail(err)
286 }
287 return true
288}
289
290// d.prepare initializes and dereferences pointers and calls UnmarshalYAML
291// if a value is found to implement it.
292// It returns the initialized and dereferenced out value, whether
293// unmarshalling was already done by UnmarshalYAML, and if so whether
294// its types unmarshalled appropriately.
295//
296// If n holds a null value, prepare returns before doing anything.
297func (d *decoder) prepare(n *node, out reflect.Value) (newout reflect.Value, unmarshaled, good bool) {
298 if n.tag == yaml_NULL_TAG || n.kind == scalarNode && n.tag == "" && (n.value == "null" || n.value == "~" || n.value == "" && n.implicit) {
299 return out, false, false
300 }
301 again := true
302 for again {
303 again = false
304 if out.Kind() == reflect.Ptr {
305 if out.IsNil() {
306 out.Set(reflect.New(out.Type().Elem()))
307 }
308 out = out.Elem()
309 again = true
310 }
311 if out.CanAddr() {
312 if u, ok := out.Addr().Interface().(Unmarshaler); ok {
313 good = d.callUnmarshaler(n, u)
314 return out, true, good
315 }
316 }
317 }
318 return out, false, false
319}
320
321const (
322 // 400,000 decode operations is ~500kb of dense object declarations, or ~5kb of dense object declarations with 10000% alias expansion
323 alias_ratio_range_low = 400000
324 // 4,000,000 decode operations is ~5MB of dense object declarations, or ~4.5MB of dense object declarations with 10% alias expansion
325 alias_ratio_range_high = 4000000
326 // alias_ratio_range is the range over which we scale allowed alias ratios
327 alias_ratio_range = float64(alias_ratio_range_high - alias_ratio_range_low)
328)
329
330func allowedAliasRatio(decodeCount int) float64 {
331 switch {
332 case decodeCount <= alias_ratio_range_low:
333 // allow 99% to come from alias expansion for small-to-medium documents
334 return 0.99
335 case decodeCount >= alias_ratio_range_high:
336 // allow 10% to come from alias expansion for very large documents
337 return 0.10
338 default:
339 // scale smoothly from 99% down to 10% over the range.
340 // this maps to 396,000 - 400,000 allowed alias-driven decodes over the range.
341 // 400,000 decode operations is ~100MB of allocations in worst-case scenarios (single-item maps).
342 return 0.99 - 0.89*(float64(decodeCount-alias_ratio_range_low)/alias_ratio_range)
343 }
344}
345
346func (d *decoder) unmarshal(n *node, out reflect.Value) (good bool) {
347 d.decodeCount++
348 if d.aliasDepth > 0 {
349 d.aliasCount++
350 }
351 if d.aliasCount > 100 && d.decodeCount > 1000 && float64(d.aliasCount)/float64(d.decodeCount) > allowedAliasRatio(d.decodeCount) {
352 failf("document contains excessive aliasing")
353 }
354 switch n.kind {
355 case documentNode:
356 return d.document(n, out)
357 case aliasNode:
358 return d.alias(n, out)
359 }
360 out, unmarshaled, good := d.prepare(n, out)
361 if unmarshaled {
362 return good
363 }
364 switch n.kind {
365 case scalarNode:
366 good = d.scalar(n, out)
367 case mappingNode:
368 good = d.mapping(n, out)
369 case sequenceNode:
370 good = d.sequence(n, out)
371 default:
372 panic("internal error: unknown node kind: " + strconv.Itoa(n.kind))
373 }
374 return good
375}
376
377func (d *decoder) document(n *node, out reflect.Value) (good bool) {
378 if len(n.children) == 1 {
379 d.doc = n
380 d.unmarshal(n.children[0], out)
381 return true
382 }
383 return false
384}
385
386func (d *decoder) alias(n *node, out reflect.Value) (good bool) {
387 if d.aliases[n] {
388 // TODO this could actually be allowed in some circumstances.
389 failf("anchor '%s' value contains itself", n.value)
390 }
391 d.aliases[n] = true
392 d.aliasDepth++
393 good = d.unmarshal(n.alias, out)
394 d.aliasDepth--
395 delete(d.aliases, n)
396 return good
397}
398
399var zeroValue reflect.Value
400
401func resetMap(out reflect.Value) {
402 for _, k := range out.MapKeys() {
403 out.SetMapIndex(k, zeroValue)
404 }
405}
406
407func (d *decoder) scalar(n *node, out reflect.Value) bool {
408 var tag string
409 var resolved interface{}
410 if n.tag == "" && !n.implicit {
411 tag = yaml_STR_TAG
412 resolved = n.value
413 } else {
414 tag, resolved = resolve(n.tag, n.value)
415 if tag == yaml_BINARY_TAG {
416 data, err := base64.StdEncoding.DecodeString(resolved.(string))
417 if err != nil {
418 failf("!!binary value contains invalid base64 data")
419 }
420 resolved = string(data)
421 }
422 }
423 if resolved == nil {
424 if out.Kind() == reflect.Map && !out.CanAddr() {
425 resetMap(out)
426 } else {
427 out.Set(reflect.Zero(out.Type()))
428 }
429 return true
430 }
431 if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
432 // We've resolved to exactly the type we want, so use that.
433 out.Set(resolvedv)
434 return true
435 }
436 // Perhaps we can use the value as a TextUnmarshaler to
437 // set its value.
438 if out.CanAddr() {
439 u, ok := out.Addr().Interface().(encoding.TextUnmarshaler)
440 if ok {
441 var text []byte
442 if tag == yaml_BINARY_TAG {
443 text = []byte(resolved.(string))
444 } else {
445 // We let any value be unmarshaled into TextUnmarshaler.
446 // That might be more lax than we'd like, but the
447 // TextUnmarshaler itself should bowl out any dubious values.
448 text = []byte(n.value)
449 }
450 err := u.UnmarshalText(text)
451 if err != nil {
452 fail(err)
453 }
454 return true
455 }
456 }
457 switch out.Kind() {
458 case reflect.String:
459 if tag == yaml_BINARY_TAG {
460 out.SetString(resolved.(string))
461 return true
462 }
463 if resolved != nil {
464 out.SetString(n.value)
465 return true
466 }
467 case reflect.Interface:
468 if resolved == nil {
469 out.Set(reflect.Zero(out.Type()))
470 } else if tag == yaml_TIMESTAMP_TAG {
471 // It looks like a timestamp but for backward compatibility
472 // reasons we set it as a string, so that code that unmarshals
473 // timestamp-like values into interface{} will continue to
474 // see a string and not a time.Time.
475 // TODO(v3) Drop this.
476 out.Set(reflect.ValueOf(n.value))
477 } else {
478 out.Set(reflect.ValueOf(resolved))
479 }
480 return true
481 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
482 switch resolved := resolved.(type) {
483 case int:
484 if !out.OverflowInt(int64(resolved)) {
485 out.SetInt(int64(resolved))
486 return true
487 }
488 case int64:
489 if !out.OverflowInt(resolved) {
490 out.SetInt(resolved)
491 return true
492 }
493 case uint64:
494 if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
495 out.SetInt(int64(resolved))
496 return true
497 }
498 case float64:
499 if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
500 out.SetInt(int64(resolved))
501 return true
502 }
503 case string:
504 if out.Type() == durationType {
505 d, err := time.ParseDuration(resolved)
506 if err == nil {
507 out.SetInt(int64(d))
508 return true
509 }
510 }
511 }
512 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
513 switch resolved := resolved.(type) {
514 case int:
515 if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
516 out.SetUint(uint64(resolved))
517 return true
518 }
519 case int64:
520 if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
521 out.SetUint(uint64(resolved))
522 return true
523 }
524 case uint64:
525 if !out.OverflowUint(uint64(resolved)) {
526 out.SetUint(uint64(resolved))
527 return true
528 }
529 case float64:
530 if resolved <= math.MaxUint64 && !out.OverflowUint(uint64(resolved)) {
531 out.SetUint(uint64(resolved))
532 return true
533 }
534 }
535 case reflect.Bool:
536 switch resolved := resolved.(type) {
537 case bool:
538 out.SetBool(resolved)
539 return true
540 }
541 case reflect.Float32, reflect.Float64:
542 switch resolved := resolved.(type) {
543 case int:
544 out.SetFloat(float64(resolved))
545 return true
546 case int64:
547 out.SetFloat(float64(resolved))
548 return true
549 case uint64:
550 out.SetFloat(float64(resolved))
551 return true
552 case float64:
553 out.SetFloat(resolved)
554 return true
555 }
556 case reflect.Struct:
557 if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
558 out.Set(resolvedv)
559 return true
560 }
561 case reflect.Ptr:
562 if out.Type().Elem() == reflect.TypeOf(resolved) {
563 // TODO DOes this make sense? When is out a Ptr except when decoding a nil value?
564 elem := reflect.New(out.Type().Elem())
565 elem.Elem().Set(reflect.ValueOf(resolved))
566 out.Set(elem)
567 return true
568 }
569 }
570 d.terror(n, tag, out)
571 return false
572}
573
574func settableValueOf(i interface{}) reflect.Value {
575 v := reflect.ValueOf(i)
576 sv := reflect.New(v.Type()).Elem()
577 sv.Set(v)
578 return sv
579}
580
581func (d *decoder) sequence(n *node, out reflect.Value) (good bool) {
582 l := len(n.children)
583
584 var iface reflect.Value
585 switch out.Kind() {
586 case reflect.Slice:
587 out.Set(reflect.MakeSlice(out.Type(), l, l))
588 case reflect.Array:
589 if l != out.Len() {
590 failf("invalid array: want %d elements but got %d", out.Len(), l)
591 }
592 case reflect.Interface:
593 // No type hints. Will have to use a generic sequence.
594 iface = out
595 out = settableValueOf(make([]interface{}, l))
596 default:
597 d.terror(n, yaml_SEQ_TAG, out)
598 return false
599 }
600 et := out.Type().Elem()
601
602 j := 0
603 for i := 0; i < l; i++ {
604 e := reflect.New(et).Elem()
605 if ok := d.unmarshal(n.children[i], e); ok {
606 out.Index(j).Set(e)
607 j++
608 }
609 }
610 if out.Kind() != reflect.Array {
611 out.Set(out.Slice(0, j))
612 }
613 if iface.IsValid() {
614 iface.Set(out)
615 }
616 return true
617}
618
619func (d *decoder) mapping(n *node, out reflect.Value) (good bool) {
620 switch out.Kind() {
621 case reflect.Struct:
622 return d.mappingStruct(n, out)
623 case reflect.Slice:
624 return d.mappingSlice(n, out)
625 case reflect.Map:
626 // okay
627 case reflect.Interface:
628 if d.mapType.Kind() == reflect.Map {
629 iface := out
630 out = reflect.MakeMap(d.mapType)
631 iface.Set(out)
632 } else {
633 slicev := reflect.New(d.mapType).Elem()
634 if !d.mappingSlice(n, slicev) {
635 return false
636 }
637 out.Set(slicev)
638 return true
639 }
640 default:
641 d.terror(n, yaml_MAP_TAG, out)
642 return false
643 }
644 outt := out.Type()
645 kt := outt.Key()
646 et := outt.Elem()
647
648 mapType := d.mapType
649 if outt.Key() == ifaceType && outt.Elem() == ifaceType {
650 d.mapType = outt
651 }
652
653 if out.IsNil() {
654 out.Set(reflect.MakeMap(outt))
655 }
656 l := len(n.children)
657 for i := 0; i < l; i += 2 {
658 if isMerge(n.children[i]) {
659 d.merge(n.children[i+1], out)
660 continue
661 }
662 k := reflect.New(kt).Elem()
663 if d.unmarshal(n.children[i], k) {
664 kkind := k.Kind()
665 if kkind == reflect.Interface {
666 kkind = k.Elem().Kind()
667 }
668 if kkind == reflect.Map || kkind == reflect.Slice {
669 failf("invalid map key: %#v", k.Interface())
670 }
671 e := reflect.New(et).Elem()
672 if d.unmarshal(n.children[i+1], e) {
673 d.setMapIndex(n.children[i+1], out, k, e)
674 }
675 }
676 }
677 d.mapType = mapType
678 return true
679}
680
681func (d *decoder) setMapIndex(n *node, out, k, v reflect.Value) {
682 if d.strict && out.MapIndex(k) != zeroValue {
683 d.terrors = append(d.terrors, fmt.Sprintf("line %d: key %#v already set in map", n.line+1, k.Interface()))
684 return
685 }
686 out.SetMapIndex(k, v)
687}
688
689func (d *decoder) mappingSlice(n *node, out reflect.Value) (good bool) {
690 outt := out.Type()
691 if outt.Elem() != mapItemType {
692 d.terror(n, yaml_MAP_TAG, out)
693 return false
694 }
695
696 mapType := d.mapType
697 d.mapType = outt
698
699 var slice []MapItem
700 var l = len(n.children)
701 for i := 0; i < l; i += 2 {
702 if isMerge(n.children[i]) {
703 d.merge(n.children[i+1], out)
704 continue
705 }
706 item := MapItem{}
707 k := reflect.ValueOf(&item.Key).Elem()
708 if d.unmarshal(n.children[i], k) {
709 v := reflect.ValueOf(&item.Value).Elem()
710 if d.unmarshal(n.children[i+1], v) {
711 slice = append(slice, item)
712 }
713 }
714 }
715 out.Set(reflect.ValueOf(slice))
716 d.mapType = mapType
717 return true
718}
719
720func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) {
721 sinfo, err := getStructInfo(out.Type())
722 if err != nil {
723 panic(err)
724 }
725 name := settableValueOf("")
726 l := len(n.children)
727
728 var inlineMap reflect.Value
729 var elemType reflect.Type
730 if sinfo.InlineMap != -1 {
731 inlineMap = out.Field(sinfo.InlineMap)
732 inlineMap.Set(reflect.New(inlineMap.Type()).Elem())
733 elemType = inlineMap.Type().Elem()
734 }
735
736 var doneFields []bool
737 if d.strict {
738 doneFields = make([]bool, len(sinfo.FieldsList))
739 }
740 for i := 0; i < l; i += 2 {
741 ni := n.children[i]
742 if isMerge(ni) {
743 d.merge(n.children[i+1], out)
744 continue
745 }
746 if !d.unmarshal(ni, name) {
747 continue
748 }
749 if info, ok := sinfo.FieldsMap[name.String()]; ok {
750 if d.strict {
751 if doneFields[info.Id] {
752 d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s already set in type %s", ni.line+1, name.String(), out.Type()))
753 continue
754 }
755 doneFields[info.Id] = true
756 }
757 var field reflect.Value
758 if info.Inline == nil {
759 field = out.Field(info.Num)
760 } else {
761 field = out.FieldByIndex(info.Inline)
762 }
763 d.unmarshal(n.children[i+1], field)
764 } else if sinfo.InlineMap != -1 {
765 if inlineMap.IsNil() {
766 inlineMap.Set(reflect.MakeMap(inlineMap.Type()))
767 }
768 value := reflect.New(elemType).Elem()
769 d.unmarshal(n.children[i+1], value)
770 d.setMapIndex(n.children[i+1], inlineMap, name, value)
771 } else if d.strict {
772 d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s not found in type %s", ni.line+1, name.String(), out.Type()))
773 }
774 }
775 return true
776}
777
778func failWantMap() {
779 failf("map merge requires map or sequence of maps as the value")
780}
781
782func (d *decoder) merge(n *node, out reflect.Value) {
783 switch n.kind {
784 case mappingNode:
785 d.unmarshal(n, out)
786 case aliasNode:
787 an, ok := d.doc.anchors[n.value]
788 if ok && an.kind != mappingNode {
789 failWantMap()
790 }
791 d.unmarshal(n, out)
792 case sequenceNode:
793 // Step backwards as earlier nodes take precedence.
794 for i := len(n.children) - 1; i >= 0; i-- {
795 ni := n.children[i]
796 if ni.kind == aliasNode {
797 an, ok := d.doc.anchors[ni.value]
798 if ok && an.kind != mappingNode {
799 failWantMap()
800 }
801 } else if ni.kind != mappingNode {
802 failWantMap()
803 }
804 d.unmarshal(ni, out)
805 }
806 default:
807 failWantMap()
808 }
809}
810
811func isMerge(n *node) bool {
812 return n.kind == scalarNode && n.value == "<<" && (n.implicit == true || n.tag == yaml_MERGE_TAG)
813}