decode.go 23 KB

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