unmarshaler.go 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. package toml
  2. import (
  3. "encoding"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "math"
  9. "reflect"
  10. "strings"
  11. "sync/atomic"
  12. "time"
  13. "github.com/pelletier/go-toml/v2/internal/danger"
  14. "github.com/pelletier/go-toml/v2/internal/tracker"
  15. "github.com/pelletier/go-toml/v2/unstable"
  16. )
  17. // Unmarshal deserializes a TOML document into a Go value.
  18. //
  19. // It is a shortcut for Decoder.Decode() with the default options.
  20. func Unmarshal(data []byte, v interface{}) error {
  21. p := unstable.Parser{}
  22. p.Reset(data)
  23. d := decoder{p: &p}
  24. return d.FromParser(v)
  25. }
  26. // Decoder reads and decode a TOML document from an input stream.
  27. type Decoder struct {
  28. // input
  29. r io.Reader
  30. // global settings
  31. strict bool
  32. }
  33. // NewDecoder creates a new Decoder that will read from r.
  34. func NewDecoder(r io.Reader) *Decoder {
  35. return &Decoder{r: r}
  36. }
  37. // DisallowUnknownFields causes the Decoder to return an error when the
  38. // destination is a struct and the input contains a key that does not match a
  39. // non-ignored field.
  40. //
  41. // In that case, the Decoder returns a StrictMissingError that can be used to
  42. // retrieve the individual errors as well as generate a human readable
  43. // description of the missing fields.
  44. func (d *Decoder) DisallowUnknownFields() *Decoder {
  45. d.strict = true
  46. return d
  47. }
  48. // Decode the whole content of r into v.
  49. //
  50. // By default, values in the document that don't exist in the target Go value
  51. // are ignored. See Decoder.DisallowUnknownFields() to change this behavior.
  52. //
  53. // When a TOML local date, time, or date-time is decoded into a time.Time, its
  54. // value is represented in time.Local timezone. Otherwise the appropriate Local*
  55. // structure is used. For time values, precision up to the nanosecond is
  56. // supported by truncating extra digits.
  57. //
  58. // Empty tables decoded in an interface{} create an empty initialized
  59. // map[string]interface{}.
  60. //
  61. // Types implementing the encoding.TextUnmarshaler interface are decoded from a
  62. // TOML string.
  63. //
  64. // When decoding a number, go-toml will return an error if the number is out of
  65. // bounds for the target type (which includes negative numbers when decoding
  66. // into an unsigned int).
  67. //
  68. // If an error occurs while decoding the content of the document, this function
  69. // returns a toml.DecodeError, providing context about the issue. When using
  70. // strict mode and a field is missing, a `toml.StrictMissingError` is
  71. // returned. In any other case, this function returns a standard Go error.
  72. //
  73. // # Type mapping
  74. //
  75. // List of supported TOML types and their associated accepted Go types:
  76. //
  77. // String -> string
  78. // Integer -> uint*, int*, depending on size
  79. // Float -> float*, depending on size
  80. // Boolean -> bool
  81. // Offset Date-Time -> time.Time
  82. // Local Date-time -> LocalDateTime, time.Time
  83. // Local Date -> LocalDate, time.Time
  84. // Local Time -> LocalTime, time.Time
  85. // Array -> slice and array, depending on elements types
  86. // Table -> map and struct
  87. // Inline Table -> same as Table
  88. // Array of Tables -> same as Array and Table
  89. func (d *Decoder) Decode(v interface{}) error {
  90. b, err := ioutil.ReadAll(d.r)
  91. if err != nil {
  92. return fmt.Errorf("toml: %w", err)
  93. }
  94. p := unstable.Parser{}
  95. p.Reset(b)
  96. dec := decoder{
  97. p: &p,
  98. strict: strict{
  99. Enabled: d.strict,
  100. },
  101. }
  102. return dec.FromParser(v)
  103. }
  104. type decoder struct {
  105. // Which parser instance in use for this decoding session.
  106. p *unstable.Parser
  107. // Flag indicating that the current expression is stashed.
  108. // If set to true, calling nextExpr will not actually pull a new expression
  109. // but turn off the flag instead.
  110. stashedExpr bool
  111. // Skip expressions until a table is found. This is set to true when a
  112. // table could not be created (missing field in map), so all KV expressions
  113. // need to be skipped.
  114. skipUntilTable bool
  115. // Tracks position in Go arrays.
  116. // This is used when decoding [[array tables]] into Go arrays. Given array
  117. // tables are separate TOML expression, we need to keep track of where we
  118. // are at in the Go array, as we can't just introspect its size.
  119. arrayIndexes map[reflect.Value]int
  120. // Tracks keys that have been seen, with which type.
  121. seen tracker.SeenTracker
  122. // Strict mode
  123. strict strict
  124. // Current context for the error.
  125. errorContext *errorContext
  126. }
  127. type errorContext struct {
  128. Struct reflect.Type
  129. Field []int
  130. }
  131. func (d *decoder) typeMismatchError(toml string, target reflect.Type) error {
  132. return fmt.Errorf("toml: %s", d.typeMismatchString(toml, target))
  133. }
  134. func (d *decoder) typeMismatchString(toml string, target reflect.Type) string {
  135. if d.errorContext != nil && d.errorContext.Struct != nil {
  136. ctx := d.errorContext
  137. f := ctx.Struct.FieldByIndex(ctx.Field)
  138. return fmt.Sprintf("cannot decode TOML %s into struct field %s.%s of type %s", toml, ctx.Struct, f.Name, f.Type)
  139. }
  140. return fmt.Sprintf("cannot decode TOML %s into a Go value of type %s", toml, target)
  141. }
  142. func (d *decoder) expr() *unstable.Node {
  143. return d.p.Expression()
  144. }
  145. func (d *decoder) nextExpr() bool {
  146. if d.stashedExpr {
  147. d.stashedExpr = false
  148. return true
  149. }
  150. return d.p.NextExpression()
  151. }
  152. func (d *decoder) stashExpr() {
  153. d.stashedExpr = true
  154. }
  155. func (d *decoder) arrayIndex(shouldAppend bool, v reflect.Value) int {
  156. if d.arrayIndexes == nil {
  157. d.arrayIndexes = make(map[reflect.Value]int, 1)
  158. }
  159. idx, ok := d.arrayIndexes[v]
  160. if !ok {
  161. d.arrayIndexes[v] = 0
  162. } else if shouldAppend {
  163. idx++
  164. d.arrayIndexes[v] = idx
  165. }
  166. return idx
  167. }
  168. func (d *decoder) FromParser(v interface{}) error {
  169. r := reflect.ValueOf(v)
  170. if r.Kind() != reflect.Ptr {
  171. return fmt.Errorf("toml: decoding can only be performed into a pointer, not %s", r.Kind())
  172. }
  173. if r.IsNil() {
  174. return fmt.Errorf("toml: decoding pointer target cannot be nil")
  175. }
  176. r = r.Elem()
  177. if r.Kind() == reflect.Interface && r.IsNil() {
  178. newMap := map[string]interface{}{}
  179. r.Set(reflect.ValueOf(newMap))
  180. }
  181. err := d.fromParser(r)
  182. if err == nil {
  183. return d.strict.Error(d.p.Data())
  184. }
  185. var e *unstable.ParserError
  186. if errors.As(err, &e) {
  187. return wrapDecodeError(d.p.Data(), e)
  188. }
  189. return err
  190. }
  191. func (d *decoder) fromParser(root reflect.Value) error {
  192. for d.nextExpr() {
  193. err := d.handleRootExpression(d.expr(), root)
  194. if err != nil {
  195. return err
  196. }
  197. }
  198. return d.p.Error()
  199. }
  200. /*
  201. Rules for the unmarshal code:
  202. - The stack is used to keep track of which values need to be set where.
  203. - handle* functions <=> switch on a given unstable.Kind.
  204. - unmarshalX* functions need to unmarshal a node of kind X.
  205. - An "object" is either a struct or a map.
  206. */
  207. func (d *decoder) handleRootExpression(expr *unstable.Node, v reflect.Value) error {
  208. var x reflect.Value
  209. var err error
  210. if !(d.skipUntilTable && expr.Kind == unstable.KeyValue) {
  211. err = d.seen.CheckExpression(expr)
  212. if err != nil {
  213. return err
  214. }
  215. }
  216. switch expr.Kind {
  217. case unstable.KeyValue:
  218. if d.skipUntilTable {
  219. return nil
  220. }
  221. x, err = d.handleKeyValue(expr, v)
  222. case unstable.Table:
  223. d.skipUntilTable = false
  224. d.strict.EnterTable(expr)
  225. x, err = d.handleTable(expr.Key(), v)
  226. case unstable.ArrayTable:
  227. d.skipUntilTable = false
  228. d.strict.EnterArrayTable(expr)
  229. x, err = d.handleArrayTable(expr.Key(), v)
  230. default:
  231. panic(fmt.Errorf("parser should not permit expression of kind %s at document root", expr.Kind))
  232. }
  233. if d.skipUntilTable {
  234. if expr.Kind == unstable.Table || expr.Kind == unstable.ArrayTable {
  235. d.strict.MissingTable(expr)
  236. }
  237. } else if err == nil && x.IsValid() {
  238. v.Set(x)
  239. }
  240. return err
  241. }
  242. func (d *decoder) handleArrayTable(key unstable.Iterator, v reflect.Value) (reflect.Value, error) {
  243. if key.Next() {
  244. return d.handleArrayTablePart(key, v)
  245. }
  246. return d.handleKeyValues(v)
  247. }
  248. func (d *decoder) handleArrayTableCollectionLast(key unstable.Iterator, v reflect.Value) (reflect.Value, error) {
  249. switch v.Kind() {
  250. case reflect.Interface:
  251. elem := v.Elem()
  252. if !elem.IsValid() {
  253. elem = reflect.New(sliceInterfaceType).Elem()
  254. elem.Set(reflect.MakeSlice(sliceInterfaceType, 0, 16))
  255. } else if elem.Kind() == reflect.Slice {
  256. if elem.Type() != sliceInterfaceType {
  257. elem = reflect.New(sliceInterfaceType).Elem()
  258. elem.Set(reflect.MakeSlice(sliceInterfaceType, 0, 16))
  259. } else if !elem.CanSet() {
  260. nelem := reflect.New(sliceInterfaceType).Elem()
  261. nelem.Set(reflect.MakeSlice(sliceInterfaceType, elem.Len(), elem.Cap()))
  262. reflect.Copy(nelem, elem)
  263. elem = nelem
  264. }
  265. }
  266. return d.handleArrayTableCollectionLast(key, elem)
  267. case reflect.Ptr:
  268. elem := v.Elem()
  269. if !elem.IsValid() {
  270. ptr := reflect.New(v.Type().Elem())
  271. v.Set(ptr)
  272. elem = ptr.Elem()
  273. }
  274. elem, err := d.handleArrayTableCollectionLast(key, elem)
  275. if err != nil {
  276. return reflect.Value{}, err
  277. }
  278. v.Elem().Set(elem)
  279. return v, nil
  280. case reflect.Slice:
  281. elemType := v.Type().Elem()
  282. var elem reflect.Value
  283. if elemType.Kind() == reflect.Interface {
  284. elem = makeMapStringInterface()
  285. } else {
  286. elem = reflect.New(elemType).Elem()
  287. }
  288. elem2, err := d.handleArrayTable(key, elem)
  289. if err != nil {
  290. return reflect.Value{}, err
  291. }
  292. if elem2.IsValid() {
  293. elem = elem2
  294. }
  295. return reflect.Append(v, elem), nil
  296. case reflect.Array:
  297. idx := d.arrayIndex(true, v)
  298. if idx >= v.Len() {
  299. return v, fmt.Errorf("%s at position %d", d.typeMismatchError("array table", v.Type()), idx)
  300. }
  301. elem := v.Index(idx)
  302. _, err := d.handleArrayTable(key, elem)
  303. return v, err
  304. default:
  305. return reflect.Value{}, d.typeMismatchError("array table", v.Type())
  306. }
  307. }
  308. // When parsing an array table expression, each part of the key needs to be
  309. // evaluated like a normal key, but if it returns a collection, it also needs to
  310. // point to the last element of the collection. Unless it is the last part of
  311. // the key, then it needs to create a new element at the end.
  312. func (d *decoder) handleArrayTableCollection(key unstable.Iterator, v reflect.Value) (reflect.Value, error) {
  313. if key.IsLast() {
  314. return d.handleArrayTableCollectionLast(key, v)
  315. }
  316. switch v.Kind() {
  317. case reflect.Ptr:
  318. elem := v.Elem()
  319. if !elem.IsValid() {
  320. ptr := reflect.New(v.Type().Elem())
  321. v.Set(ptr)
  322. elem = ptr.Elem()
  323. }
  324. elem, err := d.handleArrayTableCollection(key, elem)
  325. if err != nil {
  326. return reflect.Value{}, err
  327. }
  328. if elem.IsValid() {
  329. v.Elem().Set(elem)
  330. }
  331. return v, nil
  332. case reflect.Slice:
  333. elem := v.Index(v.Len() - 1)
  334. x, err := d.handleArrayTable(key, elem)
  335. if err != nil || d.skipUntilTable {
  336. return reflect.Value{}, err
  337. }
  338. if x.IsValid() {
  339. elem.Set(x)
  340. }
  341. return v, err
  342. case reflect.Array:
  343. idx := d.arrayIndex(false, v)
  344. if idx >= v.Len() {
  345. return v, fmt.Errorf("%s at position %d", d.typeMismatchError("array table", v.Type()), idx)
  346. }
  347. elem := v.Index(idx)
  348. _, err := d.handleArrayTable(key, elem)
  349. return v, err
  350. }
  351. return d.handleArrayTable(key, v)
  352. }
  353. func (d *decoder) handleKeyPart(key unstable.Iterator, v reflect.Value, nextFn handlerFn, makeFn valueMakerFn) (reflect.Value, error) {
  354. var rv reflect.Value
  355. // First, dispatch over v to make sure it is a valid object.
  356. // There is no guarantee over what it could be.
  357. switch v.Kind() {
  358. case reflect.Ptr:
  359. elem := v.Elem()
  360. if !elem.IsValid() {
  361. v.Set(reflect.New(v.Type().Elem()))
  362. }
  363. elem = v.Elem()
  364. return d.handleKeyPart(key, elem, nextFn, makeFn)
  365. case reflect.Map:
  366. vt := v.Type()
  367. // Create the key for the map element. Convert to key type.
  368. mk, err := d.keyFromData(vt.Key(), key.Node().Data)
  369. if err != nil {
  370. return reflect.Value{}, err
  371. }
  372. // If the map does not exist, create it.
  373. if v.IsNil() {
  374. vt := v.Type()
  375. v = reflect.MakeMap(vt)
  376. rv = v
  377. }
  378. mv := v.MapIndex(mk)
  379. set := false
  380. if !mv.IsValid() {
  381. // If there is no value in the map, create a new one according to
  382. // the map type. If the element type is interface, create either a
  383. // map[string]interface{} or a []interface{} depending on whether
  384. // this is the last part of the array table key.
  385. t := vt.Elem()
  386. if t.Kind() == reflect.Interface {
  387. mv = makeFn()
  388. } else {
  389. mv = reflect.New(t).Elem()
  390. }
  391. set = true
  392. } else if mv.Kind() == reflect.Interface {
  393. mv = mv.Elem()
  394. if !mv.IsValid() {
  395. mv = makeFn()
  396. }
  397. set = true
  398. } else if !mv.CanAddr() {
  399. vt := v.Type()
  400. t := vt.Elem()
  401. oldmv := mv
  402. mv = reflect.New(t).Elem()
  403. mv.Set(oldmv)
  404. set = true
  405. }
  406. x, err := nextFn(key, mv)
  407. if err != nil {
  408. return reflect.Value{}, err
  409. }
  410. if x.IsValid() {
  411. mv = x
  412. set = true
  413. }
  414. if set {
  415. v.SetMapIndex(mk, mv)
  416. }
  417. case reflect.Struct:
  418. path, found := structFieldPath(v, string(key.Node().Data))
  419. if !found {
  420. d.skipUntilTable = true
  421. return reflect.Value{}, nil
  422. }
  423. if d.errorContext == nil {
  424. d.errorContext = new(errorContext)
  425. }
  426. t := v.Type()
  427. d.errorContext.Struct = t
  428. d.errorContext.Field = path
  429. f := fieldByIndex(v, path)
  430. x, err := nextFn(key, f)
  431. if err != nil || d.skipUntilTable {
  432. return reflect.Value{}, err
  433. }
  434. if x.IsValid() {
  435. f.Set(x)
  436. }
  437. d.errorContext.Field = nil
  438. d.errorContext.Struct = nil
  439. case reflect.Interface:
  440. if v.Elem().IsValid() {
  441. v = v.Elem()
  442. } else {
  443. v = makeMapStringInterface()
  444. }
  445. x, err := d.handleKeyPart(key, v, nextFn, makeFn)
  446. if err != nil {
  447. return reflect.Value{}, err
  448. }
  449. if x.IsValid() {
  450. v = x
  451. }
  452. rv = v
  453. default:
  454. panic(fmt.Errorf("unhandled part: %s", v.Kind()))
  455. }
  456. return rv, nil
  457. }
  458. // HandleArrayTablePart navigates the Go structure v using the key v. It is
  459. // only used for the prefix (non-last) parts of an array-table. When
  460. // encountering a collection, it should go to the last element.
  461. func (d *decoder) handleArrayTablePart(key unstable.Iterator, v reflect.Value) (reflect.Value, error) {
  462. var makeFn valueMakerFn
  463. if key.IsLast() {
  464. makeFn = makeSliceInterface
  465. } else {
  466. makeFn = makeMapStringInterface
  467. }
  468. return d.handleKeyPart(key, v, d.handleArrayTableCollection, makeFn)
  469. }
  470. // HandleTable returns a reference when it has checked the next expression but
  471. // cannot handle it.
  472. func (d *decoder) handleTable(key unstable.Iterator, v reflect.Value) (reflect.Value, error) {
  473. if v.Kind() == reflect.Slice {
  474. if v.Len() == 0 {
  475. return reflect.Value{}, unstable.NewParserError(key.Node().Data, "cannot store a table in a slice")
  476. }
  477. elem := v.Index(v.Len() - 1)
  478. x, err := d.handleTable(key, elem)
  479. if err != nil {
  480. return reflect.Value{}, err
  481. }
  482. if x.IsValid() {
  483. elem.Set(x)
  484. }
  485. return reflect.Value{}, nil
  486. }
  487. if key.Next() {
  488. // Still scoping the key
  489. return d.handleTablePart(key, v)
  490. }
  491. // Done scoping the key.
  492. // Now handle all the key-value expressions in this table.
  493. return d.handleKeyValues(v)
  494. }
  495. // Handle root expressions until the end of the document or the next
  496. // non-key-value.
  497. func (d *decoder) handleKeyValues(v reflect.Value) (reflect.Value, error) {
  498. var rv reflect.Value
  499. for d.nextExpr() {
  500. expr := d.expr()
  501. if expr.Kind != unstable.KeyValue {
  502. // Stash the expression so that fromParser can just loop and use
  503. // the right handler.
  504. // We could just recurse ourselves here, but at least this gives a
  505. // chance to pop the stack a bit.
  506. d.stashExpr()
  507. break
  508. }
  509. err := d.seen.CheckExpression(expr)
  510. if err != nil {
  511. return reflect.Value{}, err
  512. }
  513. x, err := d.handleKeyValue(expr, v)
  514. if err != nil {
  515. return reflect.Value{}, err
  516. }
  517. if x.IsValid() {
  518. v = x
  519. rv = x
  520. }
  521. }
  522. return rv, nil
  523. }
  524. type (
  525. handlerFn func(key unstable.Iterator, v reflect.Value) (reflect.Value, error)
  526. valueMakerFn func() reflect.Value
  527. )
  528. func makeMapStringInterface() reflect.Value {
  529. return reflect.MakeMap(mapStringInterfaceType)
  530. }
  531. func makeSliceInterface() reflect.Value {
  532. return reflect.MakeSlice(sliceInterfaceType, 0, 16)
  533. }
  534. func (d *decoder) handleTablePart(key unstable.Iterator, v reflect.Value) (reflect.Value, error) {
  535. return d.handleKeyPart(key, v, d.handleTable, makeMapStringInterface)
  536. }
  537. func (d *decoder) tryTextUnmarshaler(node *unstable.Node, v reflect.Value) (bool, error) {
  538. // Special case for time, because we allow to unmarshal to it from
  539. // different kind of AST nodes.
  540. if v.Type() == timeType {
  541. return false, nil
  542. }
  543. if v.CanAddr() && v.Addr().Type().Implements(textUnmarshalerType) {
  544. err := v.Addr().Interface().(encoding.TextUnmarshaler).UnmarshalText(node.Data)
  545. if err != nil {
  546. return false, unstable.NewParserError(d.p.Raw(node.Raw), "%w", err)
  547. }
  548. return true, nil
  549. }
  550. return false, nil
  551. }
  552. func (d *decoder) handleValue(value *unstable.Node, v reflect.Value) error {
  553. for v.Kind() == reflect.Ptr {
  554. v = initAndDereferencePointer(v)
  555. }
  556. ok, err := d.tryTextUnmarshaler(value, v)
  557. if ok || err != nil {
  558. return err
  559. }
  560. switch value.Kind {
  561. case unstable.String:
  562. return d.unmarshalString(value, v)
  563. case unstable.Integer:
  564. return d.unmarshalInteger(value, v)
  565. case unstable.Float:
  566. return d.unmarshalFloat(value, v)
  567. case unstable.Bool:
  568. return d.unmarshalBool(value, v)
  569. case unstable.DateTime:
  570. return d.unmarshalDateTime(value, v)
  571. case unstable.LocalDate:
  572. return d.unmarshalLocalDate(value, v)
  573. case unstable.LocalTime:
  574. return d.unmarshalLocalTime(value, v)
  575. case unstable.LocalDateTime:
  576. return d.unmarshalLocalDateTime(value, v)
  577. case unstable.InlineTable:
  578. return d.unmarshalInlineTable(value, v)
  579. case unstable.Array:
  580. return d.unmarshalArray(value, v)
  581. default:
  582. panic(fmt.Errorf("handleValue not implemented for %s", value.Kind))
  583. }
  584. }
  585. func (d *decoder) unmarshalArray(array *unstable.Node, v reflect.Value) error {
  586. switch v.Kind() {
  587. case reflect.Slice:
  588. if v.IsNil() {
  589. v.Set(reflect.MakeSlice(v.Type(), 0, 16))
  590. } else {
  591. v.SetLen(0)
  592. }
  593. case reflect.Array:
  594. // arrays are always initialized
  595. case reflect.Interface:
  596. elem := v.Elem()
  597. if !elem.IsValid() {
  598. elem = reflect.New(sliceInterfaceType).Elem()
  599. elem.Set(reflect.MakeSlice(sliceInterfaceType, 0, 16))
  600. } else if elem.Kind() == reflect.Slice {
  601. if elem.Type() != sliceInterfaceType {
  602. elem = reflect.New(sliceInterfaceType).Elem()
  603. elem.Set(reflect.MakeSlice(sliceInterfaceType, 0, 16))
  604. } else if !elem.CanSet() {
  605. nelem := reflect.New(sliceInterfaceType).Elem()
  606. nelem.Set(reflect.MakeSlice(sliceInterfaceType, elem.Len(), elem.Cap()))
  607. reflect.Copy(nelem, elem)
  608. elem = nelem
  609. }
  610. }
  611. err := d.unmarshalArray(array, elem)
  612. if err != nil {
  613. return err
  614. }
  615. v.Set(elem)
  616. return nil
  617. default:
  618. // TODO: use newDecodeError, but first the parser needs to fill
  619. // array.Data.
  620. return d.typeMismatchError("array", v.Type())
  621. }
  622. elemType := v.Type().Elem()
  623. it := array.Children()
  624. idx := 0
  625. for it.Next() {
  626. n := it.Node()
  627. // TODO: optimize
  628. if v.Kind() == reflect.Slice {
  629. elem := reflect.New(elemType).Elem()
  630. err := d.handleValue(n, elem)
  631. if err != nil {
  632. return err
  633. }
  634. v.Set(reflect.Append(v, elem))
  635. } else { // array
  636. if idx >= v.Len() {
  637. return nil
  638. }
  639. elem := v.Index(idx)
  640. err := d.handleValue(n, elem)
  641. if err != nil {
  642. return err
  643. }
  644. idx++
  645. }
  646. }
  647. return nil
  648. }
  649. func (d *decoder) unmarshalInlineTable(itable *unstable.Node, v reflect.Value) error {
  650. // Make sure v is an initialized object.
  651. switch v.Kind() {
  652. case reflect.Map:
  653. if v.IsNil() {
  654. v.Set(reflect.MakeMap(v.Type()))
  655. }
  656. case reflect.Struct:
  657. // structs are always initialized.
  658. case reflect.Interface:
  659. elem := v.Elem()
  660. if !elem.IsValid() {
  661. elem = makeMapStringInterface()
  662. v.Set(elem)
  663. }
  664. return d.unmarshalInlineTable(itable, elem)
  665. default:
  666. return unstable.NewParserError(d.p.Raw(itable.Raw), "cannot store inline table in Go type %s", v.Kind())
  667. }
  668. it := itable.Children()
  669. for it.Next() {
  670. n := it.Node()
  671. x, err := d.handleKeyValue(n, v)
  672. if err != nil {
  673. return err
  674. }
  675. if x.IsValid() {
  676. v = x
  677. }
  678. }
  679. return nil
  680. }
  681. func (d *decoder) unmarshalDateTime(value *unstable.Node, v reflect.Value) error {
  682. dt, err := parseDateTime(value.Data)
  683. if err != nil {
  684. return err
  685. }
  686. v.Set(reflect.ValueOf(dt))
  687. return nil
  688. }
  689. func (d *decoder) unmarshalLocalDate(value *unstable.Node, v reflect.Value) error {
  690. ld, err := parseLocalDate(value.Data)
  691. if err != nil {
  692. return err
  693. }
  694. if v.Type() == timeType {
  695. cast := ld.AsTime(time.Local)
  696. v.Set(reflect.ValueOf(cast))
  697. return nil
  698. }
  699. v.Set(reflect.ValueOf(ld))
  700. return nil
  701. }
  702. func (d *decoder) unmarshalLocalTime(value *unstable.Node, v reflect.Value) error {
  703. lt, rest, err := parseLocalTime(value.Data)
  704. if err != nil {
  705. return err
  706. }
  707. if len(rest) > 0 {
  708. return unstable.NewParserError(rest, "extra characters at the end of a local time")
  709. }
  710. v.Set(reflect.ValueOf(lt))
  711. return nil
  712. }
  713. func (d *decoder) unmarshalLocalDateTime(value *unstable.Node, v reflect.Value) error {
  714. ldt, rest, err := parseLocalDateTime(value.Data)
  715. if err != nil {
  716. return err
  717. }
  718. if len(rest) > 0 {
  719. return unstable.NewParserError(rest, "extra characters at the end of a local date time")
  720. }
  721. if v.Type() == timeType {
  722. cast := ldt.AsTime(time.Local)
  723. v.Set(reflect.ValueOf(cast))
  724. return nil
  725. }
  726. v.Set(reflect.ValueOf(ldt))
  727. return nil
  728. }
  729. func (d *decoder) unmarshalBool(value *unstable.Node, v reflect.Value) error {
  730. b := value.Data[0] == 't'
  731. switch v.Kind() {
  732. case reflect.Bool:
  733. v.SetBool(b)
  734. case reflect.Interface:
  735. v.Set(reflect.ValueOf(b))
  736. default:
  737. return unstable.NewParserError(value.Data, "cannot assign boolean to a %t", b)
  738. }
  739. return nil
  740. }
  741. func (d *decoder) unmarshalFloat(value *unstable.Node, v reflect.Value) error {
  742. f, err := parseFloat(value.Data)
  743. if err != nil {
  744. return err
  745. }
  746. switch v.Kind() {
  747. case reflect.Float64:
  748. v.SetFloat(f)
  749. case reflect.Float32:
  750. if f > math.MaxFloat32 {
  751. return unstable.NewParserError(value.Data, "number %f does not fit in a float32", f)
  752. }
  753. v.SetFloat(f)
  754. case reflect.Interface:
  755. v.Set(reflect.ValueOf(f))
  756. default:
  757. return unstable.NewParserError(value.Data, "float cannot be assigned to %s", v.Kind())
  758. }
  759. return nil
  760. }
  761. const (
  762. maxInt = int64(^uint(0) >> 1)
  763. minInt = -maxInt - 1
  764. )
  765. // Maximum value of uint for decoding. Currently the decoder parses the integer
  766. // into an int64. As a result, on architectures where uint is 64 bits, the
  767. // effective maximum uint we can decode is the maximum of int64. On
  768. // architectures where uint is 32 bits, the maximum value we can decode is
  769. // lower: the maximum of uint32. I didn't find a way to figure out this value at
  770. // compile time, so it is computed during initialization.
  771. var maxUint int64 = math.MaxInt64
  772. func init() {
  773. m := uint64(^uint(0))
  774. if m < uint64(maxUint) {
  775. maxUint = int64(m)
  776. }
  777. }
  778. func (d *decoder) unmarshalInteger(value *unstable.Node, v reflect.Value) error {
  779. kind := v.Kind()
  780. if kind == reflect.Float32 || kind == reflect.Float64 {
  781. return d.unmarshalFloat(value, v)
  782. }
  783. i, err := parseInteger(value.Data)
  784. if err != nil {
  785. return err
  786. }
  787. var r reflect.Value
  788. switch kind {
  789. case reflect.Int64:
  790. v.SetInt(i)
  791. return nil
  792. case reflect.Int32:
  793. if i < math.MinInt32 || i > math.MaxInt32 {
  794. return fmt.Errorf("toml: number %d does not fit in an int32", i)
  795. }
  796. r = reflect.ValueOf(int32(i))
  797. case reflect.Int16:
  798. if i < math.MinInt16 || i > math.MaxInt16 {
  799. return fmt.Errorf("toml: number %d does not fit in an int16", i)
  800. }
  801. r = reflect.ValueOf(int16(i))
  802. case reflect.Int8:
  803. if i < math.MinInt8 || i > math.MaxInt8 {
  804. return fmt.Errorf("toml: number %d does not fit in an int8", i)
  805. }
  806. r = reflect.ValueOf(int8(i))
  807. case reflect.Int:
  808. if i < minInt || i > maxInt {
  809. return fmt.Errorf("toml: number %d does not fit in an int", i)
  810. }
  811. r = reflect.ValueOf(int(i))
  812. case reflect.Uint64:
  813. if i < 0 {
  814. return fmt.Errorf("toml: negative number %d does not fit in an uint64", i)
  815. }
  816. r = reflect.ValueOf(uint64(i))
  817. case reflect.Uint32:
  818. if i < 0 || i > math.MaxUint32 {
  819. return fmt.Errorf("toml: negative number %d does not fit in an uint32", i)
  820. }
  821. r = reflect.ValueOf(uint32(i))
  822. case reflect.Uint16:
  823. if i < 0 || i > math.MaxUint16 {
  824. return fmt.Errorf("toml: negative number %d does not fit in an uint16", i)
  825. }
  826. r = reflect.ValueOf(uint16(i))
  827. case reflect.Uint8:
  828. if i < 0 || i > math.MaxUint8 {
  829. return fmt.Errorf("toml: negative number %d does not fit in an uint8", i)
  830. }
  831. r = reflect.ValueOf(uint8(i))
  832. case reflect.Uint:
  833. if i < 0 || i > maxUint {
  834. return fmt.Errorf("toml: negative number %d does not fit in an uint", i)
  835. }
  836. r = reflect.ValueOf(uint(i))
  837. case reflect.Interface:
  838. r = reflect.ValueOf(i)
  839. default:
  840. return unstable.NewParserError(d.p.Raw(value.Raw), d.typeMismatchString("integer", v.Type()))
  841. }
  842. if !r.Type().AssignableTo(v.Type()) {
  843. r = r.Convert(v.Type())
  844. }
  845. v.Set(r)
  846. return nil
  847. }
  848. func (d *decoder) unmarshalString(value *unstable.Node, v reflect.Value) error {
  849. switch v.Kind() {
  850. case reflect.String:
  851. v.SetString(string(value.Data))
  852. case reflect.Interface:
  853. v.Set(reflect.ValueOf(string(value.Data)))
  854. default:
  855. return unstable.NewParserError(d.p.Raw(value.Raw), d.typeMismatchString("string", v.Type()))
  856. }
  857. return nil
  858. }
  859. func (d *decoder) handleKeyValue(expr *unstable.Node, v reflect.Value) (reflect.Value, error) {
  860. d.strict.EnterKeyValue(expr)
  861. v, err := d.handleKeyValueInner(expr.Key(), expr.Value(), v)
  862. if d.skipUntilTable {
  863. d.strict.MissingField(expr)
  864. d.skipUntilTable = false
  865. }
  866. d.strict.ExitKeyValue(expr)
  867. return v, err
  868. }
  869. func (d *decoder) handleKeyValueInner(key unstable.Iterator, value *unstable.Node, v reflect.Value) (reflect.Value, error) {
  870. if key.Next() {
  871. // Still scoping the key
  872. return d.handleKeyValuePart(key, value, v)
  873. }
  874. // Done scoping the key.
  875. // v is whatever Go value we need to fill.
  876. return reflect.Value{}, d.handleValue(value, v)
  877. }
  878. func (d *decoder) keyFromData(keyType reflect.Type, data []byte) (reflect.Value, error) {
  879. switch {
  880. case stringType.AssignableTo(keyType):
  881. return reflect.ValueOf(string(data)), nil
  882. case stringType.ConvertibleTo(keyType):
  883. return reflect.ValueOf(string(data)).Convert(keyType), nil
  884. case keyType.Implements(textUnmarshalerType):
  885. mk := reflect.New(keyType.Elem())
  886. if err := mk.Interface().(encoding.TextUnmarshaler).UnmarshalText(data); err != nil {
  887. return reflect.Value{}, fmt.Errorf("toml: error unmarshalling key type %s from text: %w", stringType, err)
  888. }
  889. return mk, nil
  890. case reflect.PtrTo(keyType).Implements(textUnmarshalerType):
  891. mk := reflect.New(keyType)
  892. if err := mk.Interface().(encoding.TextUnmarshaler).UnmarshalText(data); err != nil {
  893. return reflect.Value{}, fmt.Errorf("toml: error unmarshalling key type %s from text: %w", stringType, err)
  894. }
  895. return mk.Elem(), nil
  896. }
  897. return reflect.Value{}, fmt.Errorf("toml: cannot convert map key of type %s to expected type %s", stringType, keyType)
  898. }
  899. func (d *decoder) handleKeyValuePart(key unstable.Iterator, value *unstable.Node, v reflect.Value) (reflect.Value, error) {
  900. // contains the replacement for v
  901. var rv reflect.Value
  902. // First, dispatch over v to make sure it is a valid object.
  903. // There is no guarantee over what it could be.
  904. switch v.Kind() {
  905. case reflect.Map:
  906. vt := v.Type()
  907. mk, err := d.keyFromData(vt.Key(), key.Node().Data)
  908. if err != nil {
  909. return reflect.Value{}, err
  910. }
  911. // If the map does not exist, create it.
  912. if v.IsNil() {
  913. v = reflect.MakeMap(vt)
  914. rv = v
  915. }
  916. mv := v.MapIndex(mk)
  917. set := false
  918. if !mv.IsValid() || key.IsLast() {
  919. set = true
  920. mv = reflect.New(v.Type().Elem()).Elem()
  921. }
  922. nv, err := d.handleKeyValueInner(key, value, mv)
  923. if err != nil {
  924. return reflect.Value{}, err
  925. }
  926. if nv.IsValid() {
  927. mv = nv
  928. set = true
  929. }
  930. if set {
  931. v.SetMapIndex(mk, mv)
  932. }
  933. case reflect.Struct:
  934. path, found := structFieldPath(v, string(key.Node().Data))
  935. if !found {
  936. d.skipUntilTable = true
  937. break
  938. }
  939. if d.errorContext == nil {
  940. d.errorContext = new(errorContext)
  941. }
  942. t := v.Type()
  943. d.errorContext.Struct = t
  944. d.errorContext.Field = path
  945. f := fieldByIndex(v, path)
  946. if !f.CanSet() {
  947. // If the field is not settable, need to take a slower path and make a copy of
  948. // the struct itself to a new location.
  949. nvp := reflect.New(v.Type())
  950. nvp.Elem().Set(v)
  951. v = nvp.Elem()
  952. _, err := d.handleKeyValuePart(key, value, v)
  953. if err != nil {
  954. return reflect.Value{}, err
  955. }
  956. return nvp.Elem(), nil
  957. }
  958. x, err := d.handleKeyValueInner(key, value, f)
  959. if err != nil {
  960. return reflect.Value{}, err
  961. }
  962. if x.IsValid() {
  963. f.Set(x)
  964. }
  965. d.errorContext.Struct = nil
  966. d.errorContext.Field = nil
  967. case reflect.Interface:
  968. v = v.Elem()
  969. // Following encoding/json: decoding an object into an
  970. // interface{}, it needs to always hold a
  971. // map[string]interface{}. This is for the types to be
  972. // consistent whether a previous value was set or not.
  973. if !v.IsValid() || v.Type() != mapStringInterfaceType {
  974. v = makeMapStringInterface()
  975. }
  976. x, err := d.handleKeyValuePart(key, value, v)
  977. if err != nil {
  978. return reflect.Value{}, err
  979. }
  980. if x.IsValid() {
  981. v = x
  982. }
  983. rv = v
  984. case reflect.Ptr:
  985. elem := v.Elem()
  986. if !elem.IsValid() {
  987. ptr := reflect.New(v.Type().Elem())
  988. v.Set(ptr)
  989. rv = v
  990. elem = ptr.Elem()
  991. }
  992. elem2, err := d.handleKeyValuePart(key, value, elem)
  993. if err != nil {
  994. return reflect.Value{}, err
  995. }
  996. if elem2.IsValid() {
  997. elem = elem2
  998. }
  999. v.Elem().Set(elem)
  1000. default:
  1001. return reflect.Value{}, fmt.Errorf("unhandled kv part: %s", v.Kind())
  1002. }
  1003. return rv, nil
  1004. }
  1005. func initAndDereferencePointer(v reflect.Value) reflect.Value {
  1006. var elem reflect.Value
  1007. if v.IsNil() {
  1008. ptr := reflect.New(v.Type().Elem())
  1009. v.Set(ptr)
  1010. }
  1011. elem = v.Elem()
  1012. return elem
  1013. }
  1014. // Same as reflect.Value.FieldByIndex, but creates pointers if needed.
  1015. func fieldByIndex(v reflect.Value, path []int) reflect.Value {
  1016. for _, x := range path {
  1017. v = v.Field(x)
  1018. if v.Kind() == reflect.Ptr {
  1019. if v.IsNil() {
  1020. v.Set(reflect.New(v.Type().Elem()))
  1021. }
  1022. v = v.Elem()
  1023. }
  1024. }
  1025. return v
  1026. }
  1027. type fieldPathsMap = map[string][]int
  1028. var globalFieldPathsCache atomic.Value // map[danger.TypeID]fieldPathsMap
  1029. func structFieldPath(v reflect.Value, name string) ([]int, bool) {
  1030. t := v.Type()
  1031. cache, _ := globalFieldPathsCache.Load().(map[danger.TypeID]fieldPathsMap)
  1032. fieldPaths, ok := cache[danger.MakeTypeID(t)]
  1033. if !ok {
  1034. fieldPaths = map[string][]int{}
  1035. forEachField(t, nil, func(name string, path []int) {
  1036. fieldPaths[name] = path
  1037. // extra copy for the case-insensitive match
  1038. fieldPaths[strings.ToLower(name)] = path
  1039. })
  1040. newCache := make(map[danger.TypeID]fieldPathsMap, len(cache)+1)
  1041. newCache[danger.MakeTypeID(t)] = fieldPaths
  1042. for k, v := range cache {
  1043. newCache[k] = v
  1044. }
  1045. globalFieldPathsCache.Store(newCache)
  1046. }
  1047. path, ok := fieldPaths[name]
  1048. if !ok {
  1049. path, ok = fieldPaths[strings.ToLower(name)]
  1050. }
  1051. return path, ok
  1052. }
  1053. func forEachField(t reflect.Type, path []int, do func(name string, path []int)) {
  1054. n := t.NumField()
  1055. for i := 0; i < n; i++ {
  1056. f := t.Field(i)
  1057. if !f.Anonymous && f.PkgPath != "" {
  1058. // only consider exported fields.
  1059. continue
  1060. }
  1061. fieldPath := append(path, i)
  1062. fieldPath = fieldPath[:len(fieldPath):len(fieldPath)]
  1063. name := f.Tag.Get("toml")
  1064. if name == "-" {
  1065. continue
  1066. }
  1067. if i := strings.IndexByte(name, ','); i >= 0 {
  1068. name = name[:i]
  1069. }
  1070. if f.Anonymous && name == "" {
  1071. t2 := f.Type
  1072. if t2.Kind() == reflect.Ptr {
  1073. t2 = t2.Elem()
  1074. }
  1075. if t2.Kind() == reflect.Struct {
  1076. forEachField(t2, fieldPath, do)
  1077. }
  1078. continue
  1079. }
  1080. if name == "" {
  1081. name = f.Name
  1082. }
  1083. do(name, fieldPath)
  1084. }
  1085. }