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