decode.go 14 KB

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