decode.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. package toml
  2. import (
  3. "encoding"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "reflect"
  8. "strings"
  9. "time"
  10. )
  11. var e = fmt.Errorf
  12. // Primitive is a TOML value that hasn't been decoded into a Go value.
  13. // When using the various `Decode*` functions, the type `Primitive` may
  14. // be given to any value, and its decoding will be delayed.
  15. //
  16. // A `Primitive` value can be decoded using the `PrimitiveDecode` function.
  17. //
  18. // The underlying representation of a `Primitive` value is subject to change.
  19. // Do not rely on it.
  20. //
  21. // N.B. Primitive values are still parsed, so using them will only avoid
  22. // the overhead of reflection. They can be useful when you don't know the
  23. // exact type of TOML data until run time.
  24. type Primitive interface{}
  25. // PrimitiveDecode is just like the other `Decode*` functions, except it
  26. // decodes a TOML value that has already been parsed. Valid primitive values
  27. // can *only* be obtained from values filled by the decoder functions,
  28. // including `PrimitiveDecode`. (i.e., `v` may contain more `Primitive`
  29. // values.)
  30. //
  31. // Meta data for primitive values is included in the meta data returned by
  32. // the `Decode*` functions.
  33. func PrimitiveDecode(primValue Primitive, v interface{}) error {
  34. return unify(primValue, rvalue(v))
  35. }
  36. // Decode will decode the contents of `data` in TOML format into a pointer
  37. // `v`.
  38. //
  39. // TOML hashes correspond to Go structs or maps. (Dealer's choice. They can be
  40. // used interchangeably.)
  41. //
  42. // TOML arrays of tables correspond to either a slice of structs or a slice
  43. // of maps.
  44. //
  45. // TOML datetimes correspond to Go `time.Time` values.
  46. //
  47. // All other TOML types (float, string, int, bool and array) correspond
  48. // to the obvious Go types.
  49. //
  50. // An exception to the above rules is if a type implements the
  51. // encoding.TextUnmarshaler interface. In this case, any primitive TOML value
  52. // (floats, strings, integers, booleans and datetimes) will be converted to
  53. // a byte string and given to the value's UnmarshalText method. Here's an
  54. // example for parsing durations:
  55. //
  56. // type duration struct {
  57. // time.Duration
  58. // }
  59. //
  60. // func (d *duration) UnmarshalText(text []byte) error {
  61. // var err error
  62. // d.Duration, err = time.ParseDuration(string(text))
  63. // return err
  64. // }
  65. //
  66. // func ExampleUnmarshaler() {
  67. // blob := `
  68. // [[song]]
  69. // name = "Thunder Road"
  70. // duration = "4m49s"
  71. //
  72. // [[song]]
  73. // name = "Stairway to Heaven"
  74. // duration = "8m03s"
  75. // `
  76. // type song struct {
  77. // Name string
  78. // Duration duration
  79. // }
  80. // type songs struct {
  81. // Song []song
  82. // }
  83. // var favorites songs
  84. // if _, err := Decode(blob, &favorites); err != nil {
  85. // log.Fatal(err)
  86. // }
  87. //
  88. // for _, s := range favorites.Song {
  89. // fmt.Printf("%s (%s)\n", s.Name, s.Duration)
  90. // }
  91. // // Output:
  92. // // Thunder Road (4m49s)
  93. // // Stairway to Heaven (8m3s)
  94. // }
  95. //
  96. // Key mapping
  97. //
  98. // TOML keys can map to either keys in a Go map or field names in a Go
  99. // struct. The special `toml` struct tag may be used to map TOML keys to
  100. // struct fields that don't match the key name exactly. (See the example.)
  101. // A case insensitive match to struct names will be tried if an exact match
  102. // can't be found.
  103. //
  104. // The mapping between TOML values and Go values is loose. That is, there
  105. // may exist TOML values that cannot be placed into your representation, and
  106. // there may be parts of your representation that do not correspond to
  107. // TOML values.
  108. //
  109. // This decoder will not handle cyclic types. If a cyclic type is passed,
  110. // `Decode` will not terminate.
  111. func Decode(data string, v interface{}) (MetaData, error) {
  112. p, err := parse(data)
  113. if err != nil {
  114. return MetaData{}, err
  115. }
  116. return MetaData{p.mapping, p.types, p.ordered}, unify(p.mapping, rvalue(v))
  117. }
  118. // DecodeFile is just like Decode, except it will automatically read the
  119. // contents of the file at `fpath` and decode it for you.
  120. func DecodeFile(fpath string, v interface{}) (MetaData, error) {
  121. bs, err := ioutil.ReadFile(fpath)
  122. if err != nil {
  123. return MetaData{}, err
  124. }
  125. return Decode(string(bs), v)
  126. }
  127. // DecodeReader is just like Decode, except it will consume all bytes
  128. // from the reader and decode it for you.
  129. func DecodeReader(r io.Reader, v interface{}) (MetaData, error) {
  130. bs, err := ioutil.ReadAll(r)
  131. if err != nil {
  132. return MetaData{}, err
  133. }
  134. return Decode(string(bs), v)
  135. }
  136. // unify performs a sort of type unification based on the structure of `rv`,
  137. // which is the client representation.
  138. //
  139. // Any type mismatch produces an error. Finding a type that we don't know
  140. // how to handle produces an unsupported type error.
  141. func unify(data interface{}, rv reflect.Value) error {
  142. // Special case. Look for a `Primitive` value.
  143. if rv.Type() == reflect.TypeOf((*Primitive)(nil)).Elem() {
  144. return unifyAnything(data, rv)
  145. }
  146. // Special case. Look for a value satisfying the TextUnmarshaler interface.
  147. if v, ok := rv.Interface().(encoding.TextUnmarshaler); ok {
  148. return unifyText(data, v)
  149. }
  150. // BUG(burntsushi)
  151. // The behavior here is incorrect whenever a Go type satisfies the
  152. // encoding.TextUnmarshaler interface but also corresponds to a TOML
  153. // hash or array. In particular, the unmarshaler should only be applied
  154. // to primitive TOML values. But at this point, it will be applied to
  155. // all kinds of values and produce an incorrect error whenever those values
  156. // are hashes or arrays (including arrays of tables).
  157. k := rv.Kind()
  158. // laziness
  159. if k >= reflect.Int && k <= reflect.Uint64 {
  160. return unifyInt(data, rv)
  161. }
  162. switch k {
  163. case reflect.Ptr:
  164. elem := reflect.New(rv.Type().Elem())
  165. err := unify(data, reflect.Indirect(elem))
  166. if err != nil {
  167. return err
  168. }
  169. rv.Set(elem)
  170. return nil
  171. case reflect.Struct:
  172. return unifyStruct(data, rv)
  173. case reflect.Map:
  174. return unifyMap(data, rv)
  175. case reflect.Slice:
  176. return unifySlice(data, rv)
  177. case reflect.String:
  178. return unifyString(data, rv)
  179. case reflect.Bool:
  180. return unifyBool(data, rv)
  181. case reflect.Interface:
  182. // we only support empty interfaces.
  183. if rv.NumMethod() > 0 {
  184. return e("Unsupported type '%s'.", rv.Kind())
  185. }
  186. return unifyAnything(data, rv)
  187. case reflect.Float32:
  188. fallthrough
  189. case reflect.Float64:
  190. return unifyFloat64(data, rv)
  191. }
  192. return e("Unsupported type '%s'.", rv.Kind())
  193. }
  194. func unifyStruct(mapping interface{}, rv reflect.Value) error {
  195. tmap, ok := mapping.(map[string]interface{})
  196. if !ok {
  197. return mismatch(rv, "map", mapping)
  198. }
  199. for key, datum := range tmap {
  200. var f *field
  201. fields := cachedTypeFields(rv.Type())
  202. for i := range fields {
  203. ff := &fields[i]
  204. if ff.name == key {
  205. f = ff
  206. break
  207. }
  208. if f == nil && strings.EqualFold(ff.name, key) {
  209. f = ff
  210. }
  211. }
  212. if f != nil {
  213. subv := rv
  214. for _, i := range f.index {
  215. if subv.Kind() == reflect.Ptr {
  216. if subv.IsNil() {
  217. subv.Set(reflect.New(subv.Type().Elem()))
  218. }
  219. subv = subv.Elem()
  220. }
  221. subv = subv.Field(i)
  222. }
  223. sf := indirect(subv)
  224. if isUnifiable(sf) {
  225. if err := unify(datum, sf); err != nil {
  226. return e("Type mismatch for '%s.%s': %s",
  227. rv.Type().String(), f.name, err)
  228. }
  229. } else if f.name != "" {
  230. // Bad user! No soup for you!
  231. return e("Field '%s.%s' is unexported, and therefore cannot "+
  232. "be loaded with reflection.", rv.Type().String(), f.name)
  233. }
  234. }
  235. }
  236. return nil
  237. }
  238. func unifyMap(mapping interface{}, rv reflect.Value) error {
  239. tmap, ok := mapping.(map[string]interface{})
  240. if !ok {
  241. return badtype("map", mapping)
  242. }
  243. if rv.IsNil() {
  244. rv.Set(reflect.MakeMap(rv.Type()))
  245. }
  246. for k, v := range tmap {
  247. rvkey := indirect(reflect.New(rv.Type().Key()))
  248. rvval := reflect.Indirect(reflect.New(rv.Type().Elem()))
  249. if err := unify(v, rvval); err != nil {
  250. return err
  251. }
  252. rvkey.SetString(k)
  253. rv.SetMapIndex(rvkey, rvval)
  254. }
  255. return nil
  256. }
  257. func unifySlice(data interface{}, rv reflect.Value) error {
  258. datav := reflect.ValueOf(data)
  259. if datav.Kind() != reflect.Slice {
  260. return badtype("slice", data)
  261. }
  262. sliceLen := datav.Len()
  263. if rv.IsNil() {
  264. rv.Set(reflect.MakeSlice(rv.Type(), sliceLen, sliceLen))
  265. }
  266. for i := 0; i < sliceLen; i++ {
  267. v := datav.Index(i).Interface()
  268. sliceval := indirect(rv.Index(i))
  269. if err := unify(v, sliceval); err != nil {
  270. return err
  271. }
  272. }
  273. return nil
  274. }
  275. func unifyDatetime(data interface{}, rv reflect.Value) error {
  276. if _, ok := data.(time.Time); ok {
  277. rv.Set(reflect.ValueOf(data))
  278. return nil
  279. }
  280. return badtype("time.Time", data)
  281. }
  282. func unifyString(data interface{}, rv reflect.Value) error {
  283. if s, ok := data.(string); ok {
  284. rv.SetString(s)
  285. return nil
  286. }
  287. return badtype("string", data)
  288. }
  289. func unifyFloat64(data interface{}, rv reflect.Value) error {
  290. if num, ok := data.(float64); ok {
  291. switch rv.Kind() {
  292. case reflect.Float32:
  293. fallthrough
  294. case reflect.Float64:
  295. rv.SetFloat(num)
  296. default:
  297. panic("bug")
  298. }
  299. return nil
  300. }
  301. return badtype("float", data)
  302. }
  303. func unifyInt(data interface{}, rv reflect.Value) error {
  304. if num, ok := data.(int64); ok {
  305. switch rv.Kind() {
  306. case reflect.Int:
  307. fallthrough
  308. case reflect.Int8:
  309. fallthrough
  310. case reflect.Int16:
  311. fallthrough
  312. case reflect.Int32:
  313. fallthrough
  314. case reflect.Int64:
  315. rv.SetInt(int64(num))
  316. case reflect.Uint:
  317. fallthrough
  318. case reflect.Uint8:
  319. fallthrough
  320. case reflect.Uint16:
  321. fallthrough
  322. case reflect.Uint32:
  323. fallthrough
  324. case reflect.Uint64:
  325. rv.SetUint(uint64(num))
  326. default:
  327. panic("bug")
  328. }
  329. return nil
  330. }
  331. return badtype("integer", data)
  332. }
  333. func unifyBool(data interface{}, rv reflect.Value) error {
  334. if b, ok := data.(bool); ok {
  335. rv.SetBool(b)
  336. return nil
  337. }
  338. return badtype("boolean", data)
  339. }
  340. func unifyAnything(data interface{}, rv reflect.Value) error {
  341. // too awesome to fail
  342. rv.Set(reflect.ValueOf(data))
  343. return nil
  344. }
  345. func unifyText(data interface{}, v encoding.TextUnmarshaler) error {
  346. var s string
  347. switch sdata := data.(type) {
  348. case encoding.TextMarshaler:
  349. text, err := sdata.MarshalText()
  350. if err != nil {
  351. return err
  352. }
  353. s = string(text)
  354. case fmt.Stringer:
  355. s = sdata.String()
  356. case string:
  357. s = sdata
  358. case bool:
  359. s = fmt.Sprintf("%v", sdata)
  360. case int64:
  361. s = fmt.Sprintf("%d", sdata)
  362. case float64:
  363. s = fmt.Sprintf("%f", sdata)
  364. default:
  365. return badtype("primitive (string-like)", data)
  366. }
  367. if err := v.UnmarshalText([]byte(s)); err != nil {
  368. return err
  369. }
  370. return nil
  371. }
  372. // rvalue returns a reflect.Value of `v`. All pointers are resolved.
  373. func rvalue(v interface{}) reflect.Value {
  374. return indirect(reflect.ValueOf(v))
  375. }
  376. // indirect returns the value pointed to by a pointer.
  377. // Pointers are followed until the value is not a pointer.
  378. // New values are allocated for each nil pointer.
  379. //
  380. // An exception to this rule is if the value satisfies an interface of
  381. // interest to us (like encoding.TextUnmarshaler).
  382. func indirect(v reflect.Value) reflect.Value {
  383. if v.Kind() != reflect.Ptr {
  384. if v.CanAddr() {
  385. pv := v.Addr()
  386. if _, ok := pv.Interface().(encoding.TextUnmarshaler); ok {
  387. return pv
  388. }
  389. }
  390. return v
  391. }
  392. if v.IsNil() {
  393. v.Set(reflect.New(v.Type().Elem()))
  394. }
  395. return indirect(reflect.Indirect(v))
  396. }
  397. func isUnifiable(rv reflect.Value) bool {
  398. if rv.CanSet() {
  399. return true
  400. }
  401. if _, ok := rv.Interface().(encoding.TextUnmarshaler); ok {
  402. return true
  403. }
  404. return false
  405. }
  406. func tstring(rv reflect.Value) string {
  407. return rv.Type().String()
  408. }
  409. func badtype(expected string, data interface{}) error {
  410. return e("Expected %s but found '%T'.", expected, data)
  411. }
  412. func mismatch(user reflect.Value, expected string, data interface{}) error {
  413. return e("Type mismatch for %s. Expected %s but found '%T'.",
  414. tstring(user), expected, data)
  415. }
  416. func insensitiveGet(
  417. tmap map[string]interface{}, kname string) (interface{}, bool) {
  418. if datum, ok := tmap[kname]; ok {
  419. return datum, true
  420. }
  421. for k, v := range tmap {
  422. if strings.EqualFold(kname, k) {
  423. return v, true
  424. }
  425. }
  426. return nil, false
  427. }
  428. // MetaData allows access to meta information about TOML data that may not
  429. // be inferrable via reflection. In particular, whether a key has been defined
  430. // and the TOML type of a key.
  431. type MetaData struct {
  432. mapping map[string]interface{}
  433. types map[string]tomlType
  434. keys []Key
  435. }
  436. // IsDefined returns true if the key given exists in the TOML data. The key
  437. // should be specified hierarchially. e.g.,
  438. //
  439. // // access the TOML key 'a.b.c'
  440. // IsDefined("a", "b", "c")
  441. //
  442. // IsDefined will return false if an empty key given. Keys are case sensitive.
  443. func (md MetaData) IsDefined(key ...string) bool {
  444. var hashOrVal interface{}
  445. var hash map[string]interface{}
  446. var ok bool
  447. if len(key) == 0 {
  448. return false
  449. }
  450. hashOrVal = md.mapping
  451. for _, k := range key {
  452. if hash, ok = hashOrVal.(map[string]interface{}); !ok {
  453. return false
  454. }
  455. if hashOrVal, ok = hash[k]; !ok {
  456. return false
  457. }
  458. }
  459. return true
  460. }
  461. // Type returns a string representation of the type of the key specified.
  462. //
  463. // Type will return the empty string if given an empty key or a key that
  464. // does not exist. Keys are case sensitive.
  465. func (md MetaData) Type(key ...string) string {
  466. fullkey := strings.Join(key, ".")
  467. if typ, ok := md.types[fullkey]; ok {
  468. return typ.typeString()
  469. }
  470. return ""
  471. }
  472. // Key is the type of any TOML key, including key groups. Use (MetaData).Keys
  473. // to get values of this type.
  474. type Key []string
  475. func (k Key) String() string {
  476. return strings.Join(k, ".")
  477. }
  478. func (k Key) add(piece string) Key {
  479. newKey := make(Key, len(k))
  480. copy(newKey, k)
  481. return append(newKey, piece)
  482. }
  483. // Keys returns a slice of every key in the TOML data, including key groups.
  484. // Each key is itself a slice, where the first element is the top of the
  485. // hierarchy and the last is the most specific.
  486. //
  487. // The list will have the same order as the keys appeared in the TOML data.
  488. //
  489. // All keys returned are non-empty.
  490. func (md MetaData) Keys() []Key {
  491. return md.keys
  492. }
  493. func allKeys(m map[string]interface{}, context Key) []Key {
  494. keys := make([]Key, 0, len(m))
  495. for k, v := range m {
  496. keys = append(keys, context.add(k))
  497. if t, ok := v.(map[string]interface{}); ok {
  498. keys = append(keys, allKeys(t, context.add(k))...)
  499. }
  500. }
  501. return keys
  502. }