unmarshaler.go 33 KB

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