decode.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. package yaml
  2. import (
  3. "encoding"
  4. "encoding/base64"
  5. "fmt"
  6. "math"
  7. "reflect"
  8. "strconv"
  9. "time"
  10. )
  11. const (
  12. documentNode = 1 << iota
  13. mappingNode
  14. sequenceNode
  15. scalarNode
  16. aliasNode
  17. )
  18. type node struct {
  19. kind int
  20. line, column int
  21. tag string
  22. // For an alias node, alias holds the resolved alias.
  23. alias *node
  24. value string
  25. implicit bool
  26. children []*node
  27. anchors map[string]*node
  28. }
  29. // ----------------------------------------------------------------------------
  30. // Parser, produces a node tree out of a libyaml event stream.
  31. type parser struct {
  32. parser yaml_parser_t
  33. event yaml_event_t
  34. doc *node
  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. p.skip()
  46. if p.event.typ != yaml_STREAM_START_EVENT {
  47. panic("expected stream start event, got " + strconv.Itoa(int(p.event.typ)))
  48. }
  49. p.skip()
  50. return &p
  51. }
  52. func (p *parser) destroy() {
  53. if p.event.typ != yaml_NO_EVENT {
  54. yaml_event_delete(&p.event)
  55. }
  56. yaml_parser_delete(&p.parser)
  57. }
  58. func (p *parser) skip() {
  59. if p.event.typ != yaml_NO_EVENT {
  60. if p.event.typ == yaml_STREAM_END_EVENT {
  61. failf("attempted to go past the end of stream; corrupted value?")
  62. }
  63. yaml_event_delete(&p.event)
  64. }
  65. if !yaml_parser_parse(&p.parser, &p.event) {
  66. p.fail()
  67. }
  68. }
  69. func (p *parser) fail() {
  70. var where string
  71. var line int
  72. if p.parser.problem_mark.line != 0 {
  73. line = p.parser.problem_mark.line
  74. } else if p.parser.context_mark.line != 0 {
  75. line = p.parser.context_mark.line
  76. }
  77. if line != 0 {
  78. where = "line " + strconv.Itoa(line) + ": "
  79. }
  80. var msg string
  81. if len(p.parser.problem) > 0 {
  82. msg = p.parser.problem
  83. } else {
  84. msg = "unknown problem parsing YAML content"
  85. }
  86. failf("%s%s", where, msg)
  87. }
  88. func (p *parser) anchor(n *node, anchor []byte) {
  89. if anchor != nil {
  90. p.doc.anchors[string(anchor)] = n
  91. }
  92. }
  93. func (p *parser) parse() *node {
  94. switch p.event.typ {
  95. case yaml_SCALAR_EVENT:
  96. return p.scalar()
  97. case yaml_ALIAS_EVENT:
  98. return p.alias()
  99. case yaml_MAPPING_START_EVENT:
  100. return p.mapping()
  101. case yaml_SEQUENCE_START_EVENT:
  102. return p.sequence()
  103. case yaml_DOCUMENT_START_EVENT:
  104. return p.document()
  105. case yaml_STREAM_END_EVENT:
  106. // Happens when attempting to decode an empty buffer.
  107. return nil
  108. default:
  109. panic("attempted to parse unknown event: " + strconv.Itoa(int(p.event.typ)))
  110. }
  111. }
  112. func (p *parser) node(kind int) *node {
  113. return &node{
  114. kind: kind,
  115. line: p.event.start_mark.line,
  116. column: p.event.start_mark.column,
  117. }
  118. }
  119. func (p *parser) document() *node {
  120. n := p.node(documentNode)
  121. n.anchors = make(map[string]*node)
  122. p.doc = n
  123. p.skip()
  124. n.children = append(n.children, p.parse())
  125. if p.event.typ != yaml_DOCUMENT_END_EVENT {
  126. panic("expected end of document event but got " + strconv.Itoa(int(p.event.typ)))
  127. }
  128. p.skip()
  129. return n
  130. }
  131. func (p *parser) alias() *node {
  132. n := p.node(aliasNode)
  133. n.value = string(p.event.anchor)
  134. n.alias = p.doc.anchors[n.value]
  135. if n.alias == nil {
  136. failf("unknown anchor '%s' referenced", n.value)
  137. }
  138. p.skip()
  139. return n
  140. }
  141. func (p *parser) scalar() *node {
  142. n := p.node(scalarNode)
  143. n.value = string(p.event.value)
  144. n.tag = string(p.event.tag)
  145. n.implicit = p.event.implicit
  146. p.anchor(n, p.event.anchor)
  147. p.skip()
  148. return n
  149. }
  150. func (p *parser) sequence() *node {
  151. n := p.node(sequenceNode)
  152. p.anchor(n, p.event.anchor)
  153. p.skip()
  154. for p.event.typ != yaml_SEQUENCE_END_EVENT {
  155. n.children = append(n.children, p.parse())
  156. }
  157. p.skip()
  158. return n
  159. }
  160. func (p *parser) mapping() *node {
  161. n := p.node(mappingNode)
  162. p.anchor(n, p.event.anchor)
  163. p.skip()
  164. for p.event.typ != yaml_MAPPING_END_EVENT {
  165. n.children = append(n.children, p.parse(), p.parse())
  166. }
  167. p.skip()
  168. return n
  169. }
  170. // ----------------------------------------------------------------------------
  171. // Decoder, unmarshals a node into a provided value.
  172. type decoder struct {
  173. doc *node
  174. aliases map[*node]bool
  175. mapType reflect.Type
  176. terrors []string
  177. strict bool
  178. }
  179. var (
  180. mapItemType = reflect.TypeOf(MapItem{})
  181. durationType = reflect.TypeOf(time.Duration(0))
  182. defaultMapType = reflect.TypeOf(map[interface{}]interface{}{})
  183. ifaceType = defaultMapType.Elem()
  184. )
  185. func newDecoder(strict bool) *decoder {
  186. d := &decoder{mapType: defaultMapType, strict: strict}
  187. d.aliases = make(map[*node]bool)
  188. return d
  189. }
  190. func (d *decoder) terror(n *node, tag string, out reflect.Value) {
  191. if n.tag != "" {
  192. tag = n.tag
  193. }
  194. value := n.value
  195. if tag != yaml_SEQ_TAG && tag != yaml_MAP_TAG {
  196. if len(value) > 10 {
  197. value = " `" + value[:7] + "...`"
  198. } else {
  199. value = " `" + value + "`"
  200. }
  201. }
  202. d.terrors = append(d.terrors, fmt.Sprintf("line %d: cannot unmarshal %s%s into %s", n.line+1, shortTag(tag), value, out.Type()))
  203. }
  204. func (d *decoder) callUnmarshaler(n *node, u Unmarshaler) (good bool) {
  205. terrlen := len(d.terrors)
  206. err := u.UnmarshalYAML(func(v interface{}) (err error) {
  207. defer handleErr(&err)
  208. d.unmarshal(n, reflect.ValueOf(v))
  209. if len(d.terrors) > terrlen {
  210. issues := d.terrors[terrlen:]
  211. d.terrors = d.terrors[:terrlen]
  212. return &TypeError{issues}
  213. }
  214. return nil
  215. })
  216. if e, ok := err.(*TypeError); ok {
  217. d.terrors = append(d.terrors, e.Errors...)
  218. return false
  219. }
  220. if err != nil {
  221. fail(err)
  222. }
  223. return true
  224. }
  225. // d.prepare initializes and dereferences pointers and calls UnmarshalYAML
  226. // if a value is found to implement it.
  227. // It returns the initialized and dereferenced out value, whether
  228. // unmarshalling was already done by UnmarshalYAML, and if so whether
  229. // its types unmarshalled appropriately.
  230. //
  231. // If n holds a null value, prepare returns before doing anything.
  232. func (d *decoder) prepare(n *node, out reflect.Value) (newout reflect.Value, unmarshaled, good bool) {
  233. if n.tag == yaml_NULL_TAG || n.kind == scalarNode && n.tag == "" && (n.value == "null" || n.value == "~" || n.value == "" && n.implicit) {
  234. return out, false, false
  235. }
  236. again := true
  237. for again {
  238. again = false
  239. if out.Kind() == reflect.Ptr {
  240. if out.IsNil() {
  241. out.Set(reflect.New(out.Type().Elem()))
  242. }
  243. out = out.Elem()
  244. again = true
  245. }
  246. if out.CanAddr() {
  247. if u, ok := out.Addr().Interface().(Unmarshaler); ok {
  248. good = d.callUnmarshaler(n, u)
  249. return out, true, good
  250. }
  251. }
  252. }
  253. return out, false, false
  254. }
  255. func (d *decoder) unmarshal(n *node, out reflect.Value) (good bool) {
  256. switch n.kind {
  257. case documentNode:
  258. return d.document(n, out)
  259. case aliasNode:
  260. return d.alias(n, out)
  261. }
  262. out, unmarshaled, good := d.prepare(n, out)
  263. if unmarshaled {
  264. return good
  265. }
  266. switch n.kind {
  267. case scalarNode:
  268. good = d.scalar(n, out)
  269. case mappingNode:
  270. good = d.mapping(n, out)
  271. case sequenceNode:
  272. good = d.sequence(n, out)
  273. default:
  274. panic("internal error: unknown node kind: " + strconv.Itoa(n.kind))
  275. }
  276. return good
  277. }
  278. func (d *decoder) document(n *node, out reflect.Value) (good bool) {
  279. if len(n.children) == 1 {
  280. d.doc = n
  281. d.unmarshal(n.children[0], out)
  282. return true
  283. }
  284. return false
  285. }
  286. func (d *decoder) alias(n *node, out reflect.Value) (good bool) {
  287. if d.aliases[n] {
  288. // TODO this could actually be allowed in some circumstances.
  289. failf("anchor '%s' value contains itself", n.value)
  290. }
  291. d.aliases[n] = true
  292. good = d.unmarshal(n.alias, out)
  293. delete(d.aliases, n)
  294. return good
  295. }
  296. var zeroValue reflect.Value
  297. func resetMap(out reflect.Value) {
  298. for _, k := range out.MapKeys() {
  299. out.SetMapIndex(k, zeroValue)
  300. }
  301. }
  302. func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
  303. var tag string
  304. var resolved interface{}
  305. if n.tag == "" && !n.implicit {
  306. tag = yaml_STR_TAG
  307. resolved = n.value
  308. } else {
  309. tag, resolved = resolve(n.tag, n.value)
  310. if tag == yaml_BINARY_TAG {
  311. data, err := base64.StdEncoding.DecodeString(resolved.(string))
  312. if err != nil {
  313. failf("!!binary value contains invalid base64 data")
  314. }
  315. resolved = string(data)
  316. }
  317. }
  318. if resolved == nil {
  319. if out.Kind() == reflect.Map && !out.CanAddr() {
  320. resetMap(out)
  321. } else {
  322. out.Set(reflect.Zero(out.Type()))
  323. }
  324. return true
  325. }
  326. if s, ok := resolved.(string); ok && out.CanAddr() {
  327. if u, ok := out.Addr().Interface().(encoding.TextUnmarshaler); ok {
  328. err := u.UnmarshalText([]byte(s))
  329. if err != nil {
  330. fail(err)
  331. }
  332. return true
  333. }
  334. }
  335. switch out.Kind() {
  336. case reflect.String:
  337. if tag == yaml_BINARY_TAG {
  338. out.SetString(resolved.(string))
  339. good = true
  340. } else if resolved != nil {
  341. out.SetString(n.value)
  342. good = true
  343. }
  344. case reflect.Interface:
  345. if resolved == nil {
  346. out.Set(reflect.Zero(out.Type()))
  347. } else {
  348. out.Set(reflect.ValueOf(resolved))
  349. }
  350. good = true
  351. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  352. switch resolved := resolved.(type) {
  353. case int:
  354. if !out.OverflowInt(int64(resolved)) {
  355. out.SetInt(int64(resolved))
  356. good = true
  357. }
  358. case int64:
  359. if !out.OverflowInt(resolved) {
  360. out.SetInt(resolved)
  361. good = true
  362. }
  363. case uint64:
  364. if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
  365. out.SetInt(int64(resolved))
  366. good = true
  367. }
  368. case float64:
  369. if resolved <= math.MaxInt64 && !out.OverflowInt(int64(resolved)) {
  370. out.SetInt(int64(resolved))
  371. good = true
  372. }
  373. case string:
  374. if out.Type() == durationType {
  375. d, err := time.ParseDuration(resolved)
  376. if err == nil {
  377. out.SetInt(int64(d))
  378. good = true
  379. }
  380. }
  381. }
  382. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  383. switch resolved := resolved.(type) {
  384. case int:
  385. if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
  386. out.SetUint(uint64(resolved))
  387. good = true
  388. }
  389. case int64:
  390. if resolved >= 0 && !out.OverflowUint(uint64(resolved)) {
  391. out.SetUint(uint64(resolved))
  392. good = true
  393. }
  394. case uint64:
  395. if !out.OverflowUint(uint64(resolved)) {
  396. out.SetUint(uint64(resolved))
  397. good = true
  398. }
  399. case float64:
  400. if resolved <= math.MaxUint64 && !out.OverflowUint(uint64(resolved)) {
  401. out.SetUint(uint64(resolved))
  402. good = true
  403. }
  404. }
  405. case reflect.Bool:
  406. switch resolved := resolved.(type) {
  407. case bool:
  408. out.SetBool(resolved)
  409. good = true
  410. }
  411. case reflect.Float32, reflect.Float64:
  412. switch resolved := resolved.(type) {
  413. case int:
  414. out.SetFloat(float64(resolved))
  415. good = true
  416. case int64:
  417. out.SetFloat(float64(resolved))
  418. good = true
  419. case uint64:
  420. out.SetFloat(float64(resolved))
  421. good = true
  422. case float64:
  423. out.SetFloat(resolved)
  424. good = true
  425. }
  426. case reflect.Ptr:
  427. if out.Type().Elem() == reflect.TypeOf(resolved) {
  428. // TODO DOes this make sense? When is out a Ptr except when decoding a nil value?
  429. elem := reflect.New(out.Type().Elem())
  430. elem.Elem().Set(reflect.ValueOf(resolved))
  431. out.Set(elem)
  432. good = true
  433. }
  434. }
  435. if !good {
  436. d.terror(n, tag, out)
  437. }
  438. return good
  439. }
  440. func settableValueOf(i interface{}) reflect.Value {
  441. v := reflect.ValueOf(i)
  442. sv := reflect.New(v.Type()).Elem()
  443. sv.Set(v)
  444. return sv
  445. }
  446. func (d *decoder) sequence(n *node, out reflect.Value) (good bool) {
  447. l := len(n.children)
  448. var iface reflect.Value
  449. switch out.Kind() {
  450. case reflect.Slice:
  451. out.Set(reflect.MakeSlice(out.Type(), l, l))
  452. case reflect.Interface:
  453. // No type hints. Will have to use a generic sequence.
  454. iface = out
  455. out = settableValueOf(make([]interface{}, l))
  456. default:
  457. d.terror(n, yaml_SEQ_TAG, out)
  458. return false
  459. }
  460. et := out.Type().Elem()
  461. j := 0
  462. for i := 0; i < l; i++ {
  463. e := reflect.New(et).Elem()
  464. if ok := d.unmarshal(n.children[i], e); ok {
  465. out.Index(j).Set(e)
  466. j++
  467. }
  468. }
  469. out.Set(out.Slice(0, j))
  470. if iface.IsValid() {
  471. iface.Set(out)
  472. }
  473. return true
  474. }
  475. func (d *decoder) mapping(n *node, out reflect.Value) (good bool) {
  476. switch out.Kind() {
  477. case reflect.Struct:
  478. return d.mappingStruct(n, out)
  479. case reflect.Slice:
  480. return d.mappingSlice(n, out)
  481. case reflect.Map:
  482. // okay
  483. case reflect.Interface:
  484. if d.mapType.Kind() == reflect.Map {
  485. iface := out
  486. out = reflect.MakeMap(d.mapType)
  487. iface.Set(out)
  488. } else {
  489. slicev := reflect.New(d.mapType).Elem()
  490. if !d.mappingSlice(n, slicev) {
  491. return false
  492. }
  493. out.Set(slicev)
  494. return true
  495. }
  496. default:
  497. d.terror(n, yaml_MAP_TAG, out)
  498. return false
  499. }
  500. outt := out.Type()
  501. kt := outt.Key()
  502. et := outt.Elem()
  503. mapType := d.mapType
  504. if outt.Key() == ifaceType && outt.Elem() == ifaceType {
  505. d.mapType = outt
  506. }
  507. if out.IsNil() {
  508. out.Set(reflect.MakeMap(outt))
  509. }
  510. l := len(n.children)
  511. for i := 0; i < l; i += 2 {
  512. if isMerge(n.children[i]) {
  513. d.merge(n.children[i+1], out)
  514. continue
  515. }
  516. k := reflect.New(kt).Elem()
  517. if d.unmarshal(n.children[i], k) {
  518. kkind := k.Kind()
  519. if kkind == reflect.Interface {
  520. kkind = k.Elem().Kind()
  521. }
  522. if kkind == reflect.Map || kkind == reflect.Slice {
  523. failf("invalid map key: %#v", k.Interface())
  524. }
  525. e := reflect.New(et).Elem()
  526. if d.unmarshal(n.children[i+1], e) {
  527. out.SetMapIndex(k, e)
  528. }
  529. }
  530. }
  531. d.mapType = mapType
  532. return true
  533. }
  534. func (d *decoder) mappingSlice(n *node, out reflect.Value) (good bool) {
  535. outt := out.Type()
  536. if outt.Elem() != mapItemType {
  537. d.terror(n, yaml_MAP_TAG, out)
  538. return false
  539. }
  540. mapType := d.mapType
  541. d.mapType = outt
  542. var slice []MapItem
  543. var l = len(n.children)
  544. for i := 0; i < l; i += 2 {
  545. if isMerge(n.children[i]) {
  546. d.merge(n.children[i+1], out)
  547. continue
  548. }
  549. item := MapItem{}
  550. k := reflect.ValueOf(&item.Key).Elem()
  551. if d.unmarshal(n.children[i], k) {
  552. v := reflect.ValueOf(&item.Value).Elem()
  553. if d.unmarshal(n.children[i+1], v) {
  554. slice = append(slice, item)
  555. }
  556. }
  557. }
  558. out.Set(reflect.ValueOf(slice))
  559. d.mapType = mapType
  560. return true
  561. }
  562. func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) {
  563. sinfo, err := getStructInfo(out.Type())
  564. if err != nil {
  565. panic(err)
  566. }
  567. name := settableValueOf("")
  568. l := len(n.children)
  569. var inlineMap reflect.Value
  570. var elemType reflect.Type
  571. if sinfo.InlineMap != -1 {
  572. inlineMap = out.Field(sinfo.InlineMap)
  573. inlineMap.Set(reflect.New(inlineMap.Type()).Elem())
  574. elemType = inlineMap.Type().Elem()
  575. }
  576. for i := 0; i < l; i += 2 {
  577. ni := n.children[i]
  578. if isMerge(ni) {
  579. d.merge(n.children[i+1], out)
  580. continue
  581. }
  582. if !d.unmarshal(ni, name) {
  583. continue
  584. }
  585. if info, ok := sinfo.FieldsMap[name.String()]; ok {
  586. var field reflect.Value
  587. if info.Inline == nil {
  588. field = out.Field(info.Num)
  589. } else {
  590. field = out.FieldByIndex(info.Inline)
  591. }
  592. d.unmarshal(n.children[i+1], field)
  593. } else if sinfo.InlineMap != -1 {
  594. if inlineMap.IsNil() {
  595. inlineMap.Set(reflect.MakeMap(inlineMap.Type()))
  596. }
  597. value := reflect.New(elemType).Elem()
  598. d.unmarshal(n.children[i+1], value)
  599. inlineMap.SetMapIndex(name, value)
  600. } else if d.strict {
  601. d.terrors = append(d.terrors, fmt.Sprintf("line %d: field %s not found in struct %s", ni.line+1, name.String(), out.Type()))
  602. }
  603. }
  604. return true
  605. }
  606. func failWantMap() {
  607. failf("map merge requires map or sequence of maps as the value")
  608. }
  609. func (d *decoder) merge(n *node, out reflect.Value) {
  610. switch n.kind {
  611. case mappingNode:
  612. d.unmarshal(n, out)
  613. case aliasNode:
  614. an, ok := d.doc.anchors[n.value]
  615. if ok && an.kind != mappingNode {
  616. failWantMap()
  617. }
  618. d.unmarshal(n, out)
  619. case sequenceNode:
  620. // Step backwards as earlier nodes take precedence.
  621. for i := len(n.children) - 1; i >= 0; i-- {
  622. ni := n.children[i]
  623. if ni.kind == aliasNode {
  624. an, ok := d.doc.anchors[ni.value]
  625. if ok && an.kind != mappingNode {
  626. failWantMap()
  627. }
  628. } else if ni.kind != mappingNode {
  629. failWantMap()
  630. }
  631. d.unmarshal(ni, out)
  632. }
  633. default:
  634. failWantMap()
  635. }
  636. }
  637. func isMerge(n *node) bool {
  638. return n.kind == scalarNode && n.value == "<<" && (n.implicit == true || n.tag == yaml_MERGE_TAG)
  639. }