Skip to content

Commit 6ea1db5

Browse files
committed
Disallow pointers as listmap keys
1 parent fc7f017 commit 6ea1db5

File tree

1 file changed

+14
-0
lines changed
  • staging/src/k8s.io/code-generator/cmd/validation-gen/validators

1 file changed

+14
-0
lines changed

staging/src/k8s.io/code-generator/cmd/validation-gen/validators/each.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ func (lmktv listMapKeyTagValidator) GetValidations(context Context, _ []string,
151151
return Validations{}, fmt.Errorf("no field for JSON name %q", payload)
152152
} else if k := realType(memb.Type).Kind; k != types.Builtin {
153153
return Validations{}, fmt.Errorf("only primitive types can be list-map keys, not %s", k)
154+
} else if isPointer(memb.Type) {
155+
return Validations{}, fmt.Errorf("pointer types cannot be list-map keys")
154156
} else {
155157
fieldName = memb.Name
156158
}
@@ -166,6 +168,16 @@ func (lmktv listMapKeyTagValidator) GetValidations(context Context, _ []string,
166168
return Validations{}, nil
167169
}
168170

171+
func isPointer(t *types.Type) bool {
172+
if t.Kind == types.Pointer {
173+
return true
174+
}
175+
if t.Kind == types.Alias {
176+
return isPointer(t.Underlying)
177+
}
178+
return false
179+
}
180+
169181
func (lmktv listMapKeyTagValidator) Docs() TagDoc {
170182
doc := TagDoc{
171183
Tag: lmktv.TagName(),
@@ -277,6 +289,8 @@ func (evtv eachValTagValidator) getListValidations(fldPath *field.Path, t *types
277289
}
278290
buf := strings.Builder{}
279291
buf.WriteString("return ")
292+
// Note: this does not handle pointer fields, which are not
293+
// supposed to be used as listMap keys.
280294
for i, fld := range listMap.keyFields {
281295
if i > 0 {
282296
buf.WriteString(" && ")

0 commit comments

Comments
 (0)