@@ -742,6 +742,10 @@ func (d *Decoder) valueFromTree(mtype reflect.Type, tval *Tree, mval1 *reflect.V
|
||||
if mvalPtr := reflect.New(mtype); isCustomUnmarshaler(mvalPtr.Type()) {
|
||||
d.visitor.visitAll()
|
||||
|
||||
if tval == nil {
|
||||
return mvalPtr.Elem(), nil
|
||||
}
|
||||
|
||||
if err := callCustomUnmarshaler(mvalPtr, tval.ToMap()); err != nil {
|
||||
return reflect.ValueOf(nil), fmt.Errorf("unmarshal toml: %v", err)
|
||||
}
|
||||
|
||||
@@ -3941,3 +3941,38 @@ bar = 42
|
||||
|
||||
reflect.DeepEqual(x, expected)
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Key string `toml:"key"`
|
||||
Obj Custom `toml:"obj"`
|
||||
}
|
||||
|
||||
type Custom struct {
|
||||
v string
|
||||
}
|
||||
|
||||
func (c *Custom) UnmarshalTOML(v interface{}) error {
|
||||
c.v = "called"
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestGithubIssue431(t *testing.T) {
|
||||
doc := `key = "value"`
|
||||
tree, err := LoadBytes([]byte(doc))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
var c Config
|
||||
if err := tree.Unmarshal(&c); err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if c.Key != "value" {
|
||||
t.Errorf("expected c.Key='value', not '%s'", c.Key)
|
||||
}
|
||||
|
||||
if c.Obj.v == "called" {
|
||||
t.Errorf("UnmarshalTOML should not have been called")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user