Skip to content

Commit

Permalink
fix allow zero in get func and adding test for checking that
Browse files Browse the repository at this point in the history
  • Loading branch information
Erfan Momeni authored and Erfan Momeni committed Dec 8, 2022
1 parent b9184fe commit b75b3f5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion retrieve.go
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions retrieve_test.go
Expand Up @@ -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)

Expand Down

0 comments on commit b75b3f5

Please sign in to comment.