Fix Unmarshaler call when value is missing (#439)

Fixes #431
This commit is contained in:
Thomas Pelletier
2020-09-12 14:42:04 -04:00
committed by GitHub
parent 5c94d86029
commit 65ca806488
2 changed files with 39 additions and 0 deletions
+4
View File
@@ -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)
}
+35
View File
@@ -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")
}
}