decode.go 20 KB

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