decode.go 15 KB

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