decode.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. if err := u.UnmarshalYAML(n); err != nil {
  326. fail(err)
  327. }
  328. return true
  329. }
  330. func (d *decoder) callObsoleteUnmarshaler(n *Node, u obsoleteUnmarshaler) (good bool) {
  331. terrlen := len(d.terrors)
  332. err := u.UnmarshalYAML(func(v interface{}) (err error) {
  333. defer handleErr(&err)
  334. d.unmarshal(n, reflect.ValueOf(v))
  335. if len(d.terrors) > terrlen {
  336. issues := d.terrors[terrlen:]
  337. d.terrors = d.terrors[:terrlen]
  338. return &TypeError{issues}
  339. }
  340. return nil
  341. })
  342. if e, ok := err.(*TypeError); ok {
  343. d.terrors = append(d.terrors, e.Errors...)
  344. return false
  345. }
  346. if err != nil {
  347. fail(err)
  348. }
  349. return true
  350. }
  351. // d.prepare initializes and dereferences pointers and calls UnmarshalYAML
  352. // if a value is found to implement it.
  353. // It returns the initialized and dereferenced out value, whether
  354. // unmarshalling was already done by UnmarshalYAML, and if so whether
  355. // its types unmarshalled appropriately.
  356. //
  357. // If n holds a null value, prepare returns before doing anything.
  358. func (d *decoder) prepare(n *Node, out reflect.Value) (newout reflect.Value, unmarshaled, good bool) {
  359. if n.ShortTag() == nullTag {
  360. return out, false, false
  361. }
  362. again := true
  363. for again {
  364. again = false
  365. if out.Kind() == reflect.Ptr {
  366. if out.IsNil() {
  367. out.Set(reflect.New(out.Type().Elem()))
  368. }
  369. out = out.Elem()
  370. again = true
  371. }
  372. if out.CanAddr() {
  373. outi := out.Addr().Interface()
  374. if u, ok := outi.(Unmarshaler); ok {
  375. good = d.callUnmarshaler(n, u)
  376. return out, true, good
  377. }
  378. if u, ok := outi.(obsoleteUnmarshaler); ok {
  379. good = d.callObsoleteUnmarshaler(n, u)
  380. return out, true, good
  381. }
  382. }
  383. }
  384. return out, false, false
  385. }
  386. func (d *decoder) fieldByIndex(n *Node, v reflect.Value, index []int) (field reflect.Value) {
  387. if n.ShortTag() == nullTag {
  388. return reflect.Value{}
  389. }
  390. for _, num := range index {
  391. for {
  392. if v.Kind() == reflect.Ptr {
  393. if v.IsNil() {
  394. v.Set(reflect.New(v.Type().Elem()))
  395. }
  396. v = v.Elem()
  397. continue
  398. }
  399. break
  400. }
  401. v = v.Field(num)
  402. }
  403. return v
  404. }
  405. func (d *decoder) unmarshal(n *Node, out reflect.Value) (good bool) {
  406. d.decodeCount++
  407. if d.aliasDepth > 0 {
  408. d.aliasCount++
  409. }
  410. if d.aliasCount > 100 && d.decodeCount > 1000 && float64(d.aliasCount)/float64(d.decodeCount) > 0.99 {
  411. failf("document contains excessive aliasing")
  412. }
  413. if out.Type() == nodeType {
  414. out.Set(reflect.ValueOf(n).Elem())
  415. return true
  416. }
  417. switch n.Kind {
  418. case DocumentNode:
  419. return d.document(n, out)
  420. case AliasNode:
  421. return d.alias(n, out)
  422. }
  423. out, unmarshaled, good := d.prepare(n, out)
  424. if unmarshaled {
  425. return good
  426. }
  427. switch n.Kind {
  428. case ScalarNode:
  429. good = d.scalar(n, out)
  430. case MappingNode:
  431. good = d.mapping(n, out)
  432. case SequenceNode:
  433. good = d.sequence(n, out)
  434. default:
  435. panic("internal error: unknown node kind: " + strconv.Itoa(int(n.Kind)))
  436. }
  437. return good
  438. }
  439. func (d *decoder) document(n *Node, out reflect.Value) (good bool) {
  440. if len(n.Content) == 1 {
  441. d.doc = n
  442. d.unmarshal(n.Content[0], out)
  443. return true
  444. }
  445. return false
  446. }
  447. func (d *decoder) alias(n *Node, out reflect.Value) (good bool) {
  448. if d.aliases[n] {
  449. // TODO this could actually be allowed in some circumstances.
  450. failf("anchor '%s' value contains itself", n.Value)
  451. }
  452. d.aliases[n] = true
  453. d.aliasDepth++
  454. good = d.unmarshal(n.Alias, out)
  455. d.aliasDepth--
  456. delete(d.aliases, n)
  457. return good
  458. }
  459. var zeroValue reflect.Value
  460. func resetMap(out reflect.Value) {
  461. for _, k := range out.MapKeys() {
  462. out.SetMapIndex(k, zeroValue)
  463. }
  464. }
  465. func (d *decoder) scalar(n *Node, out reflect.Value) bool {
  466. var tag string
  467. var resolved interface{}
  468. if n.indicatedString() {
  469. tag = strTag
  470. resolved = n.Value
  471. } else {
  472. tag, resolved = resolve(n.Tag, n.Value)
  473. if tag == binaryTag {
  474. data, err := base64.StdEncoding.DecodeString(resolved.(string))
  475. if err != nil {
  476. failf("!!binary value contains invalid base64 data")
  477. }
  478. resolved = string(data)
  479. }
  480. }
  481. if resolved == nil {
  482. if out.CanAddr() {
  483. switch out.Kind() {
  484. case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
  485. out.Set(reflect.Zero(out.Type()))
  486. return true
  487. }
  488. }
  489. return false
  490. }
  491. if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
  492. // We've resolved to exactly the type we want, so use that.
  493. out.Set(resolvedv)
  494. return true
  495. }
  496. // Perhaps we can use the value as a TextUnmarshaler to
  497. // set its value.
  498. if out.CanAddr() {
  499. u, ok := out.Addr().Interface().(encoding.TextUnmarshaler)
  500. if ok {
  501. var text []byte
  502. if tag == binaryTag {
  503. text = []byte(resolved.(string))
  504. } else {
  505. // We let any value be unmarshaled into TextUnmarshaler.
  506. // That might be more lax than we'd like, but the
  507. // TextUnmarshaler itself should bowl out any dubious values.
  508. text = []byte(n.Value)
  509. }
  510. err := u.UnmarshalText(text)
  511. if err != nil {
  512. fail(err)
  513. }
  514. return true
  515. }
  516. }
  517. switch out.Kind() {
  518. case reflect.String:
  519. if tag == binaryTag {
  520. out.SetString(resolved.(string))
  521. return true
  522. }
  523. out.SetString(n.Value)
  524. return true
  525. case reflect.Interface:
  526. out.Set(reflect.ValueOf(resolved))
  527. return true
  528. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  529. // This used to work in v2, but it's very unfriendly.
  530. isDuration := out.Type() == durationType
  531. switch resolved := resolved.(type) {
  532. case int:
  533. if !isDuration && !out.OverflowInt(int64(resolved)) {
  534. out.SetInt(int64(resolved))
  535. return true
  536. }
  537. case int64:
  538. if !isDuration && !out.OverflowInt(resolved) {
  539. out.SetInt(resolved)
  540. return true
  541. }
  542. case uint64:
  543. if !isDuration && resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
  544. out.SetInt(int64(resolved))
  545. return true
  546. }
  547. case float64:
  548. if !isDuration && resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
  549. out.SetInt(int64(resolved))
  550. return true
  551. }
  552. case string:
  553. if out.Type() == durationType {
  554. d, err := time.ParseDuration(resolved)
  555. if err == nil {
  556. out.SetInt(int64(d))
  557. return true
  558. }
  559. }
  560. }
  561. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  562. switch resolved := resolved.(type) {
  563. case int:
  564. if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
  565. out.SetUint(uint64(resolved))
  566. return true
  567. }
  568. case int64:
  569. if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
  570. out.SetUint(uint64(resolved))
  571. return true
  572. }
  573. case uint64:
  574. if !out.OverflowUint(uint64(resolved)) {
  575. out.SetUint(uint64(resolved))
  576. return true
  577. }
  578. case float64:
  579. if resolved <= math.MaxUint64 && !out.OverflowUint(uint64(resolved)) {
  580. out.SetUint(uint64(resolved))
  581. return true
  582. }
  583. }
  584. case reflect.Bool:
  585. switch resolved := resolved.(type) {
  586. case bool:
  587. out.SetBool(resolved)
  588. return true
  589. case string:
  590. // This offers some compatibility with the 1.1 spec (https://yaml.org/type/bool.html).
  591. // It only works if explicitly attempting to unmarshal into a typed bool value.
  592. switch resolved {
  593. case "y", "Y", "yes", "Yes", "YES", "on", "On", "ON":
  594. out.SetBool(true)
  595. return true
  596. case "n", "N", "no", "No", "NO", "off", "Off", "OFF":
  597. out.SetBool(false)
  598. return true
  599. }
  600. }
  601. case reflect.Float32, reflect.Float64:
  602. switch resolved := resolved.(type) {
  603. case int:
  604. out.SetFloat(float64(resolved))
  605. return true
  606. case int64:
  607. out.SetFloat(float64(resolved))
  608. return true
  609. case uint64:
  610. out.SetFloat(float64(resolved))
  611. return true
  612. case float64:
  613. out.SetFloat(resolved)
  614. return true
  615. }
  616. case reflect.Struct:
  617. if resolvedv := reflect.ValueOf(resolved); out.Type() == resolvedv.Type() {
  618. out.Set(resolvedv)
  619. return true
  620. }
  621. case reflect.Ptr:
  622. panic("yaml internal error: please report the issue")
  623. }
  624. d.terror(n, tag, out)
  625. return false
  626. }
  627. func settableValueOf(i interface{}) reflect.Value {
  628. v := reflect.ValueOf(i)
  629. sv := reflect.New(v.Type()).Elem()
  630. sv.Set(v)
  631. return sv
  632. }
  633. func (d *decoder) sequence(n *Node, out reflect.Value) (good bool) {
  634. l := len(n.Content)
  635. var iface reflect.Value
  636. switch out.Kind() {
  637. case reflect.Slice:
  638. out.Set(reflect.MakeSlice(out.Type(), l, l))
  639. case reflect.Array:
  640. if l != out.Len() {
  641. failf("invalid array: want %d elements but got %d", out.Len(), l)
  642. }
  643. case reflect.Interface:
  644. // No type hints. Will have to use a generic sequence.
  645. iface = out
  646. out = settableValueOf(make([]interface{}, l))
  647. default:
  648. d.terror(n, seqTag, out)
  649. return false
  650. }
  651. et := out.Type().Elem()
  652. j := 0
  653. for i := 0; i < l; i++ {
  654. e := reflect.New(et).Elem()
  655. if ok := d.unmarshal(n.Content[i], e); ok {
  656. out.Index(j).Set(e)
  657. j++
  658. }
  659. }
  660. if out.Kind() != reflect.Array {
  661. out.Set(out.Slice(0, j))
  662. }
  663. if iface.IsValid() {
  664. iface.Set(out)
  665. }
  666. return true
  667. }
  668. func (d *decoder) mapping(n *Node, out reflect.Value) (good bool) {
  669. l := len(n.Content)
  670. if d.uniqueKeys {
  671. nerrs := len(d.terrors)
  672. for i := 0; i < l; i += 2 {
  673. ni := n.Content[i]
  674. for j := i + 2; j < l; j += 2 {
  675. nj := n.Content[j]
  676. if ni.Kind == nj.Kind && ni.Value == nj.Value {
  677. d.terrors = append(d.terrors, fmt.Sprintf("line %d: mapping key %#v already defined at line %d", nj.Line, nj.Value, ni.Line))
  678. }
  679. }
  680. }
  681. if len(d.terrors) > nerrs {
  682. return false
  683. }
  684. }
  685. switch out.Kind() {
  686. case reflect.Struct:
  687. return d.mappingStruct(n, out)
  688. case reflect.Map:
  689. // okay
  690. case reflect.Interface:
  691. iface := out
  692. if isStringMap(n) {
  693. out = reflect.MakeMap(d.stringMapType)
  694. } else {
  695. out = reflect.MakeMap(d.generalMapType)
  696. }
  697. iface.Set(out)
  698. default:
  699. d.terror(n, mapTag, out)
  700. return false
  701. }
  702. outt := out.Type()
  703. kt := outt.Key()
  704. et := outt.Elem()
  705. stringMapType := d.stringMapType
  706. generalMapType := d.generalMapType
  707. if outt.Elem() == ifaceType {
  708. if outt.Key().Kind() == reflect.String {
  709. d.stringMapType = outt
  710. } else if outt.Key() == ifaceType {
  711. d.generalMapType = outt
  712. }
  713. }
  714. if out.IsNil() {
  715. out.Set(reflect.MakeMap(outt))
  716. }
  717. for i := 0; i < l; i += 2 {
  718. if isMerge(n.Content[i]) {
  719. d.merge(n.Content[i+1], out)
  720. continue
  721. }
  722. k := reflect.New(kt).Elem()
  723. if d.unmarshal(n.Content[i], k) {
  724. kkind := k.Kind()
  725. if kkind == reflect.Interface {
  726. kkind = k.Elem().Kind()
  727. }
  728. if kkind == reflect.Map || kkind == reflect.Slice {
  729. failf("invalid map key: %#v", k.Interface())
  730. }
  731. e := reflect.New(et).Elem()
  732. if d.unmarshal(n.Content[i+1], e) {
  733. out.SetMapIndex(k, e)
  734. }
  735. }
  736. }
  737. d.stringMapType = stringMapType
  738. d.generalMapType = generalMapType
  739. return true
  740. }
  741. func isStringMap(n *Node) bool {
  742. if n.Kind != MappingNode {
  743. return false
  744. }
  745. l := len(n.Content)
  746. for i := 0; i < l; i++ {
  747. if n.Content[i].ShortTag() != strTag {
  748. return false
  749. }
  750. }
  751. return true
  752. }
  753. func (d *decoder) mappingStruct(n *Node, out reflect.Value) (good bool) {
  754. sinfo, err := getStructInfo(out.Type())
  755. if err != nil {
  756. panic(err)
  757. }
  758. var inlineMap reflect.Value
  759. var elemType reflect.Type
  760. if sinfo.InlineMap != -1 {
  761. inlineMap = out.Field(sinfo.InlineMap)
  762. inlineMap.Set(reflect.New(inlineMap.Type()).Elem())
  763. elemType = inlineMap.Type().Elem()
  764. }
  765. for _, index := range sinfo.InlineUnmarshalers {
  766. field := d.fieldByIndex(n, out, index)
  767. d.prepare(n, field)
  768. }
  769. var doneFields []bool
  770. if d.uniqueKeys {
  771. doneFields = make([]bool, len(sinfo.FieldsList))
  772. }
  773. name := settableValueOf("")
  774. l := len(n.Content)
  775. for i := 0; i < l; i += 2 {
  776. ni := n.Content[i]
  777. if isMerge(ni) {
  778. d.merge(n.Content[i+1], out)
  779. continue
  780. }
  781. if !d.unmarshal(ni, name) {
  782. continue
  783. }
  784. if info, ok := sinfo.FieldsMap[name.String()]; ok {
  785. if d.uniqueKeys {
  786. if doneFields[info.Id] {
  787. d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s already set in type %s", ni.Line, name.String(), out.Type()))
  788. continue
  789. }
  790. doneFields[info.Id] = true
  791. }
  792. var field reflect.Value
  793. if info.Inline == nil {
  794. field = out.Field(info.Num)
  795. } else {
  796. field = d.fieldByIndex(n, out, info.Inline)
  797. }
  798. d.unmarshal(n.Content[i+1], field)
  799. } else if sinfo.InlineMap != -1 {
  800. if inlineMap.IsNil() {
  801. inlineMap.Set(reflect.MakeMap(inlineMap.Type()))
  802. }
  803. value := reflect.New(elemType).Elem()
  804. d.unmarshal(n.Content[i+1], value)
  805. inlineMap.SetMapIndex(name, value)
  806. } else if d.knownFields {
  807. d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s not found in type %s", ni.Line, name.String(), out.Type()))
  808. }
  809. }
  810. return true
  811. }
  812. func failWantMap() {
  813. failf("map merge requires map or sequence of maps as the value")
  814. }
  815. func (d *decoder) merge(n *Node, out reflect.Value) {
  816. switch n.Kind {
  817. case MappingNode:
  818. d.unmarshal(n, out)
  819. case AliasNode:
  820. if n.Alias != nil && n.Alias.Kind != MappingNode {
  821. failWantMap()
  822. }
  823. d.unmarshal(n, out)
  824. case SequenceNode:
  825. // Step backwards as earlier nodes take precedence.
  826. for i := len(n.Content) - 1; i >= 0; i-- {
  827. ni := n.Content[i]
  828. if ni.Kind == AliasNode {
  829. if ni.Alias != nil && ni.Alias.Kind != MappingNode {
  830. failWantMap()
  831. }
  832. } else if ni.Kind != MappingNode {
  833. failWantMap()
  834. }
  835. d.unmarshal(ni, out)
  836. }
  837. default:
  838. failWantMap()
  839. }
  840. }
  841. func isMerge(n *Node) bool {
  842. return n.Kind == ScalarNode && n.Value == "<<" && (n.Tag == "" || n.Tag == "!" || shortTag(n.Tag) == mergeTag)
  843. }