Skip to content

Commit a43cfef

Browse files
lmikaantonmedv
authored andcommitted
Added pointer dereferencing for field extraction
1 parent cf61ee1 commit a43cfef

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Diff for: eval_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ var evalTests = []evalTest{
129129
struct{ Foo struct{ Bar bool } }{Foo: struct{ Bar bool }{Bar: true}},
130130
true,
131131
},
132+
{
133+
`Foo.Bar`,
134+
&struct{ Foo *struct{ Bar bool } }{Foo: &struct{ Bar bool }{Bar: true}},
135+
true,
136+
},
132137
{
133138
"foo[2]",
134139
map[string]interface{}{"foo": []rune{'a', 'b', 'c'}},
@@ -162,6 +167,29 @@ var evalTests = []evalTest{
162167
}{1, 1},
163168
true,
164169
},
170+
{
171+
`[true][A]`,
172+
&struct{ A int }{0},
173+
true,
174+
},
175+
{
176+
`A-1`,
177+
&struct{ A int }{1},
178+
float64(0),
179+
},
180+
{
181+
`A == 0`,
182+
&struct{ A uint8 }{0},
183+
true,
184+
},
185+
{
186+
`A == B`,
187+
&struct {
188+
A uint8
189+
B float64
190+
}{1, 1},
191+
true,
192+
},
165193
{
166194
`5 in 0..9`,
167195
nil,
@@ -187,6 +215,11 @@ var evalTests = []evalTest{
187215
map[string]interface{}{"foo": map[string]interface{}{"bar": map[string]interface{}{"baz": true}}},
188216
true,
189217
},
218+
{
219+
"foo.Bar['baz']",
220+
map[string]interface{}{"foo": &struct{ Bar map[string]interface{}}{Bar: map[string]interface{}{"baz": true}}},
221+
true,
222+
},
190223
{
191224
`60 & 13`,
192225
nil,

Diff for: utils.go

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ func extract(from interface{}, it interface{}) (interface{}, error) {
9090
if value.IsValid() && value.CanInterface() {
9191
return value.Interface(), nil
9292
}
93+
case reflect.Ptr:
94+
derefValue := reflect.ValueOf(from).Elem()
95+
if derefValue.IsValid() && derefValue.CanInterface() {
96+
return extract(derefValue.Interface(), it)
97+
}
9398
}
9499
}
95100
return nil, fmt.Errorf("can't get %q from %T", it, from)

0 commit comments

Comments
 (0)