Support TextUnmarshaler

This commit is contained in:
Thomas Pelletier
2021-03-24 21:02:02 -04:00
parent a0d031abec
commit dd5837651d
3 changed files with 50 additions and 50 deletions
+2 -2
View File
@@ -12,8 +12,8 @@ Development branch. Probably does not work.
- [x] Support Date / times.
- [x] Support struct tags annotations.
- [x] Support Arrays.
- [ ] Support Unmarshaler interface.
- [ ] Original go-toml unmarshal tests pass.
- [x] Support Unmarshaler interface.
- [x] Original go-toml unmarshal tests pass.
- [ ] Benchmark!
- [ ] Abstract AST.
- [ ] Attach comments to AST (gated by parser flag).
@@ -1994,6 +1994,7 @@ type parent struct {
}
func TestCustomUnmarshal(t *testing.T) {
t.Skip("not sure if UnmarshalTOML is a good idea")
input := `
[Doc]
key = "ok1"
@@ -2002,18 +2003,15 @@ func TestCustomUnmarshal(t *testing.T) {
`
var d parent
if err := toml.Unmarshal([]byte(input), &d); err != nil {
t.Fatalf("unexpected err: %s", err.Error())
}
if d.Doc.Decoded.Key != "ok1" {
t.Errorf("Bad unmarshal: expected ok, got %v", d.Doc.Decoded.Key)
}
if d.DocPointer.Decoded.Key != "ok2" {
t.Errorf("Bad unmarshal: expected ok, got %v", d.DocPointer.Decoded.Key)
}
err := toml.Unmarshal([]byte(input), &d)
require.NoError(t, err)
assert.Equal(t, "ok1", d.Doc.Decoded.Key)
assert.Equal(t, "ok2", d.DocPointer.Decoded.Key)
}
func TestCustomUnmarshalError(t *testing.T) {
t.Skip("not sure if UnmarshalTOML is a good idea")
input := `
[Doc]
key = 1
@@ -2071,25 +2069,13 @@ Bool = true
Int = 21
Float = 2.0
`
if err := toml.Unmarshal([]byte(input), &doc); err != nil {
t.Fatalf("unexpected err: %s", err.Error())
}
if doc.UnixTime.Value != 12 {
t.Fatalf("expected UnixTime: 12 got: %d", doc.UnixTime.Value)
}
if doc.Version.Value != 42 {
t.Fatalf("expected Version: 42 got: %d", doc.Version.Value)
}
if doc.Bool.Value != 1 {
t.Fatalf("expected Bool: 1 got: %d", doc.Bool.Value)
}
if doc.Int.Value != 21 {
t.Fatalf("expected Int: 21 got: %d", doc.Int.Value)
}
if doc.Float.Value != 2 {
t.Fatalf("expected Float: 2 got: %d", doc.Float.Value)
}
err := toml.Unmarshal([]byte(input), &doc)
require.NoError(t, err)
assert.Equal(t, 12, doc.UnixTime.Value)
assert.Equal(t, 42, doc.Version.Value)
assert.Equal(t, 1, doc.Bool.Value)
assert.Equal(t, 21, doc.Int.Value)
assert.Equal(t, 2, doc.Float.Value)
}
func TestTextUnmarshalError(t *testing.T) {
+34 -20
View File
@@ -1,8 +1,8 @@
package toml
import (
"encoding"
"fmt"
"os"
"reflect"
"time"
@@ -16,29 +16,10 @@ func Unmarshal(data []byte, v interface{}) error {
return err
}
// TODO: remove me; sanity check
allValidOrDump(p.tree, p.tree)
d := decoder{}
return d.fromAst(p.tree, v)
}
func allValidOrDump(tree ast.Root, nodes []ast.Node) bool {
for i, n := range nodes {
if n.Kind == ast.Invalid {
fmt.Printf("AST contains invalid node! idx=%d\n", i)
fmt.Fprintf(os.Stderr, "%s\n", tree.Sdot())
return false
}
ok := allValidOrDump(tree, n.Children)
if !ok {
return ok
}
}
return true
}
type decoder struct {
// Tracks position in Go arrays.
arrayIndexes map[reflect.Value]int
@@ -187,7 +168,40 @@ func (d *decoder) unmarshalKeyValue(x target, node *ast.Node) error {
return d.unmarshalValue(x, node.Value())
}
var textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
func tryTextUnmarshaler(x target, node *ast.Node) (bool, error) {
v := x.get()
if v.Kind() == reflect.Ptr {
if !v.Elem().IsValid() {
err := x.set(reflect.New(v.Type().Elem()))
if err != nil {
return false, nil
}
v = x.get()
}
return tryTextUnmarshaler(valueTarget(v.Elem()), node)
}
if v.Kind() != reflect.Struct {
return false, nil
}
if v.Type().Implements(textUnmarshalerType) {
return true, v.Interface().(encoding.TextUnmarshaler).UnmarshalText(node.Data)
}
if v.CanAddr() && v.Addr().Type().Implements(textUnmarshalerType) {
return true, v.Addr().Interface().(encoding.TextUnmarshaler).UnmarshalText(node.Data)
}
return false, nil
}
func (d *decoder) unmarshalValue(x target, node *ast.Node) error {
ok, err := tryTextUnmarshaler(x, node)
if ok {
return err
}
switch node.Kind {
case ast.String:
return unmarshalString(x, node)