unmarshaler.go 28 KB

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