unmarshaler.go 32 KB

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