@@ -123,7 +123,7 @@ type decoder struct {
123
123
stashedExpr bool
124
124
125
125
// Skip expressions until a table is found. This is set to true when a
126
- // table could not be create (missing field in map), so all KV expressions
126
+ // table could not be created (missing field in map), so all KV expressions
127
127
// need to be skipped.
128
128
skipUntilTable bool
129
129
@@ -483,7 +483,7 @@ func (d *decoder) handleKeyPart(key ast.Iterator, v reflect.Value, nextFn handle
483
483
d .errorContext .Struct = t
484
484
d .errorContext .Field = path
485
485
486
- f := v . FieldByIndex ( path )
486
+ f := fieldByIndex ( v , path )
487
487
x , err := nextFn (key , f )
488
488
if err != nil || d .skipUntilTable {
489
489
return reflect.Value {}, err
@@ -1071,7 +1071,7 @@ func (d *decoder) handleKeyValuePart(key ast.Iterator, value *ast.Node, v reflec
1071
1071
d .errorContext .Struct = t
1072
1072
d .errorContext .Field = path
1073
1073
1074
- f := v . FieldByIndex ( path )
1074
+ f := fieldByIndex ( v , path )
1075
1075
x , err := d .handleKeyValueInner (key , value , f )
1076
1076
if err != nil {
1077
1077
return reflect.Value {}, err
@@ -1135,6 +1135,21 @@ func initAndDereferencePointer(v reflect.Value) reflect.Value {
1135
1135
return elem
1136
1136
}
1137
1137
1138
+ // Same as reflect.Value.FieldByIndex, but creates pointers if needed.
1139
+ func fieldByIndex (v reflect.Value , path []int ) reflect.Value {
1140
+ for i , x := range path {
1141
+ v = v .Field (x )
1142
+
1143
+ if i < len (path )- 1 && v .Kind () == reflect .Pointer {
1144
+ if v .IsNil () {
1145
+ v .Set (reflect .New (v .Type ().Elem ()))
1146
+ }
1147
+ v = v .Elem ()
1148
+ }
1149
+ }
1150
+ return v
1151
+ }
1152
+
1138
1153
type fieldPathsMap = map [string ][]int
1139
1154
1140
1155
var globalFieldPathsCache atomic.Value // map[danger.TypeID]fieldPathsMap
@@ -1192,7 +1207,11 @@ func forEachField(t reflect.Type, path []int, do func(name string, path []int))
1192
1207
}
1193
1208
1194
1209
if f .Anonymous && name == "" {
1195
- forEachField (f .Type , fieldPath , do )
1210
+ t2 := f .Type
1211
+ if t2 .Kind () == reflect .Pointer {
1212
+ t2 = t2 .Elem ()
1213
+ }
1214
+ forEachField (t2 , fieldPath , do )
1196
1215
continue
1197
1216
}
1198
1217
0 commit comments