decode.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. timeType = reflect.TypeOf(time.Time{})
  213. )
  214. func newDecoder(strict bool) *decoder {
  215. d := &decoder{mapType: defaultMapType, strict: strict}
  216. d.aliases = make(map[*node]bool)
  217. return d
  218. }
  219. func (d *decoder) terror(n *node, tag string, out reflect.Value) {
  220. if n.tag != "" {
  221. tag = n.tag
  222. }
  223. value := n.value
  224. if tag != yaml_SEQ_TAG && tag != yaml_MAP_TAG {
  225. if len(value) > 10 {
  226. value = " `" + value[:7] + "...`"
  227. } else {
  228. value = " `" + value + "`"
  229. }
  230. }
  231. d.terrors = append(d.terrors, fmt.Sprintf("line %d: cannot unmarshal %s%s into %s", n.line+1, shortTag(tag), value, out.Type()))
  232. }
  233. func (d *decoder) callUnmarshaler(n *node, u Unmarshaler) (good bool) {
  234. terrlen := len(d.terrors)
  235. err := u.UnmarshalYAML(func(v interface{}) (err error) {
  236. defer handleErr(&err)
  237. d.unmarshal(n, reflect.ValueOf(v))
  238. if len(d.terrors) > terrlen {
  239. issues := d.terrors[terrlen:]
  240. d.terrors = d.terrors[:terrlen]
  241. return &TypeError{issues}
  242. }
  243. return nil
  244. })
  245. if e, ok := err.(*TypeError); ok {
  246. d.terrors = append(d.terrors, e.Errors...)
  247. return false
  248. }
  249. if err != nil {
  250. fail(err)
  251. }
  252. return true
  253. }
  254. // d.prepare initializes and dereferences pointers and calls UnmarshalYAML
  255. // if a value is found to implement it.
  256. // It returns the initialized and dereferenced out value, whether
  257. // unmarshalling was already done by UnmarshalYAML, and if so whether
  258. // its types unmarshalled appropriately.
  259. //
  260. // If n holds a null value, prepare returns before doing anything.
  261. func (d *decoder) prepare(n *node, out reflect.Value) (newout reflect.Value, unmarshaled, good bool) {
  262. if n.tag == yaml_NULL_TAG || n.kind == scalarNode && n.tag == "" && (n.value == "null" || n.value == "~" || n.value == "" && n.implicit) {
  263. return out, false, false
  264. }
  265. again := true
  266. for again {
  267. again = false
  268. if out.Kind() == reflect.Ptr {
  269. if out.IsNil() {
  270. out.Set(reflect.New(out.Type().Elem()))
  271. }
  272. out = out.Elem()
  273. again = true
  274. }
  275. if out.CanAddr() {
  276. if u, ok := out.Addr().Interface().(Unmarshaler); ok {
  277. good = d.callUnmarshaler(n, u)
  278. return out, true, good
  279. }
  280. }
  281. }
  282. return out, false, false
  283. }
  284. func (d *decoder) unmarshal(n *node, out reflect.Value) (good bool) {
  285. switch n.kind {
  286. case documentNode:
  287. return d.document(n, out)
  288. case aliasNode:
  289. return d.alias(n, out)
  290. }
  291. out, unmarshaled, good := d.prepare(n, out)
  292. if unmarshaled {
  293. return good
  294. }
  295. switch n.kind {
  296. case scalarNode:
  297. good = d.scalar(n, out)
  298. case mappingNode:
  299. good = d.mapping(n, out)
  300. case sequenceNode:
  301. good = d.sequence(n, out)
  302. default:
  303. panic("internal error: unknown node kind: " + strconv.Itoa(n.kind))
  304. }
  305. return good
  306. }
  307. func (d *decoder) document(n *node, out reflect.Value) (good bool) {
  308. if len(n.children) == 1 {
  309. d.doc = n
  310. d.unmarshal(n.children[0], out)
  311. return true
  312. }
  313. return false
  314. }
  315. func (d *decoder) alias(n *node, out reflect.Value) (good bool) {
  316. if d.aliases[n] {
  317. // TODO this could actually be allowed in some circumstances.
  318. failf("anchor '%s' value contains itself", n.value)
  319. }
  320. d.aliases[n] = true
  321. good = d.unmarshal(n.alias, out)
  322. delete(d.aliases, n)
  323. return good
  324. }
  325. var zeroValue reflect.Value
  326. func resetMap(out reflect.Value) {
  327. for _, k := range out.MapKeys() {
  328. out.SetMapIndex(k, zeroValue)
  329. }
  330. }
  331. func (d *decoder) scalar(n *node, out reflect.Value) 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 resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
  356. // We've resolved to exactly the type we want, so use that.
  357. out.Set(resolvedv)
  358. return true
  359. }
  360. // Perhaps we can use the value as a TextUnmarshaler to
  361. // set its value.
  362. if out.CanAddr() {
  363. u, ok := out.Addr().Interface().(encoding.TextUnmarshaler)
  364. if ok {
  365. var text []byte
  366. if tag == yaml_BINARY_TAG {
  367. text = []byte(resolved.(string))
  368. } else {
  369. // We let any value be unmarshaled into TextUnmarshaler.
  370. // That might be more lax than we'd like, but the
  371. // TextUnmarshaler itself should bowl out any dubious values.
  372. text = []byte(n.value)
  373. }
  374. err := u.UnmarshalText(text)
  375. if err != nil {
  376. fail(err)
  377. }
  378. return true
  379. }
  380. }
  381. switch out.Kind() {
  382. case reflect.String:
  383. if tag == yaml_BINARY_TAG {
  384. out.SetString(resolved.(string))
  385. return true
  386. }
  387. if resolved != nil {
  388. out.SetString(n.value)
  389. return true
  390. }
  391. case reflect.Interface:
  392. if resolved == nil {
  393. out.Set(reflect.Zero(out.Type()))
  394. } else if tag == yaml_TIMESTAMP_TAG {
  395. // It looks like a timestamp but for backward compatibility
  396. // reasons we set it as a string, so that code that unmarshals
  397. // timestamp-like values into interface{} will continue to
  398. // see a string and not a time.Time.
  399. out.Set(reflect.ValueOf(n.value))
  400. } else {
  401. out.Set(reflect.ValueOf(resolved))
  402. }
  403. return true
  404. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  405. switch resolved := resolved.(type) {
  406. case int:
  407. if !out.OverflowInt(int64(resolved)) {
  408. out.SetInt(int64(resolved))
  409. return true
  410. }
  411. case int64:
  412. if !out.OverflowInt(resolved) {
  413. out.SetInt(resolved)
  414. return true
  415. }
  416. case uint64:
  417. if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
  418. out.SetInt(int64(resolved))
  419. return true
  420. }
  421. case float64:
  422. if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
  423. out.SetInt(int64(resolved))
  424. return true
  425. }
  426. case string:
  427. if out.Type() == durationType {
  428. d, err := time.ParseDuration(resolved)
  429. if err == nil {
  430. out.SetInt(int64(d))
  431. return true
  432. }
  433. }
  434. }
  435. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  436. switch resolved := resolved.(type) {
  437. case int:
  438. if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
  439. out.SetUint(uint64(resolved))
  440. return true
  441. }
  442. case int64:
  443. if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
  444. out.SetUint(uint64(resolved))
  445. return true
  446. }
  447. case uint64:
  448. if !out.OverflowUint(uint64(resolved)) {
  449. out.SetUint(uint64(resolved))
  450. return true
  451. }
  452. case float64:
  453. if resolved <= math.MaxUint64 && !out.OverflowUint(uint64(resolved)) {
  454. out.SetUint(uint64(resolved))
  455. return true
  456. }
  457. }
  458. case reflect.Bool:
  459. switch resolved := resolved.(type) {
  460. case bool:
  461. out.SetBool(resolved)
  462. return true
  463. }
  464. case reflect.Float32, reflect.Float64:
  465. switch resolved := resolved.(type) {
  466. case int:
  467. out.SetFloat(float64(resolved))
  468. return true
  469. case int64:
  470. out.SetFloat(float64(resolved))
  471. return true
  472. case uint64:
  473. out.SetFloat(float64(resolved))
  474. return true
  475. case float64:
  476. out.SetFloat(resolved)
  477. return true
  478. }
  479. case reflect.Struct:
  480. if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
  481. out.Set(resolvedv)
  482. return true
  483. }
  484. case reflect.Ptr:
  485. if out.Type().Elem() == reflect.TypeOf(resolved) {
  486. // TODO DOes this make sense? When is out a Ptr except when decoding a nil value?
  487. elem := reflect.New(out.Type().Elem())
  488. elem.Elem().Set(reflect.ValueOf(resolved))
  489. out.Set(elem)
  490. return true
  491. }
  492. }
  493. d.terror(n, tag, out)
  494. return false
  495. }
  496. func settableValueOf(i interface{}) reflect.Value {
  497. v := reflect.ValueOf(i)
  498. sv := reflect.New(v.Type()).Elem()
  499. sv.Set(v)
  500. return sv
  501. }
  502. func (d *decoder) sequence(n *node, out reflect.Value) (good bool) {
  503. l := len(n.children)
  504. var iface reflect.Value
  505. switch out.Kind() {
  506. case reflect.Slice:
  507. out.Set(reflect.MakeSlice(out.Type(), l, l))
  508. case reflect.Interface:
  509. // No type hints. Will have to use a generic sequence.
  510. iface = out
  511. out = settableValueOf(make([]interface{}, l))
  512. default:
  513. d.terror(n, yaml_SEQ_TAG, out)
  514. return false
  515. }
  516. et := out.Type().Elem()
  517. j := 0
  518. for i := 0; i < l; i++ {
  519. e := reflect.New(et).Elem()
  520. if ok := d.unmarshal(n.children[i], e); ok {
  521. out.Index(j).Set(e)
  522. j++
  523. }
  524. }
  525. out.Set(out.Slice(0, j))
  526. if iface.IsValid() {
  527. iface.Set(out)
  528. }
  529. return true
  530. }
  531. func (d *decoder) mapping(n *node, out reflect.Value) (good bool) {
  532. switch out.Kind() {
  533. case reflect.Struct:
  534. return d.mappingStruct(n, out)
  535. case reflect.Slice:
  536. return d.mappingSlice(n, out)
  537. case reflect.Map:
  538. // okay
  539. case reflect.Interface:
  540. if d.mapType.Kind() == reflect.Map {
  541. iface := out
  542. out = reflect.MakeMap(d.mapType)
  543. iface.Set(out)
  544. } else {
  545. slicev := reflect.New(d.mapType).Elem()
  546. if !d.mappingSlice(n, slicev) {
  547. return false
  548. }
  549. out.Set(slicev)
  550. return true
  551. }
  552. default:
  553. d.terror(n, yaml_MAP_TAG, out)
  554. return false
  555. }
  556. outt := out.Type()
  557. kt := outt.Key()
  558. et := outt.Elem()
  559. mapType := d.mapType
  560. if outt.Key() == ifaceType && outt.Elem() == ifaceType {
  561. d.mapType = outt
  562. }
  563. if out.IsNil() {
  564. out.Set(reflect.MakeMap(outt))
  565. }
  566. l := len(n.children)
  567. for i := 0; i < l; i += 2 {
  568. if isMerge(n.children[i]) {
  569. d.merge(n.children[i+1], out)
  570. continue
  571. }
  572. k := reflect.New(kt).Elem()
  573. if d.unmarshal(n.children[i], k) {
  574. kkind := k.Kind()
  575. if kkind == reflect.Interface {
  576. kkind = k.Elem().Kind()
  577. }
  578. if kkind == reflect.Map || kkind == reflect.Slice {
  579. failf("invalid map key: %#v", k.Interface())
  580. }
  581. e := reflect.New(et).Elem()
  582. if d.unmarshal(n.children[i+1], e) {
  583. d.setMapIndex(n.children[i+1], out, k, e)
  584. }
  585. }
  586. }
  587. d.mapType = mapType
  588. return true
  589. }
  590. func (d *decoder) setMapIndex(n *node, out, k, v reflect.Value) {
  591. if d.strict && out.MapIndex(k) != zeroValue {
  592. d.terrors = append(d.terrors, fmt.Sprintf("line %d: key %#v already set in map", n.line+1, k.Interface()))
  593. return
  594. }
  595. out.SetMapIndex(k, v)
  596. }
  597. func (d *decoder) mappingSlice(n *node, out reflect.Value) (good bool) {
  598. outt := out.Type()
  599. if outt.Elem() != mapItemType {
  600. d.terror(n, yaml_MAP_TAG, out)
  601. return false
  602. }
  603. mapType := d.mapType
  604. d.mapType = outt
  605. var slice []MapItem
  606. var l = len(n.children)
  607. for i := 0; i < l; i += 2 {
  608. if isMerge(n.children[i]) {
  609. d.merge(n.children[i+1], out)
  610. continue
  611. }
  612. item := MapItem{}
  613. k := reflect.ValueOf(&item.Key).Elem()
  614. if d.unmarshal(n.children[i], k) {
  615. v := reflect.ValueOf(&item.Value).Elem()
  616. if d.unmarshal(n.children[i+1], v) {
  617. slice = append(slice, item)
  618. }
  619. }
  620. }
  621. out.Set(reflect.ValueOf(slice))
  622. d.mapType = mapType
  623. return true
  624. }
  625. func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) {
  626. sinfo, err := getStructInfo(out.Type())
  627. if err != nil {
  628. panic(err)
  629. }
  630. name := settableValueOf("")
  631. l := len(n.children)
  632. var inlineMap reflect.Value
  633. var elemType reflect.Type
  634. if sinfo.InlineMap != -1 {
  635. inlineMap = out.Field(sinfo.InlineMap)
  636. inlineMap.Set(reflect.New(inlineMap.Type()).Elem())
  637. elemType = inlineMap.Type().Elem()
  638. }
  639. var doneFields []bool
  640. if d.strict {
  641. doneFields = make([]bool, len(sinfo.FieldsList))
  642. }
  643. for i := 0; i < l; i += 2 {
  644. ni := n.children[i]
  645. if isMerge(ni) {
  646. d.merge(n.children[i+1], out)
  647. continue
  648. }
  649. if !d.unmarshal(ni, name) {
  650. continue
  651. }
  652. if info, ok := sinfo.FieldsMap[name.String()]; ok {
  653. if d.strict {
  654. if doneFields[info.Id] {
  655. d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s already set in type %s", ni.line+1, name.String(), out.Type()))
  656. continue
  657. }
  658. doneFields[info.Id] = true
  659. }
  660. var field reflect.Value
  661. if info.Inline == nil {
  662. field = out.Field(info.Num)
  663. } else {
  664. field = out.FieldByIndex(info.Inline)
  665. }
  666. d.unmarshal(n.children[i+1], field)
  667. } else if sinfo.InlineMap != -1 {
  668. if inlineMap.IsNil() {
  669. inlineMap.Set(reflect.MakeMap(inlineMap.Type()))
  670. }
  671. value := reflect.New(elemType).Elem()
  672. d.unmarshal(n.children[i+1], value)
  673. d.setMapIndex(n.children[i+1], inlineMap, name, value)
  674. } else if d.strict {
  675. d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s not found in type %s", ni.line+1, name.String(), out.Type()))
  676. }
  677. }
  678. return true
  679. }
  680. func failWantMap() {
  681. failf("map merge requires map or sequence of maps as the value")
  682. }
  683. func (d *decoder) merge(n *node, out reflect.Value) {
  684. switch n.kind {
  685. case mappingNode:
  686. d.unmarshal(n, out)
  687. case aliasNode:
  688. an, ok := d.doc.anchors[n.value]
  689. if ok && an.kind != mappingNode {
  690. failWantMap()
  691. }
  692. d.unmarshal(n, out)
  693. case sequenceNode:
  694. // Step backwards as earlier nodes take precedence.
  695. for i := len(n.children) - 1; i >= 0; i-- {
  696. ni := n.children[i]
  697. if ni.kind == aliasNode {
  698. an, ok := d.doc.anchors[ni.value]
  699. if ok && an.kind != mappingNode {
  700. failWantMap()
  701. }
  702. } else if ni.kind != mappingNode {
  703. failWantMap()
  704. }
  705. d.unmarshal(ni, out)
  706. }
  707. default:
  708. failWantMap()
  709. }
  710. }
  711. func isMerge(n *node) bool {
  712. return n.kind == scalarNode && n.value == "<<" && (n.implicit == true || n.tag == yaml_MERGE_TAG)
  713. }