skip loading fields that are already populated

g-counter v0.0.3
Jordan Orelli 4 years ago
parent 765d13bb19
commit 7bbaa6d8ed

@ -60,6 +60,11 @@ func (e *env) load(dest Test) error {
continue continue
} }
fv := destV.Field(i) fv := destV.Field(i)
if !fv.IsZero() {
// the value is already populated, so we don't want to overwrite
// it.
continue
}
set := false set := false
for e := e; e != nil; e = e.parent { for e := e; e != nil; e = e.parent {

@ -81,7 +81,9 @@ func TestLoad(t *testing.T) {
Foo int `tea:"load"` Foo int `tea:"load"`
} }
e.load(&test) if err := e.load(&test); err != nil {
t.Errorf("unexpected load error: %v", err)
}
if test.Foo != 5 { if test.Foo != 5 {
t.Errorf("expected value %v but saw %v instead", 5, test.Foo) t.Errorf("expected value %v but saw %v instead", 5, test.Foo)
} }
@ -101,6 +103,25 @@ func TestLoad(t *testing.T) {
t.Errorf("expected a load error but did not see one") t.Errorf("expected a load error but did not see one")
} }
}) })
t.Run("skip load if field is already set", func(t *testing.T) {
e := &env{
data: map[string]interface{}{"Foo": 3},
}
var test struct {
empty
Foo int `tea:"load"`
}
test.Foo = 5
if err := e.load(&test); err != nil {
t.Errorf("unexpected load error: %v", err)
}
if test.Foo != 5 {
t.Errorf("load overwrote expected value of 5 with %d", test.Foo)
}
})
} }
func TestMatch(t *testing.T) { func TestMatch(t *testing.T) {

Loading…
Cancel
Save