Skip to content

Commit

Permalink
Added pointer dereferencing for field extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
lmika authored and antonmedv committed Jul 29, 2018
1 parent cf61ee1 commit a43cfef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
33 changes: 33 additions & 0 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ var evalTests = []evalTest{
struct{ Foo struct{ Bar bool } }{Foo: struct{ Bar bool }{Bar: true}},
true,
},
{
`Foo.Bar`,
&struct{ Foo *struct{ Bar bool } }{Foo: &struct{ Bar bool }{Bar: true}},
true,
},
{
"foo[2]",
map[string]interface{}{"foo": []rune{'a', 'b', 'c'}},
Expand Down Expand Up @@ -162,6 +167,29 @@ var evalTests = []evalTest{
}{1, 1},
true,
},
{
`[true][A]`,
&struct{ A int }{0},
true,
},
{
`A-1`,
&struct{ A int }{1},
float64(0),
},
{
`A == 0`,
&struct{ A uint8 }{0},
true,
},
{
`A == B`,
&struct {
A uint8
B float64
}{1, 1},
true,
},
{
`5 in 0..9`,
nil,
Expand All @@ -187,6 +215,11 @@ var evalTests = []evalTest{
map[string]interface{}{"foo": map[string]interface{}{"bar": map[string]interface{}{"baz": true}}},
true,
},
{
"foo.Bar['baz']",
map[string]interface{}{"foo": &struct{ Bar map[string]interface{}}{Bar: map[string]interface{}{"baz": true}}},
true,
},
{
`60 & 13`,
nil,
Expand Down
5 changes: 5 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func extract(from interface{}, it interface{}) (interface{}, error) {
if value.IsValid() && value.CanInterface() {
return value.Interface(), nil
}
case reflect.Ptr:
derefValue := reflect.ValueOf(from).Elem()
if derefValue.IsValid() && derefValue.CanInterface() {
return extract(derefValue.Interface(), it)
}
}
}
return nil, fmt.Errorf("can't get %q from %T", it, from)
Expand Down

0 comments on commit a43cfef

Please sign in to comment.