decode.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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. case yaml_TAIL_COMMENT_EVENT:
  143. panic("internal error: unexpected tail comment event (please report)")
  144. default:
  145. panic("internal error: attempted to parse unknown event (please report): " + p.event.typ.String())
  146. }
  147. }
  148. func (p *parser) node(kind Kind, defaultTag, tag, value string) *Node {
  149. var style Style
  150. if tag != "" && tag != "!" {
  151. tag = shortTag(tag)
  152. style = TaggedStyle
  153. } else if defaultTag != "" {
  154. tag = defaultTag
  155. } else if kind == ScalarNode {
  156. tag, _ = resolve("", value)
  157. }
  158. return &Node{
  159. Kind: kind,
  160. Tag: tag,
  161. Value: value,
  162. Style: style,
  163. Line: p.event.start_mark.line + 1,
  164. Column: p.event.start_mark.column + 1,
  165. HeadComment: string(p.event.head_comment),
  166. LineComment: string(p.event.line_comment),
  167. FootComment: string(p.event.foot_comment),
  168. }
  169. }
  170. func (p *parser) parseChild(parent *Node) *Node {
  171. child := p.parse()
  172. parent.Content = append(parent.Content, child)
  173. return child
  174. }
  175. func (p *parser) document() *Node {
  176. n := p.node(DocumentNode, "", "", "")
  177. p.doc = n
  178. p.expect(yaml_DOCUMENT_START_EVENT)
  179. p.parseChild(n)
  180. if p.peek() == yaml_DOCUMENT_END_EVENT {
  181. n.FootComment = string(p.event.foot_comment)
  182. }
  183. p.expect(yaml_DOCUMENT_END_EVENT)
  184. return n
  185. }
  186. func (p *parser) alias() *Node {
  187. n := p.node(AliasNode, "", "", string(p.event.anchor))
  188. n.Alias = p.anchors[n.Value]
  189. if n.Alias == nil {
  190. failf("unknown anchor '%s' referenced", n.Value)
  191. }
  192. p.expect(yaml_ALIAS_EVENT)
  193. return n
  194. }
  195. func (p *parser) scalar() *Node {
  196. var parsedStyle = p.event.scalar_style()
  197. var nodeStyle Style
  198. switch {
  199. case parsedStyle&yaml_DOUBLE_QUOTED_SCALAR_STYLE != 0:
  200. nodeStyle = DoubleQuotedStyle
  201. case parsedStyle&yaml_SINGLE_QUOTED_SCALAR_STYLE != 0:
  202. nodeStyle = SingleQuotedStyle
  203. case parsedStyle&yaml_LITERAL_SCALAR_STYLE != 0:
  204. nodeStyle = LiteralStyle
  205. case parsedStyle&yaml_FOLDED_SCALAR_STYLE != 0:
  206. nodeStyle = FoldedStyle
  207. }
  208. var nodeValue = string(p.event.value)
  209. var nodeTag = string(p.event.tag)
  210. var defaultTag string
  211. if nodeStyle == 0 {
  212. if nodeValue == "<<" {
  213. defaultTag = mergeTag
  214. }
  215. } else {
  216. defaultTag = strTag
  217. }
  218. n := p.node(ScalarNode, defaultTag, nodeTag, nodeValue)
  219. n.Style |= nodeStyle
  220. p.anchor(n, p.event.anchor)
  221. p.expect(yaml_SCALAR_EVENT)
  222. return n
  223. }
  224. func (p *parser) sequence() *Node {
  225. n := p.node(SequenceNode, seqTag, string(p.event.tag), "")
  226. if p.event.sequence_style()&yaml_FLOW_SEQUENCE_STYLE != 0 {
  227. n.Style |= FlowStyle
  228. }
  229. p.anchor(n, p.event.anchor)
  230. p.expect(yaml_SEQUENCE_START_EVENT)
  231. for p.peek() != yaml_SEQUENCE_END_EVENT {
  232. p.parseChild(n)
  233. }
  234. n.LineComment = string(p.event.line_comment)
  235. n.FootComment = string(p.event.foot_comment)
  236. p.expect(yaml_SEQUENCE_END_EVENT)
  237. return n
  238. }
  239. func (p *parser) mapping() *Node {
  240. n := p.node(MappingNode, mapTag, string(p.event.tag), "")
  241. block := true
  242. if p.event.mapping_style()&yaml_FLOW_MAPPING_STYLE != 0 {
  243. block = false
  244. n.Style |= FlowStyle
  245. }
  246. p.anchor(n, p.event.anchor)
  247. p.expect(yaml_MAPPING_START_EVENT)
  248. for p.peek() != yaml_MAPPING_END_EVENT {
  249. k := p.parseChild(n)
  250. if block && k.FootComment != "" {
  251. // Must be a foot comment for the prior value when being dedented.
  252. if len(n.Content) > 2 {
  253. n.Content[len(n.Content)-3].FootComment = k.FootComment
  254. k.FootComment = ""
  255. }
  256. }
  257. v := p.parseChild(n)
  258. if k.FootComment == "" && v.FootComment != "" {
  259. k.FootComment = v.FootComment
  260. v.FootComment = ""
  261. }
  262. if p.peek() == yaml_TAIL_COMMENT_EVENT {
  263. if k.FootComment == "" {
  264. k.FootComment = string(p.event.foot_comment)
  265. }
  266. p.expect(yaml_TAIL_COMMENT_EVENT)
  267. }
  268. }
  269. n.LineComment = string(p.event.line_comment)
  270. n.FootComment = string(p.event.foot_comment)
  271. if n.Style&FlowStyle == 0 && n.FootComment != "" && len(n.Content) > 1 {
  272. n.Content[len(n.Content)-2].FootComment = n.FootComment
  273. n.FootComment = ""
  274. }
  275. p.expect(yaml_MAPPING_END_EVENT)
  276. return n
  277. }
  278. // ----------------------------------------------------------------------------
  279. // Decoder, unmarshals a node into a provided value.
  280. type decoder struct {
  281. doc *Node
  282. aliases map[*Node]bool
  283. terrors []string
  284. stringMapType reflect.Type
  285. generalMapType reflect.Type
  286. knownFields bool
  287. uniqueKeys bool
  288. decodeCount int
  289. aliasCount int
  290. aliasDepth int
  291. }
  292. var (
  293. nodeType = reflect.TypeOf(Node{})
  294. durationType = reflect.TypeOf(time.Duration(0))
  295. stringMapType = reflect.TypeOf(map[string]interface{}{})
  296. generalMapType = reflect.TypeOf(map[interface{}]interface{}{})
  297. ifaceType = generalMapType.Elem()
  298. timeType = reflect.TypeOf(time.Time{})
  299. ptrTimeType = reflect.TypeOf(&time.Time{})
  300. )
  301. func newDecoder() *decoder {
  302. d := &decoder{
  303. stringMapType: stringMapType,
  304. generalMapType: generalMapType,
  305. uniqueKeys: true,
  306. }
  307. d.aliases = make(map[*Node]bool)
  308. return d
  309. }
  310. func (d *decoder) terror(n *Node, tag string, out reflect.Value) {
  311. if n.Tag != "" {
  312. tag = n.Tag
  313. }
  314. value := n.Value
  315. if tag != seqTag && tag != mapTag {
  316. if len(value) > 10 {
  317. value = " `" + value[:7] + "...`"
  318. } else {
  319. value = " `" + value + "`"
  320. }
  321. }
  322. d.terrors = append(d.terrors, fmt.Sprintf("line %d: cannot unmarshal %s%s into %s", n.Line, shortTag(tag), value, out.Type()))
  323. }
  324. func (d *decoder) callUnmarshaler(n *Node, u Unmarshaler) (good bool) {
  325. err := u.UnmarshalYAML(n)
  326. if e, ok := err.(*TypeError); ok {
  327. d.terrors = append(d.terrors, e.Errors...)
  328. return false
  329. }
  330. if err != nil {
  331. fail(err)
  332. }
  333. return true
  334. }
  335. func (d *decoder) callObsoleteUnmarshaler(n *Node, u obsoleteUnmarshaler) (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.ShortTag() == nullTag {
  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. outi := out.Addr().Interface()
  379. if u, ok := outi.(Unmarshaler); ok {
  380. good = d.callUnmarshaler(n, u)
  381. return out, true, good
  382. }
  383. if u, ok := outi.(obsoleteUnmarshaler); ok {
  384. good = d.callObsoleteUnmarshaler(n, u)
  385. return out, true, good
  386. }
  387. }
  388. }
  389. return out, false, false
  390. }
  391. func (d *decoder) fieldByIndex(n *Node, v reflect.Value, index []int) (field reflect.Value) {
  392. if n.ShortTag() == nullTag {
  393. return reflect.Value{}
  394. }
  395. for _, num := range index {
  396. for {
  397. if v.Kind() == reflect.Ptr {
  398. if v.IsNil() {
  399. v.Set(reflect.New(v.Type().Elem()))
  400. }
  401. v = v.Elem()
  402. continue
  403. }
  404. break
  405. }
  406. v = v.Field(num)
  407. }
  408. return v
  409. }
  410. func (d *decoder) unmarshal(n *Node, out reflect.Value) (good bool) {
  411. d.decodeCount++
  412. if d.aliasDepth > 0 {
  413. d.aliasCount++
  414. }
  415. if d.aliasCount > 100 && d.decodeCount > 1000 && float64(d.aliasCount)/float64(d.decodeCount) > 0.99 {
  416. failf("document contains excessive aliasing")
  417. }
  418. if out.Type() == nodeType {
  419. out.Set(reflect.ValueOf(n).Elem())
  420. return true
  421. }
  422. switch n.Kind {
  423. case DocumentNode:
  424. return d.document(n, out)
  425. case AliasNode:
  426. return d.alias(n, out)
  427. }
  428. out, unmarshaled, good := d.prepare(n, out)
  429. if unmarshaled {
  430. return good
  431. }
  432. switch n.Kind {
  433. case ScalarNode:
  434. good = d.scalar(n, out)
  435. case MappingNode:
  436. good = d.mapping(n, out)
  437. case SequenceNode:
  438. good = d.sequence(n, out)
  439. default:
  440. panic("internal error: unknown node kind: " + strconv.Itoa(int(n.Kind)))
  441. }
  442. return good
  443. }
  444. func (d *decoder) document(n *Node, out reflect.Value) (good bool) {
  445. if len(n.Content) == 1 {
  446. d.doc = n
  447. d.unmarshal(n.Content[0], out)
  448. return true
  449. }
  450. return false
  451. }
  452. func (d *decoder) alias(n *Node, out reflect.Value) (good bool) {
  453. if d.aliases[n] {
  454. // TODO this could actually be allowed in some circumstances.
  455. failf("anchor '%s' value contains itself", n.Value)
  456. }
  457. d.aliases[n] = true
  458. d.aliasDepth++
  459. good = d.unmarshal(n.Alias, out)
  460. d.aliasDepth--
  461. delete(d.aliases, n)
  462. return good
  463. }
  464. var zeroValue reflect.Value
  465. func resetMap(out reflect.Value) {
  466. for _, k := range out.MapKeys() {
  467. out.SetMapIndex(k, zeroValue)
  468. }
  469. }
  470. func (d *decoder) scalar(n *Node, out reflect.Value) bool {
  471. var tag string
  472. var resolved interface{}
  473. if n.indicatedString() {
  474. tag = strTag
  475. resolved = n.Value
  476. } else {
  477. tag, resolved = resolve(n.Tag, n.Value)
  478. if tag == binaryTag {
  479. data, err := base64.StdEncoding.DecodeString(resolved.(string))
  480. if err != nil {
  481. failf("!!binary value contains invalid base64 data")
  482. }
  483. resolved = string(data)
  484. }
  485. }
  486. if resolved == nil {
  487. if out.CanAddr() {
  488. switch out.Kind() {
  489. case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
  490. out.Set(reflect.Zero(out.Type()))
  491. return true
  492. }
  493. }
  494. return false
  495. }
  496. if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
  497. // We've resolved to exactly the type we want, so use that.
  498. out.Set(resolvedv)
  499. return true
  500. }
  501. // Perhaps we can use the value as a TextUnmarshaler to
  502. // set its value.
  503. if out.CanAddr() {
  504. u, ok := out.Addr().Interface().(encoding.TextUnmarshaler)
  505. if ok {
  506. var text []byte
  507. if tag == binaryTag {
  508. text = []byte(resolved.(string))
  509. } else {
  510. // We let any value be unmarshaled into TextUnmarshaler.
  511. // That might be more lax than we'd like, but the
  512. // TextUnmarshaler itself should bowl out any dubious values.
  513. text = []byte(n.Value)
  514. }
  515. err := u.UnmarshalText(text)
  516. if err != nil {
  517. fail(err)
  518. }
  519. return true
  520. }
  521. }
  522. switch out.Kind() {
  523. case reflect.String:
  524. if tag == binaryTag {
  525. out.SetString(resolved.(string))
  526. return true
  527. }
  528. out.SetString(n.Value)
  529. return true
  530. case reflect.Interface:
  531. out.Set(reflect.ValueOf(resolved))
  532. return true
  533. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  534. // This used to work in v2, but it's very unfriendly.
  535. isDuration := out.Type() == durationType
  536. switch resolved := resolved.(type) {
  537. case int:
  538. if !isDuration && !out.OverflowInt(int64(resolved)) {
  539. out.SetInt(int64(resolved))
  540. return true
  541. }
  542. case int64:
  543. if !isDuration && !out.OverflowInt(resolved) {
  544. out.SetInt(resolved)
  545. return true
  546. }
  547. case uint64:
  548. if !isDuration && resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
  549. out.SetInt(int64(resolved))
  550. return true
  551. }
  552. case float64:
  553. if !isDuration && resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
  554. out.SetInt(int64(resolved))
  555. return true
  556. }
  557. case string:
  558. if out.Type() == durationType {
  559. d, err := time.ParseDuration(resolved)
  560. if err == nil {
  561. out.SetInt(int64(d))
  562. return true
  563. }
  564. }
  565. }
  566. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  567. switch resolved := resolved.(type) {
  568. case int:
  569. if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
  570. out.SetUint(uint64(resolved))
  571. return true
  572. }
  573. case int64:
  574. if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
  575. out.SetUint(uint64(resolved))
  576. return true
  577. }
  578. case uint64:
  579. if !out.OverflowUint(uint64(resolved)) {
  580. out.SetUint(uint64(resolved))
  581. return true
  582. }
  583. case float64:
  584. if resolved <= math.MaxUint64 && !out.OverflowUint(uint64(resolved)) {
  585. out.SetUint(uint64(resolved))
  586. return true
  587. }
  588. }
  589. case reflect.Bool:
  590. switch resolved := resolved.(type) {
  591. case bool:
  592. out.SetBool(resolved)
  593. return true
  594. case string:
  595. // This offers some compatibility with the 1.1 spec (https://yaml.org/type/bool.html).
  596. // It only works if explicitly attempting to unmarshal into a typed bool value.
  597. switch resolved {
  598. case "y", "Y", "yes", "Yes", "YES", "on", "On", "ON":
  599. out.SetBool(true)
  600. return true
  601. case "n", "N", "no", "No", "NO", "off", "Off", "OFF":
  602. out.SetBool(false)
  603. return true
  604. }
  605. }
  606. case reflect.Float32, reflect.Float64:
  607. switch resolved := resolved.(type) {
  608. case int:
  609. out.SetFloat(float64(resolved))
  610. return true
  611. case int64:
  612. out.SetFloat(float64(resolved))
  613. return true
  614. case uint64:
  615. out.SetFloat(float64(resolved))
  616. return true
  617. case float64:
  618. out.SetFloat(resolved)
  619. return true
  620. }
  621. case reflect.Struct:
  622. if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
  623. out.Set(resolvedv)
  624. return true
  625. }
  626. case reflect.Ptr:
  627. panic("yaml internal error: please report the issue")
  628. }
  629. d.terror(n, tag, out)
  630. return false
  631. }
  632. func settableValueOf(i interface{}) reflect.Value {
  633. v := reflect.ValueOf(i)
  634. sv := reflect.New(v.Type()).Elem()
  635. sv.Set(v)
  636. return sv
  637. }
  638. func (d *decoder) sequence(n *Node, out reflect.Value) (good bool) {
  639. l := len(n.Content)
  640. var iface reflect.Value
  641. switch out.Kind() {
  642. case reflect.Slice:
  643. out.Set(reflect.MakeSlice(out.Type(), l, l))
  644. case reflect.Array:
  645. if l != out.Len() {
  646. failf("invalid array: want %d elements but got %d", out.Len(), l)
  647. }
  648. case reflect.Interface:
  649. // No type hints. Will have to use a generic sequence.
  650. iface = out
  651. out = settableValueOf(make([]interface{}, l))
  652. default:
  653. d.terror(n, seqTag, out)
  654. return false
  655. }
  656. et := out.Type().Elem()
  657. j := 0
  658. for i := 0; i < l; i++ {
  659. e := reflect.New(et).Elem()
  660. if ok := d.unmarshal(n.Content[i], e); ok {
  661. out.Index(j).Set(e)
  662. j++
  663. }
  664. }
  665. if out.Kind() != reflect.Array {
  666. out.Set(out.Slice(0, j))
  667. }
  668. if iface.IsValid() {
  669. iface.Set(out)
  670. }
  671. return true
  672. }
  673. func (d *decoder) mapping(n *Node, out reflect.Value) (good bool) {
  674. l := len(n.Content)
  675. if d.uniqueKeys {
  676. nerrs := len(d.terrors)
  677. for i := 0; i < l; i += 2 {
  678. ni := n.Content[i]
  679. for j := i + 2; j < l; j += 2 {
  680. nj := n.Content[j]
  681. if ni.Kind == nj.Kind && ni.Value == nj.Value {
  682. d.terrors = append(d.terrors, fmt.Sprintf("line %d: mapping key %#v already defined at line %d", nj.Line, nj.Value, ni.Line))
  683. }
  684. }
  685. }
  686. if len(d.terrors) > nerrs {
  687. return false
  688. }
  689. }
  690. switch out.Kind() {
  691. case reflect.Struct:
  692. return d.mappingStruct(n, out)
  693. case reflect.Map:
  694. // okay
  695. case reflect.Interface:
  696. iface := out
  697. if isStringMap(n) {
  698. out = reflect.MakeMap(d.stringMapType)
  699. } else {
  700. out = reflect.MakeMap(d.generalMapType)
  701. }
  702. iface.Set(out)
  703. default:
  704. d.terror(n, mapTag, out)
  705. return false
  706. }
  707. outt := out.Type()
  708. kt := outt.Key()
  709. et := outt.Elem()
  710. stringMapType := d.stringMapType
  711. generalMapType := d.generalMapType
  712. if outt.Elem() == ifaceType {
  713. if outt.Key().Kind() == reflect.String {
  714. d.stringMapType = outt
  715. } else if outt.Key() == ifaceType {
  716. d.generalMapType = outt
  717. }
  718. }
  719. if out.IsNil() {
  720. out.Set(reflect.MakeMap(outt))
  721. }
  722. for i := 0; i < l; i += 2 {
  723. if isMerge(n.Content[i]) {
  724. d.merge(n.Content[i+1], out)
  725. continue
  726. }
  727. k := reflect.New(kt).Elem()
  728. if d.unmarshal(n.Content[i], k) {
  729. kkind := k.Kind()
  730. if kkind == reflect.Interface {
  731. kkind = k.Elem().Kind()
  732. }
  733. if kkind == reflect.Map || kkind == reflect.Slice {
  734. failf("invalid map key: %#v", k.Interface())
  735. }
  736. e := reflect.New(et).Elem()
  737. if d.unmarshal(n.Content[i+1], e) {
  738. out.SetMapIndex(k, e)
  739. }
  740. }
  741. }
  742. d.stringMapType = stringMapType
  743. d.generalMapType = generalMapType
  744. return true
  745. }
  746. func isStringMap(n *Node) bool {
  747. if n.Kind != MappingNode {
  748. return false
  749. }
  750. l := len(n.Content)
  751. for i := 0; i < l; i += 2 {
  752. if n.Content[i].ShortTag() != strTag {
  753. return false
  754. }
  755. }
  756. return true
  757. }
  758. func (d *decoder) mappingStruct(n *Node, out reflect.Value) (good bool) {
  759. sinfo, err := getStructInfo(out.Type())
  760. if err != nil {
  761. panic(err)
  762. }
  763. var inlineMap reflect.Value
  764. var elemType reflect.Type
  765. if sinfo.InlineMap != -1 {
  766. inlineMap = out.Field(sinfo.InlineMap)
  767. inlineMap.Set(reflect.New(inlineMap.Type()).Elem())
  768. elemType = inlineMap.Type().Elem()
  769. }
  770. for _, index := range sinfo.InlineUnmarshalers {
  771. field := d.fieldByIndex(n, out, index)
  772. d.prepare(n, field)
  773. }
  774. var doneFields []bool
  775. if d.uniqueKeys {
  776. doneFields = make([]bool, len(sinfo.FieldsList))
  777. }
  778. name := settableValueOf("")
  779. l := len(n.Content)
  780. for i := 0; i < l; i += 2 {
  781. ni := n.Content[i]
  782. if isMerge(ni) {
  783. d.merge(n.Content[i+1], out)
  784. continue
  785. }
  786. if !d.unmarshal(ni, name) {
  787. continue
  788. }
  789. if info, ok := sinfo.FieldsMap[name.String()]; ok {
  790. if d.uniqueKeys {
  791. if doneFields[info.Id] {
  792. d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s already set in type %s", ni.Line, name.String(), out.Type()))
  793. continue
  794. }
  795. doneFields[info.Id] = true
  796. }
  797. var field reflect.Value
  798. if info.Inline == nil {
  799. field = out.Field(info.Num)
  800. } else {
  801. field = d.fieldByIndex(n, out, info.Inline)
  802. }
  803. d.unmarshal(n.Content[i+1], field)
  804. } else if sinfo.InlineMap != -1 {
  805. if inlineMap.IsNil() {
  806. inlineMap.Set(reflect.MakeMap(inlineMap.Type()))
  807. }
  808. value := reflect.New(elemType).Elem()
  809. d.unmarshal(n.Content[i+1], value)
  810. inlineMap.SetMapIndex(name, value)
  811. } else if d.knownFields {
  812. d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s not found in type %s", ni.Line, name.String(), out.Type()))
  813. }
  814. }
  815. return true
  816. }
  817. func failWantMap() {
  818. failf("map merge requires map or sequence of maps as the value")
  819. }
  820. func (d *decoder) merge(n *Node, out reflect.Value) {
  821. switch n.Kind {
  822. case MappingNode:
  823. d.unmarshal(n, out)
  824. case AliasNode:
  825. if n.Alias != nil && n.Alias.Kind != MappingNode {
  826. failWantMap()
  827. }
  828. d.unmarshal(n, out)
  829. case SequenceNode:
  830. // Step backwards as earlier nodes take precedence.
  831. for i := len(n.Content) - 1; i >= 0; i-- {
  832. ni := n.Content[i]
  833. if ni.Kind == AliasNode {
  834. if ni.Alias != nil && ni.Alias.Kind != MappingNode {
  835. failWantMap()
  836. }
  837. } else if ni.Kind != MappingNode {
  838. failWantMap()
  839. }
  840. d.unmarshal(ni, out)
  841. }
  842. default:
  843. failWantMap()
  844. }
  845. }
  846. func isMerge(n *Node) bool {
  847. return n.Kind == ScalarNode && n.Value == "<<" && (n.Tag == "" || n.Tag == "!" || shortTag(n.Tag) == mergeTag)
  848. }