decode.go 17 KB

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