mirror of
https://github.com/danog/liquid.git
synced 2024-11-27 02:44:38 +01:00
Merge pull request #28 from Proximaio/feature/struct_pointer_member_fix
Fix struct PropertyValue attempting to use an invalid pointer
This commit is contained in:
commit
519fbbeb04
@ -45,6 +45,9 @@ func (v structValue) PropertyValue(index Value) Value {
|
||||
}
|
||||
rt = rt.Elem()
|
||||
rv = rv.Elem()
|
||||
if !rv.IsValid() {
|
||||
return nilValue
|
||||
}
|
||||
}
|
||||
if _, found := rt.MethodByName(name); found {
|
||||
m := rv.MethodByName(name)
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
|
||||
type testValueStruct struct {
|
||||
F int
|
||||
Nest *testValueStruct
|
||||
Renamed int `liquid:"name"`
|
||||
Omitted int `liquid:"-"`
|
||||
F1 func() int
|
||||
@ -27,6 +28,7 @@ func (tv *testValueStruct) PM2e() (int, error) { return 4, fmt.Errorf("expected
|
||||
func TestValue_struct(t *testing.T) {
|
||||
s := ValueOf(testValueStruct{
|
||||
F: -1,
|
||||
Nest: &testValueStruct{F: -2},
|
||||
Renamed: 100,
|
||||
Omitted: 200,
|
||||
F1: func() int { return 1 },
|
||||
@ -39,6 +41,10 @@ func TestValue_struct(t *testing.T) {
|
||||
require.True(t, s.Contains(ValueOf("F1")))
|
||||
require.Equal(t, -1, s.PropertyValue(ValueOf("F")).Interface())
|
||||
|
||||
// Nesting
|
||||
require.Equal(t, -2, s.PropertyValue(ValueOf("Nest")).PropertyValue(ValueOf("F")).Interface())
|
||||
require.Equal(t, nil, s.PropertyValue(ValueOf("Nest")).PropertyValue(ValueOf("Nest")).PropertyValue(ValueOf("F")).Interface())
|
||||
|
||||
// field tags
|
||||
require.False(t, s.Contains(ValueOf("Renamed")))
|
||||
require.False(t, s.Contains(ValueOf("Omitted")))
|
||||
|
Loading…
Reference in New Issue
Block a user