decode.go 21 KB

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