decode.go 20 KB

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