decode.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package candiedyaml
  13. import (
  14. "bytes"
  15. "errors"
  16. "fmt"
  17. "io"
  18. "reflect"
  19. "runtime"
  20. "strconv"
  21. "strings"
  22. )
  23. type Unmarshaler interface {
  24. UnmarshalYAML(tag string, value interface{}) error
  25. }
  26. // A Number represents a JSON number literal.
  27. type Number string
  28. // String returns the literal text of the number.
  29. func (n Number) String() string { return string(n) }
  30. // Float64 returns the number as a float64.
  31. func (n Number) Float64() (float64, error) {
  32. return strconv.ParseFloat(string(n), 64)
  33. }
  34. // Int64 returns the number as an int64.
  35. func (n Number) Int64() (int64, error) {
  36. return strconv.ParseInt(string(n), 10, 64)
  37. }
  38. type Decoder struct {
  39. parser yaml_parser_t
  40. event yaml_event_t
  41. replay_events []yaml_event_t
  42. useNumber bool
  43. anchors map[string][]yaml_event_t
  44. tracking_anchors [][]yaml_event_t
  45. }
  46. type ParserError struct {
  47. ErrorType YAML_error_type_t
  48. Context string
  49. ContextMark YAML_mark_t
  50. Problem string
  51. ProblemMark YAML_mark_t
  52. }
  53. func (e *ParserError) Error() string {
  54. return fmt.Sprintf("yaml: [%s] %s at line %d, column %d", e.Context, e.Problem, e.ProblemMark.line+1, e.ProblemMark.column+1)
  55. }
  56. type UnexpectedEventError struct {
  57. Value string
  58. EventType yaml_event_type_t
  59. At YAML_mark_t
  60. }
  61. func (e *UnexpectedEventError) Error() string {
  62. return fmt.Sprintf("yaml: Unexpect event [%d]: '%s' at line %d, column %d", e.EventType, e.Value, e.At.line+1, e.At.column+1)
  63. }
  64. func recovery(err *error) {
  65. if r := recover(); r != nil {
  66. if _, ok := r.(runtime.Error); ok {
  67. panic(r)
  68. }
  69. var tmpError error
  70. switch r := r.(type) {
  71. case error:
  72. tmpError = r
  73. case string:
  74. tmpError = errors.New(r)
  75. default:
  76. tmpError = errors.New("Unknown panic: " + reflect.ValueOf(r).String())
  77. }
  78. *err = tmpError
  79. }
  80. }
  81. func Unmarshal(data []byte, v interface{}) error {
  82. d := NewDecoder(bytes.NewBuffer(data))
  83. return d.Decode(v)
  84. }
  85. func NewDecoder(r io.Reader) *Decoder {
  86. d := &Decoder{
  87. anchors: make(map[string][]yaml_event_t),
  88. tracking_anchors: make([][]yaml_event_t, 1),
  89. }
  90. yaml_parser_initialize(&d.parser)
  91. yaml_parser_set_input_reader(&d.parser, r)
  92. return d
  93. }
  94. func (d *Decoder) Decode(v interface{}) (err error) {
  95. defer recovery(&err)
  96. rv := reflect.ValueOf(v)
  97. if rv.Kind() != reflect.Ptr || rv.IsNil() {
  98. return fmt.Errorf("Expected a pointer or nil but was a %s at %s", rv.String(), d.event.start_mark)
  99. }
  100. if d.event.event_type == yaml_NO_EVENT {
  101. d.nextEvent()
  102. if d.event.event_type != yaml_STREAM_START_EVENT {
  103. return errors.New("Invalid stream")
  104. }
  105. d.nextEvent()
  106. }
  107. d.document(rv)
  108. return nil
  109. }
  110. func (d *Decoder) UseNumber() { d.useNumber = true }
  111. func (d *Decoder) error(err error) {
  112. panic(err)
  113. }
  114. func (d *Decoder) nextEvent() {
  115. if d.event.event_type == yaml_STREAM_END_EVENT {
  116. d.error(errors.New("The stream is closed"))
  117. }
  118. if d.replay_events != nil {
  119. d.event = d.replay_events[0]
  120. if len(d.replay_events) == 1 {
  121. d.replay_events = nil
  122. } else {
  123. d.replay_events = d.replay_events[1:]
  124. }
  125. } else {
  126. if !yaml_parser_parse(&d.parser, &d.event) {
  127. yaml_event_delete(&d.event)
  128. d.error(&ParserError{
  129. ErrorType: d.parser.error,
  130. Context: d.parser.context,
  131. ContextMark: d.parser.context_mark,
  132. Problem: d.parser.problem,
  133. ProblemMark: d.parser.problem_mark,
  134. })
  135. }
  136. }
  137. last := len(d.tracking_anchors)
  138. // skip aliases when tracking an anchor
  139. if last > 0 && d.event.event_type != yaml_ALIAS_EVENT {
  140. d.tracking_anchors[last-1] = append(d.tracking_anchors[last-1], d.event)
  141. }
  142. }
  143. func (d *Decoder) document(rv reflect.Value) {
  144. if d.event.event_type != yaml_DOCUMENT_START_EVENT {
  145. d.error(fmt.Errorf("Expected document start at %s", d.event.start_mark))
  146. }
  147. d.nextEvent()
  148. d.parse(rv)
  149. if d.event.event_type != yaml_DOCUMENT_END_EVENT {
  150. d.error(fmt.Errorf("Expected document end at %s", d.event.start_mark))
  151. }
  152. d.nextEvent()
  153. }
  154. func (d *Decoder) parse(rv reflect.Value) {
  155. if !rv.IsValid() {
  156. // skip ahead since we cannot store
  157. d.valueInterface()
  158. return
  159. }
  160. anchor := string(d.event.anchor)
  161. switch d.event.event_type {
  162. case yaml_SEQUENCE_START_EVENT:
  163. d.begin_anchor(anchor)
  164. d.sequence(rv)
  165. d.end_anchor(anchor)
  166. case yaml_MAPPING_START_EVENT:
  167. d.begin_anchor(anchor)
  168. d.mapping(rv)
  169. d.end_anchor(anchor)
  170. case yaml_SCALAR_EVENT:
  171. d.begin_anchor(anchor)
  172. d.scalar(rv)
  173. d.end_anchor(anchor)
  174. case yaml_ALIAS_EVENT:
  175. d.alias(rv)
  176. case yaml_DOCUMENT_END_EVENT:
  177. default:
  178. d.error(&UnexpectedEventError{
  179. Value: string(d.event.value),
  180. EventType: d.event.event_type,
  181. At: d.event.start_mark,
  182. })
  183. }
  184. }
  185. func (d *Decoder) begin_anchor(anchor string) {
  186. if anchor != "" {
  187. events := []yaml_event_t{d.event}
  188. d.tracking_anchors = append(d.tracking_anchors, events)
  189. }
  190. }
  191. func (d *Decoder) end_anchor(anchor string) {
  192. if anchor != "" {
  193. events := d.tracking_anchors[len(d.tracking_anchors)-1]
  194. d.tracking_anchors = d.tracking_anchors[0 : len(d.tracking_anchors)-1]
  195. // remove the anchor, replaying events shouldn't have anchors
  196. events[0].anchor = nil
  197. // we went one too many, remove the extra event
  198. events = events[:len(events)-1]
  199. // if nested, append to all the other anchors
  200. for i, e := range d.tracking_anchors {
  201. d.tracking_anchors[i] = append(e, events...)
  202. }
  203. d.anchors[anchor] = events
  204. }
  205. }
  206. func (d *Decoder) indirect(v reflect.Value, decodingNull bool) (Unmarshaler, reflect.Value) {
  207. // If v is a named type and is addressable,
  208. // start with its address, so that if the type has pointer methods,
  209. // we find them.
  210. if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
  211. v = v.Addr()
  212. }
  213. for {
  214. // Load value from interface, but only if the result will be
  215. // usefully addressable.
  216. if v.Kind() == reflect.Interface && !v.IsNil() {
  217. e := v.Elem()
  218. if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
  219. v = e
  220. continue
  221. }
  222. }
  223. if v.Kind() != reflect.Ptr {
  224. break
  225. }
  226. if v.Elem().Kind() != reflect.Ptr && decodingNull && v.CanSet() {
  227. break
  228. }
  229. if v.IsNil() {
  230. v.Set(reflect.New(v.Type().Elem()))
  231. }
  232. if v.Type().NumMethod() > 0 {
  233. if u, ok := v.Interface().(Unmarshaler); ok {
  234. var temp interface{}
  235. return u, reflect.ValueOf(&temp)
  236. }
  237. }
  238. v = v.Elem()
  239. }
  240. return nil, v
  241. }
  242. func (d *Decoder) sequence(v reflect.Value) {
  243. if d.event.event_type != yaml_SEQUENCE_START_EVENT {
  244. d.error(fmt.Errorf("Expected sequence start at %s", d.event.start_mark))
  245. }
  246. u, pv := d.indirect(v, false)
  247. if u != nil {
  248. defer func() {
  249. if err := u.UnmarshalYAML(yaml_SEQ_TAG, pv.Interface()); err != nil {
  250. d.error(err)
  251. }
  252. }()
  253. _, pv = d.indirect(pv, false)
  254. }
  255. v = pv
  256. // Check type of target.
  257. switch v.Kind() {
  258. case reflect.Interface:
  259. if v.NumMethod() == 0 {
  260. // Decoding into nil interface? Switch to non-reflect code.
  261. v.Set(reflect.ValueOf(d.sequenceInterface()))
  262. return
  263. }
  264. // Otherwise it's invalid.
  265. fallthrough
  266. default:
  267. d.error(fmt.Errorf("Expected an array, slice or interface{} but was a %s at %s", v, d.event.start_mark))
  268. case reflect.Array:
  269. case reflect.Slice:
  270. break
  271. }
  272. d.nextEvent()
  273. i := 0
  274. done:
  275. for {
  276. switch d.event.event_type {
  277. case yaml_SEQUENCE_END_EVENT, yaml_DOCUMENT_END_EVENT:
  278. break done
  279. }
  280. // Get element of array, growing if necessary.
  281. if v.Kind() == reflect.Slice {
  282. // Grow slice if necessary
  283. if i >= v.Cap() {
  284. newcap := v.Cap() + v.Cap()/2
  285. if newcap < 4 {
  286. newcap = 4
  287. }
  288. newv := reflect.MakeSlice(v.Type(), v.Len(), newcap)
  289. reflect.Copy(newv, v)
  290. v.Set(newv)
  291. }
  292. if i >= v.Len() {
  293. v.SetLen(i + 1)
  294. }
  295. }
  296. if i < v.Len() {
  297. // Decode into element.
  298. d.parse(v.Index(i))
  299. } else {
  300. // Ran out of fixed array: skip.
  301. d.parse(reflect.Value{})
  302. }
  303. i++
  304. }
  305. if i < v.Len() {
  306. if v.Kind() == reflect.Array {
  307. // Array. Zero the rest.
  308. z := reflect.Zero(v.Type().Elem())
  309. for ; i < v.Len(); i++ {
  310. v.Index(i).Set(z)
  311. }
  312. } else {
  313. v.SetLen(i)
  314. }
  315. }
  316. if i == 0 && v.Kind() == reflect.Slice {
  317. v.Set(reflect.MakeSlice(v.Type(), 0, 0))
  318. }
  319. if d.event.event_type != yaml_DOCUMENT_END_EVENT {
  320. d.nextEvent()
  321. }
  322. }
  323. func (d *Decoder) mapping(v reflect.Value) {
  324. u, pv := d.indirect(v, false)
  325. if u != nil {
  326. defer func() {
  327. if err := u.UnmarshalYAML(yaml_MAP_TAG, pv.Interface()); err != nil {
  328. d.error(err)
  329. }
  330. }()
  331. _, pv = d.indirect(pv, false)
  332. }
  333. v = pv
  334. // Decoding into nil interface? Switch to non-reflect code.
  335. if v.Kind() == reflect.Interface && v.NumMethod() == 0 {
  336. v.Set(reflect.ValueOf(d.mappingInterface()))
  337. return
  338. }
  339. // Check type of target: struct or map[X]Y
  340. switch v.Kind() {
  341. case reflect.Struct:
  342. d.mappingStruct(v)
  343. return
  344. case reflect.Map:
  345. default:
  346. d.error(fmt.Errorf("Expected a struct or map but was a %s at %s ", v, d.event.start_mark))
  347. }
  348. mapt := v.Type()
  349. if v.IsNil() {
  350. v.Set(reflect.MakeMap(mapt))
  351. }
  352. d.nextEvent()
  353. keyt := mapt.Key()
  354. mapElemt := mapt.Elem()
  355. var mapElem reflect.Value
  356. done:
  357. for {
  358. switch d.event.event_type {
  359. case yaml_MAPPING_END_EVENT:
  360. break done
  361. case yaml_DOCUMENT_END_EVENT:
  362. return
  363. }
  364. key := reflect.New(keyt)
  365. d.parse(key.Elem())
  366. if !mapElem.IsValid() {
  367. mapElem = reflect.New(mapElemt).Elem()
  368. } else {
  369. mapElem.Set(reflect.Zero(mapElemt))
  370. }
  371. d.parse(mapElem)
  372. v.SetMapIndex(key.Elem(), mapElem)
  373. }
  374. d.nextEvent()
  375. }
  376. func (d *Decoder) mappingStruct(v reflect.Value) {
  377. structt := v.Type()
  378. fields := cachedTypeFields(structt)
  379. d.nextEvent()
  380. done:
  381. for {
  382. switch d.event.event_type {
  383. case yaml_MAPPING_END_EVENT:
  384. break done
  385. case yaml_DOCUMENT_END_EVENT:
  386. return
  387. }
  388. key := ""
  389. d.parse(reflect.ValueOf(&key))
  390. // Figure out field corresponding to key.
  391. var subv reflect.Value
  392. var f *field
  393. for i := range fields {
  394. ff := &fields[i]
  395. if ff.name == key {
  396. f = ff
  397. break
  398. }
  399. if f == nil && strings.EqualFold(ff.name, key) {
  400. f = ff
  401. }
  402. }
  403. if f != nil {
  404. subv = v
  405. for _, i := range f.index {
  406. if subv.Kind() == reflect.Ptr {
  407. if subv.IsNil() {
  408. subv.Set(reflect.New(subv.Type().Elem()))
  409. }
  410. subv = subv.Elem()
  411. }
  412. subv = subv.Field(i)
  413. }
  414. }
  415. d.parse(subv)
  416. }
  417. d.nextEvent()
  418. }
  419. func (d *Decoder) scalar(v reflect.Value) {
  420. val := string(d.event.value)
  421. wantptr := null_values[val]
  422. u, pv := d.indirect(v, wantptr)
  423. var tag string
  424. if u != nil {
  425. defer func() {
  426. if err := u.UnmarshalYAML(tag, pv.Interface()); err != nil {
  427. d.error(err)
  428. }
  429. }()
  430. _, pv = d.indirect(pv, wantptr)
  431. }
  432. v = pv
  433. var err error
  434. tag, err = resolve(d.event, v, d.useNumber)
  435. if err != nil {
  436. d.error(err)
  437. }
  438. d.nextEvent()
  439. }
  440. func (d *Decoder) alias(rv reflect.Value) {
  441. val, ok := d.anchors[string(d.event.anchor)]
  442. if !ok {
  443. d.error(fmt.Errorf("missing anchor: '%s' at %s", d.event.anchor, d.event.start_mark))
  444. }
  445. d.replay_events = val
  446. d.nextEvent()
  447. d.parse(rv)
  448. }
  449. func (d *Decoder) valueInterface() interface{} {
  450. var v interface{}
  451. anchor := string(d.event.anchor)
  452. switch d.event.event_type {
  453. case yaml_SEQUENCE_START_EVENT:
  454. d.begin_anchor(anchor)
  455. v = d.sequenceInterface()
  456. case yaml_MAPPING_START_EVENT:
  457. d.begin_anchor(anchor)
  458. v = d.mappingInterface()
  459. case yaml_SCALAR_EVENT:
  460. d.begin_anchor(anchor)
  461. v = d.scalarInterface()
  462. case yaml_ALIAS_EVENT:
  463. rv := reflect.ValueOf(&v)
  464. d.alias(rv)
  465. return v
  466. case yaml_DOCUMENT_END_EVENT:
  467. d.error(&UnexpectedEventError{
  468. Value: string(d.event.value),
  469. EventType: d.event.event_type,
  470. At: d.event.start_mark,
  471. })
  472. }
  473. d.end_anchor(anchor)
  474. return v
  475. }
  476. func (d *Decoder) scalarInterface() interface{} {
  477. _, v := resolveInterface(d.event, d.useNumber)
  478. d.nextEvent()
  479. return v
  480. }
  481. // arrayInterface is like array but returns []interface{}.
  482. func (d *Decoder) sequenceInterface() []interface{} {
  483. var v = make([]interface{}, 0)
  484. d.nextEvent()
  485. done:
  486. for {
  487. switch d.event.event_type {
  488. case yaml_SEQUENCE_END_EVENT, yaml_DOCUMENT_END_EVENT:
  489. break done
  490. }
  491. v = append(v, d.valueInterface())
  492. }
  493. if d.event.event_type != yaml_DOCUMENT_END_EVENT {
  494. d.nextEvent()
  495. }
  496. return v
  497. }
  498. // objectInterface is like object but returns map[string]interface{}.
  499. func (d *Decoder) mappingInterface() map[interface{}]interface{} {
  500. m := make(map[interface{}]interface{})
  501. d.nextEvent()
  502. done:
  503. for {
  504. switch d.event.event_type {
  505. case yaml_MAPPING_END_EVENT, yaml_DOCUMENT_END_EVENT:
  506. break done
  507. }
  508. key := d.valueInterface()
  509. // Read value.
  510. m[key] = d.valueInterface()
  511. }
  512. if d.event.event_type != yaml_DOCUMENT_END_EVENT {
  513. d.nextEvent()
  514. }
  515. return m
  516. }