decode.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. package yaml
  2. import (
  3. "reflect"
  4. "strconv"
  5. )
  6. const (
  7. documentNode = 1 << iota
  8. mappingNode
  9. sequenceNode
  10. scalarNode
  11. aliasNode
  12. )
  13. type node struct {
  14. kind int
  15. line, column int
  16. tag string
  17. value string
  18. implicit bool
  19. children []*node
  20. anchors map[string]*node
  21. }
  22. // ----------------------------------------------------------------------------
  23. // Parser, produces a node tree out of a libyaml event stream.
  24. type parser struct {
  25. parser yaml_parser_t
  26. event yaml_event_t
  27. doc *node
  28. }
  29. func newParser(b []byte) *parser {
  30. p := parser{}
  31. if !yaml_parser_initialize(&p.parser) {
  32. panic("Failed to initialize YAML emitter")
  33. }
  34. if len(b) == 0 {
  35. b = []byte{'\n'}
  36. }
  37. yaml_parser_set_input_string(&p.parser, b)
  38. p.skip()
  39. if p.event.typ != yaml_STREAM_START_EVENT {
  40. panic("Expected stream start event, got " + strconv.Itoa(int(p.event.typ)))
  41. }
  42. p.skip()
  43. return &p
  44. }
  45. func (p *parser) destroy() {
  46. if p.event.typ != yaml_NO_EVENT {
  47. yaml_event_delete(&p.event)
  48. }
  49. yaml_parser_delete(&p.parser)
  50. }
  51. func (p *parser) skip() {
  52. if p.event.typ != yaml_NO_EVENT {
  53. if p.event.typ == yaml_STREAM_END_EVENT {
  54. panic("Attempted to go past the end of stream. Corrupted value?")
  55. }
  56. yaml_event_delete(&p.event)
  57. }
  58. if !yaml_parser_parse(&p.parser, &p.event) {
  59. p.fail()
  60. }
  61. }
  62. func (p *parser) fail() {
  63. var where string
  64. var line int
  65. if p.parser.problem_mark.line != 0 {
  66. line = p.parser.problem_mark.line
  67. } else if p.parser.context_mark.line != 0 {
  68. line = p.parser.context_mark.line
  69. }
  70. if line != 0 {
  71. where = "line " + strconv.Itoa(line) + ": "
  72. }
  73. var msg string
  74. if len(p.parser.problem) > 0 {
  75. msg = p.parser.problem
  76. } else {
  77. msg = "Unknown problem parsing YAML content"
  78. }
  79. panic(where + msg)
  80. }
  81. func (p *parser) anchor(n *node, anchor []byte) {
  82. if anchor != nil {
  83. p.doc.anchors[string(anchor)] = n
  84. }
  85. }
  86. func (p *parser) parse() *node {
  87. switch p.event.typ {
  88. case yaml_SCALAR_EVENT:
  89. return p.scalar()
  90. case yaml_ALIAS_EVENT:
  91. return p.alias()
  92. case yaml_MAPPING_START_EVENT:
  93. return p.mapping()
  94. case yaml_SEQUENCE_START_EVENT:
  95. return p.sequence()
  96. case yaml_DOCUMENT_START_EVENT:
  97. return p.document()
  98. case yaml_STREAM_END_EVENT:
  99. // Happens when attempting to decode an empty buffer.
  100. return nil
  101. default:
  102. panic("Attempted to parse unknown event: " +
  103. strconv.Itoa(int(p.event.typ)))
  104. }
  105. panic("Unreachable")
  106. }
  107. func (p *parser) node(kind int) *node {
  108. return &node{
  109. kind: kind,
  110. line: p.event.start_mark.line,
  111. column: p.event.start_mark.column,
  112. }
  113. }
  114. func (p *parser) document() *node {
  115. n := p.node(documentNode)
  116. n.anchors = make(map[string]*node)
  117. p.doc = n
  118. p.skip()
  119. n.children = append(n.children, p.parse())
  120. if p.event.typ != yaml_DOCUMENT_END_EVENT {
  121. panic("Expected end of document event but got " +
  122. strconv.Itoa(int(p.event.typ)))
  123. }
  124. p.skip()
  125. return n
  126. }
  127. func (p *parser) alias() *node {
  128. n := p.node(aliasNode)
  129. n.value = string(p.event.anchor)
  130. p.skip()
  131. return n
  132. }
  133. func (p *parser) scalar() *node {
  134. n := p.node(scalarNode)
  135. n.value = string(p.event.value)
  136. n.tag = string(p.event.tag)
  137. n.implicit = p.event.implicit
  138. p.anchor(n, p.event.anchor)
  139. p.skip()
  140. return n
  141. }
  142. func (p *parser) sequence() *node {
  143. n := p.node(sequenceNode)
  144. p.anchor(n, p.event.anchor)
  145. p.skip()
  146. for p.event.typ != yaml_SEQUENCE_END_EVENT {
  147. n.children = append(n.children, p.parse())
  148. }
  149. p.skip()
  150. return n
  151. }
  152. func (p *parser) mapping() *node {
  153. n := p.node(mappingNode)
  154. p.anchor(n, p.event.anchor)
  155. p.skip()
  156. for p.event.typ != yaml_MAPPING_END_EVENT {
  157. n.children = append(n.children, p.parse(), p.parse())
  158. }
  159. p.skip()
  160. return n
  161. }
  162. // ----------------------------------------------------------------------------
  163. // Decoder, unmarshals a node into a provided value.
  164. type decoder struct {
  165. doc *node
  166. aliases map[string]bool
  167. }
  168. func newDecoder() *decoder {
  169. d := &decoder{}
  170. d.aliases = make(map[string]bool)
  171. return d
  172. }
  173. // d.setter deals with setters and pointer dereferencing and initialization.
  174. //
  175. // It's a slightly convoluted case to handle properly:
  176. //
  177. // - nil pointers should be initialized, unless being set to nil
  178. // - we don't know at this point yet what's the value to SetYAML() with.
  179. // - we can't separate pointer deref/init and setter checking, because
  180. // a setter may be found while going down a pointer chain.
  181. //
  182. // Thus, here is how it takes care of it:
  183. //
  184. // - out is provided as a pointer, so that it can be replaced.
  185. // - when looking at a non-setter ptr, *out=ptr.Elem(), unless tag=!!null
  186. // - when a setter is found, *out=interface{}, and a set() function is
  187. // returned to call SetYAML() with the value of *out once it's defined.
  188. //
  189. func (d *decoder) setter(tag string, out *reflect.Value, good *bool) (set func()) {
  190. again := true
  191. for again {
  192. again = false
  193. setter, _ := (*out).Interface().(Setter)
  194. if tag != "!!null" || setter != nil {
  195. if pv := (*out); pv.Kind() == reflect.Ptr {
  196. if pv.IsNil() {
  197. *out = reflect.New(pv.Type().Elem()).Elem()
  198. pv.Set((*out).Addr())
  199. } else {
  200. *out = pv.Elem()
  201. }
  202. setter, _ = pv.Interface().(Setter)
  203. again = true
  204. }
  205. }
  206. if setter != nil {
  207. var arg interface{}
  208. *out = reflect.ValueOf(&arg).Elem()
  209. return func() {
  210. *good = setter.SetYAML(tag, arg)
  211. }
  212. }
  213. }
  214. return nil
  215. }
  216. func (d *decoder) unmarshal(n *node, out reflect.Value) (good bool) {
  217. switch n.kind {
  218. case documentNode:
  219. good = d.document(n, out)
  220. case scalarNode:
  221. good = d.scalar(n, out)
  222. case aliasNode:
  223. good = d.alias(n, out)
  224. case mappingNode:
  225. good = d.mapping(n, out)
  226. case sequenceNode:
  227. good = d.sequence(n, out)
  228. default:
  229. panic("Internal error: unknown node kind: " + strconv.Itoa(n.kind))
  230. }
  231. return
  232. }
  233. func (d *decoder) document(n *node, out reflect.Value) (good bool) {
  234. if len(n.children) == 1 {
  235. d.doc = n
  236. d.unmarshal(n.children[0], out)
  237. return true
  238. }
  239. return false
  240. }
  241. func (d *decoder) alias(n *node, out reflect.Value) (good bool) {
  242. an, ok := d.doc.anchors[n.value]
  243. if !ok {
  244. panic("Unknown anchor '" + n.value + "' referenced")
  245. }
  246. if d.aliases[n.value] {
  247. panic("Anchor '" + n.value + "' value contains itself")
  248. }
  249. d.aliases[n.value] = true
  250. good = d.unmarshal(an, out)
  251. delete(d.aliases, n.value)
  252. return good
  253. }
  254. func (d *decoder) scalar(n *node, out reflect.Value) (good bool) {
  255. var tag string
  256. var resolved interface{}
  257. if n.tag == "" && !n.implicit {
  258. tag = "!!str"
  259. resolved = n.value
  260. } else {
  261. tag, resolved = resolve(n.tag, n.value)
  262. }
  263. if set := d.setter(tag, &out, &good); set != nil {
  264. defer set()
  265. }
  266. switch out.Kind() {
  267. case reflect.String:
  268. if resolved != nil {
  269. out.SetString(n.value)
  270. good = true
  271. }
  272. case reflect.Interface:
  273. if resolved == nil {
  274. out.Set(reflect.Zero(out.Type()))
  275. } else {
  276. out.Set(reflect.ValueOf(resolved))
  277. }
  278. good = true
  279. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  280. switch resolved := resolved.(type) {
  281. case int:
  282. if !out.OverflowInt(int64(resolved)) {
  283. out.SetInt(int64(resolved))
  284. good = true
  285. }
  286. case int64:
  287. if !out.OverflowInt(resolved) {
  288. out.SetInt(resolved)
  289. good = true
  290. }
  291. case float64:
  292. if resolved < 1<<63-1 && !out.OverflowInt(int64(resolved)) {
  293. out.SetInt(int64(resolved))
  294. good = true
  295. }
  296. }
  297. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  298. switch resolved := resolved.(type) {
  299. case int:
  300. if resolved >= 0 {
  301. out.SetUint(uint64(resolved))
  302. good = true
  303. }
  304. case int64:
  305. if resolved >= 0 {
  306. out.SetUint(uint64(resolved))
  307. good = true
  308. }
  309. case float64:
  310. if resolved < 1<<64-1 && !out.OverflowUint(uint64(resolved)) {
  311. out.SetUint(uint64(resolved))
  312. good = true
  313. }
  314. }
  315. case reflect.Bool:
  316. switch resolved := resolved.(type) {
  317. case bool:
  318. out.SetBool(resolved)
  319. good = true
  320. }
  321. case reflect.Float32, reflect.Float64:
  322. switch resolved := resolved.(type) {
  323. case int:
  324. out.SetFloat(float64(resolved))
  325. good = true
  326. case int64:
  327. out.SetFloat(float64(resolved))
  328. good = true
  329. case float64:
  330. out.SetFloat(resolved)
  331. good = true
  332. }
  333. case reflect.Ptr:
  334. switch resolved.(type) {
  335. case nil:
  336. out.Set(reflect.Zero(out.Type()))
  337. good = true
  338. default:
  339. if out.Type().Elem() == reflect.TypeOf(resolved) {
  340. elem := reflect.New(out.Type().Elem())
  341. elem.Elem().Set(reflect.ValueOf(resolved))
  342. out.Set(elem)
  343. good = true
  344. }
  345. }
  346. }
  347. return good
  348. }
  349. func settableValueOf(i interface{}) reflect.Value {
  350. v := reflect.ValueOf(i)
  351. sv := reflect.New(v.Type()).Elem()
  352. sv.Set(v)
  353. return sv
  354. }
  355. func (d *decoder) sequence(n *node, out reflect.Value) (good bool) {
  356. if set := d.setter("!!seq", &out, &good); set != nil {
  357. defer set()
  358. }
  359. var iface reflect.Value
  360. if out.Kind() == reflect.Interface {
  361. // No type hints. Will have to use a generic sequence.
  362. iface = out
  363. out = settableValueOf(make([]interface{}, 0))
  364. }
  365. if out.Kind() != reflect.Slice {
  366. return false
  367. }
  368. et := out.Type().Elem()
  369. l := len(n.children)
  370. for i := 0; i < l; i++ {
  371. e := reflect.New(et).Elem()
  372. if ok := d.unmarshal(n.children[i], e); ok {
  373. out.Set(reflect.Append(out, e))
  374. }
  375. }
  376. if iface.IsValid() {
  377. iface.Set(out)
  378. }
  379. return true
  380. }
  381. func (d *decoder) mapping(n *node, out reflect.Value) (good bool) {
  382. if set := d.setter("!!map", &out, &good); set != nil {
  383. defer set()
  384. }
  385. if out.Kind() == reflect.Struct {
  386. return d.mappingStruct(n, out)
  387. }
  388. if out.Kind() == reflect.Interface {
  389. // No type hints. Will have to use a generic map.
  390. iface := out
  391. out = settableValueOf(make(map[interface{}]interface{}))
  392. iface.Set(out)
  393. }
  394. if out.Kind() != reflect.Map {
  395. return false
  396. }
  397. outt := out.Type()
  398. kt := outt.Key()
  399. et := outt.Elem()
  400. if out.IsNil() {
  401. out.Set(reflect.MakeMap(outt))
  402. }
  403. l := len(n.children)
  404. for i := 0; i < l; i += 2 {
  405. if isMerge(n.children[i]) {
  406. d.merge(n.children[i+1], out)
  407. continue
  408. }
  409. k := reflect.New(kt).Elem()
  410. if d.unmarshal(n.children[i], k) {
  411. e := reflect.New(et).Elem()
  412. if d.unmarshal(n.children[i+1], e) {
  413. out.SetMapIndex(k, e)
  414. }
  415. }
  416. }
  417. return true
  418. }
  419. func (d *decoder) mappingStruct(n *node, out reflect.Value) (good bool) {
  420. sinfo, err := getStructInfo(out.Type())
  421. if err != nil {
  422. panic(err)
  423. }
  424. name := settableValueOf("")
  425. l := len(n.children)
  426. for i := 0; i < l; i += 2 {
  427. ni := n.children[i]
  428. if isMerge(ni) {
  429. d.merge(n.children[i+1], out)
  430. continue
  431. }
  432. if !d.unmarshal(ni, name) {
  433. continue
  434. }
  435. if info, ok := sinfo.FieldsMap[name.String()]; ok {
  436. var field reflect.Value
  437. if info.Inline == nil {
  438. field = out.Field(info.Num)
  439. } else {
  440. field = out.FieldByIndex(info.Inline)
  441. }
  442. d.unmarshal(n.children[i+1], field)
  443. }
  444. }
  445. return true
  446. }
  447. func (d *decoder) merge(n *node, out reflect.Value) {
  448. const wantMap = "map merge requires map or sequence of maps as the value"
  449. switch n.kind {
  450. case mappingNode:
  451. d.unmarshal(n, out)
  452. case aliasNode:
  453. an, ok := d.doc.anchors[n.value]
  454. if ok && an.kind != mappingNode {
  455. panic(wantMap)
  456. }
  457. d.unmarshal(n, out)
  458. case sequenceNode:
  459. // Step backwards as earlier nodes take precedence.
  460. for i := len(n.children)-1; i >= 0; i-- {
  461. ni := n.children[i]
  462. if ni.kind == aliasNode {
  463. an, ok := d.doc.anchors[ni.value]
  464. if ok && an.kind != mappingNode {
  465. panic(wantMap)
  466. }
  467. } else if ni.kind != mappingNode {
  468. panic(wantMap)
  469. }
  470. d.unmarshal(ni, out)
  471. }
  472. default:
  473. panic(wantMap)
  474. }
  475. }
  476. func isMerge(n *node) bool {
  477. return n.kind == scalarNode && n.value == "<<" && (n.implicit == true || n.tag == "!!merge" || n.tag == "tag:yaml.org,2002:merge")
  478. }