decode.go 20 KB

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