diff --git a/retrieve.go b/retrieve.go index dc1bd96..66c0518 100644 --- a/retrieve.go +++ b/retrieve.go @@ -9,7 +9,7 @@ import ( func Get(out interface{}, path string, opts ...option) interface{} { options := newOptions(opts...) - result := get(reflect.ValueOf(out), path) + result := get(reflect.ValueOf(out), path, opts...) // valid kind and we can return a result.Interface() without panic if result.Kind() != reflect.Invalid && result.CanInterface() { // if we don't allow zero and the result is a zero value return nil diff --git a/retrieve_test.go b/retrieve_test.go index b5f4d4e..c091eb7 100644 --- a/retrieve_test.go +++ b/retrieve_test.go @@ -67,6 +67,21 @@ func TestGetThroughInterface(t *testing.T) { is.Equal(Get(foo, "BarPointer.Bars.Bar.Name"), []string{"Level2-1", "Level2-2"}) } +func TestGetWithAllowZero(t *testing.T) { + is := assert.New(t) + + var test []struct { + Age int + } + + for i := 0; i < 10; i++ { + test = append(test, struct{ Age int }{Age: i}) + } + + is.Equal(Get(test, "Age").([]int), []int{1, 2, 3, 4, 5, 6, 7, 8, 9}) + is.Equal(Get(test, "Age", WithAllowZero()).([]int), []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) +} + func TestGetNotFound(t *testing.T) { is := assert.New(t)