diff --git a/values/structvalue.go b/values/structvalue.go index 64b873a..ae2cdb4 100644 --- a/values/structvalue.go +++ b/values/structvalue.go @@ -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) diff --git a/values/structvalue_test.go b/values/structvalue_test.go index 5df763a..8d35aa9 100644 --- a/values/structvalue_test.go +++ b/values/structvalue_test.go @@ -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")))