You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

775 lines
18 KiB

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