decode.go 11 KB

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