Fix lint for ptr.To()

This commit is contained in:
Tim Hockin committed 2026-07-08 09:59:36 -07:00
1 parent 181177004b
commit 045d301647
6 files changed
+72 -82

No files matched your search

@@ -25,7 +25,6 @@ import (
"k8s.io/apimachinery/pkg/api/operation"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
)
type TestStruct struct {
@@ -310,12 +309,12 @@ func TestEachMapValRatcheting(t *testing.T) {
)
testEachMapValRatcheting(t, "struct with pointer field, same value different pointer",
map[string]NonComparableStructWithPtr{
"one": {I: 11, P: ptr.To(1)},
"two": {I: 12, P: ptr.To(2)},
"one": {I: 11, P: new(1)},
"two": {I: 12, P: new(2)},
},
map[string]NonComparableStructWithPtr{
"one": {I: 11, P: ptr.To(1)},
"two": {I: 12, P: ptr.To(2)},
"one": {I: 11, P: new(1)},
"two": {I: 12, P: new(2)},
},
SemanticDeepEqual,
0,
@@ -492,16 +491,16 @@ func testValSliceUniqueByReflect[T any](t *testing.T, name string, input []T, wa
}
func TestEachPtrSliceVal(t *testing.T) {
testEachPtrSliceVal(t, "valid", []*int{ptr.To(11), ptr.To(12), ptr.To(13)})
testEachPtrSliceVal(t, "valid", []*string{ptr.To("a"), ptr.To("b"), ptr.To("c")})
testEachPtrSliceVal(t, "valid", []*TestStruct{ptr.To(TestStruct{11, "a"}), ptr.To(TestStruct{12, "b"}), ptr.To(TestStruct{13, "c"})})
testEachPtrSliceVal(t, "valid", []*int{new(11), new(12), new(13)})
testEachPtrSliceVal(t, "valid", []*string{new("a"), new("b"), new("c")})
testEachPtrSliceVal(t, "valid", []*TestStruct{{11, "a"}, {12, "b"}, {13, "c"}})
testEachPtrSliceVal(t, "empty", []*int{})
testEachPtrSliceVal[int](t, "nil", nil)
// Test nil elements
t.Run("nil elements", func(t *testing.T) {
input := []*int{ptr.To(11), nil, ptr.To(13)}
input := []*int{new(11), nil, new(13)}
calls := 0
vfn := func(ctx context.Context, op operation.Operation, fldPath *field.Path, newVal, oldVal *int) field.ErrorList {
calls++
@@ -516,7 +515,7 @@ func TestEachPtrSliceVal(t *testing.T) {
}
})
testEachPtrSliceValUpdate(t, "valid", []*int{ptr.To(11), ptr.To(12), ptr.To(13)})
testEachPtrSliceValUpdate(t, "valid", []*int{new(11), new(12), new(13)})
}
func testEachPtrSliceVal[T any](t *testing.T, name string, input []*T) {
@@ -572,12 +571,8 @@ func testEachPtrSliceValUpdate[T any](t *testing.T, name string, input []*T) {
func TestEachPtrSliceValRatcheting(t *testing.T) {
testEachPtrSliceValRatcheting(t, "ComparableStruct same data different order",
[]*TestStruct{
ptr.To(TestStruct{11, "a"}), ptr.To(TestStruct{12, "b"}), ptr.To(TestStruct{13, "c"}),
},
[]*TestStruct{
ptr.To(TestStruct{11, "a"}), ptr.To(TestStruct{13, "c"}), ptr.To(TestStruct{12, "b"}),
},
[]*TestStruct{{11, "a"}, {12, "b"}, {13, "c"}},
[]*TestStruct{{11, "a"}, {13, "c"}, {12, "b"}},
SemanticDeepEqual,
nil,
)
@@ -600,10 +595,10 @@ func testEachPtrSliceValRatcheting[T any](t *testing.T, name string, old, new []
func TestPtrSliceUnique(t *testing.T) {
testPtrSliceUnique(t, "int_nil", []*int(nil), 0, 0)
testPtrSliceUnique(t, "int_empty", []*int{}, 0, 0)
testPtrSliceUnique(t, "int_uniq", []*int{ptr.To(1), ptr.To(2), ptr.To(3)}, 0, 0)
testPtrSliceUnique(t, "int_dup", []*int{ptr.To(1), ptr.To(2), ptr.To(3), ptr.To(2), ptr.To(1)}, 2, 0)
testPtrSliceUnique(t, "int_nil_element", []*int{ptr.To(1), nil, ptr.To(3), nil}, 0, 0)
testPtrSliceUnique(t, "int_dup_and_nil", []*int{ptr.To(1), nil, ptr.To(1), nil}, 1, 0)
testPtrSliceUnique(t, "int_uniq", []*int{new(1), new(2), new(3)}, 0, 0)
testPtrSliceUnique(t, "int_dup", []*int{new(1), new(2), new(3), new(2), new(1)}, 2, 0)
testPtrSliceUnique(t, "int_nil_element", []*int{new(1), nil, new(3), nil}, 0, 0)
testPtrSliceUnique(t, "int_dup_and_nil", []*int{new(1), nil, new(1), nil}, 1, 0)
}
func testPtrSliceUnique[T comparable](t *testing.T, name string, input []*T, wantDupErrs, wantReqErrs int) {
@@ -637,8 +632,8 @@ func TestPtrSliceNoNils(t *testing.T) {
}{
{"nil", nil, 0},
{"empty", []*int{}, 0},
{"no_nil", []*int{ptr.To(1), ptr.To(2)}, 0},
{"has_nil", []*int{ptr.To(1), nil, ptr.To(3), nil}, 2},
{"no_nil", []*int{new(1), new(2)}, 0},
{"has_nil", []*int{new(1), nil, new(3), nil}, 2},
}
for _, tt := range tests {
@@ -23,7 +23,6 @@ import (
"k8s.io/apimachinery/pkg/api/operation"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
)
type multiKeyItem struct {
@@ -179,7 +178,7 @@ func TestPtrSliceItem(t *testing.T) {
{
name: "no match",
new: []*multiKeyItem{
ptr.To(multiKeyItem{K1: "a", K2: "1", V: 1}),
{K1: "a", K2: "1", V: 1},
},
match: func(i *multiKeyItem) bool {
return i.K1 == "target"
@@ -192,8 +191,8 @@ func TestPtrSliceItem(t *testing.T) {
{
name: "new item with matching keys",
new: []*multiKeyItem{
ptr.To(multiKeyItem{K1: "a", K2: "1", V: 1}),
ptr.To(multiKeyItem{K1: "target", K2: "target2", V: 2}),
{K1: "a", K2: "1", V: 1},
{K1: "target", K2: "target2", V: 2},
},
match: func(i *multiKeyItem) bool {
return i.K1 == "target" && i.K2 == "target2"
@@ -210,7 +209,7 @@ func TestPtrSliceItem(t *testing.T) {
name: "nil element in new list (ignored)",
new: []*multiKeyItem{
nil,
ptr.To(multiKeyItem{K1: "target", V: 1}),
{K1: "target", V: 1},
},
match: func(i *multiKeyItem) bool {
return i.K1 == "target"
@@ -22,7 +22,6 @@ import (
"k8s.io/apimachinery/pkg/api/operation"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
)
func TestUpdateValue(t *testing.T) {
@@ -705,7 +704,7 @@ func TestPtrSliceUpdate(t *testing.T) {
{
name: "create operation - no validation",
op: operation.Create,
value: []*string{ptr.To("a")},
value: []*string{new("a")},
oldValue: nil,
constraints: []UpdateConstraint{NoSet, NoUnset, NoAddItem, NoRemoveItem},
match: DirectEqual[string],
@@ -713,7 +712,7 @@ func TestPtrSliceUpdate(t *testing.T) {
{
name: "NoSet nil to non-empty (forbidden)",
op: operation.Update,
value: []*string{ptr.To("a")},
value: []*string{new("a")},
oldValue: nil,
constraints: []UpdateConstraint{NoSet},
wantDetails: []string{"field cannot be set once created"},
@@ -721,8 +720,8 @@ func TestPtrSliceUpdate(t *testing.T) {
{
name: "NoAddItem direct-equal item added",
op: operation.Update,
value: []*string{ptr.To("a"), ptr.To("b"), ptr.To("c")},
oldValue: []*string{ptr.To("a"), ptr.To("c")},
value: []*string{new("a"), new("b"), new("c")},
oldValue: []*string{new("a"), new("c")},
match: DirectEqual[string],
constraints: []UpdateConstraint{NoAddItem},
wantDetails: []string{"item may not be added"},
@@ -730,8 +729,8 @@ func TestPtrSliceUpdate(t *testing.T) {
{
name: "NoRemoveItem direct-equal item removed",
op: operation.Update,
value: []*string{ptr.To("a")},
oldValue: []*string{ptr.To("a"), ptr.To("b")},
value: []*string{new("a")},
oldValue: []*string{new("a"), new("b")},
match: DirectEqual[string],
constraints: []UpdateConstraint{NoRemoveItem},
wantDetails: []string{"item may not be removed"},
@@ -739,8 +738,8 @@ func TestPtrSliceUpdate(t *testing.T) {
{
name: "nil element in value (ignored)",
op: operation.Update,
value: []*string{ptr.To("a"), nil, ptr.To("c")},
oldValue: []*string{ptr.To("a"), ptr.To("c")},
value: []*string{new("a"), nil, new("c")},
oldValue: []*string{new("a"), new("c")},
match: DirectEqual[string],
constraints: []UpdateConstraint{NoAddItem},
// Expect 0 errors because nil is ignored and non-nil elements match old values.
@@ -768,8 +767,8 @@ func TestPtrSliceUpdate(t *testing.T) {
t.Run("NoAddItem+NoRemoveItem keyed match allows item modification", func(t *testing.T) {
op := operation.Operation{Type: operation.Update}
newList := []*keyed{ptr.To(keyed{Name: "alpha", Value: "v2"}), ptr.To(keyed{Name: "beta", Value: "v1"})}
oldList := []*keyed{ptr.To(keyed{Name: "alpha", Value: "v1"}), ptr.To(keyed{Name: "beta", Value: "v1"})}
newList := []*keyed{{Name: "alpha", Value: "v2"}, {Name: "beta", Value: "v1"}}
oldList := []*keyed{{Name: "alpha", Value: "v1"}, {Name: "beta", Value: "v1"}}
errs := PtrSliceUpdate(context.TODO(), op, field.NewPath("test"), newList, oldList, keyMatch, NoAddItem, NoRemoveItem)
if len(errs) != 0 {
t.Errorf("expected no errors for keyed-match item modification, got %v", errs)
@@ -778,7 +777,7 @@ func TestPtrSliceUpdate(t *testing.T) {
t.Run("NoAddItem without match returns internal error", func(t *testing.T) {
op := operation.Operation{Type: operation.Update}
errs := PtrSliceUpdate[string](context.TODO(), op, field.NewPath("test"), []*string{ptr.To("a")}, nil, nil, NoAddItem)
errs := PtrSliceUpdate[string](context.TODO(), op, field.NewPath("test"), []*string{new("a")}, nil, nil, NoAddItem)
if len(errs) != 1 || errs[0].Type != field.ErrorTypeInternal {
t.Errorf("expected single InternalError, got %v", errs)
}
@@ -20,7 +20,6 @@ import (
"testing"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
)
func Test(t *testing.T) {
@@ -34,7 +33,7 @@ func Test(t *testing.T) {
// 2. Non-nil elements trigger validation errors
st.Value(&Struct{
ListField: []*OtherStruct{{}, {}},
ListPrimitiveField: []*string{ptr.To("a"), ptr.To("b")},
ListPrimitiveField: []*string{new("a"), new("b")},
}).ExpectValidateFalseByPath(map[string][]string{
"listField[0]": {"field Struct.ListField[*]", "type OtherStruct"},
"listField[1]": {"field Struct.ListField[*]", "type OtherStruct"},
@@ -20,7 +20,6 @@ import (
"testing"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
)
func TestUnique(t *testing.T) {
@@ -47,18 +46,18 @@ func TestUnique(t *testing.T) {
CustomUniqueListWithTypeSet: []string{"a", "b", "a"},
CustomUniqueListWithTypeMap: []Item{{Key: "a"}, {Key: "b"}, {Key: "a"}},
SliceMapFieldWithPtrKey: []PtrKeyStruct{
{Key: ptr.To("a"), Data: "first"},
{Key: ptr.To("b"), Data: "second"},
{Key: new("a"), Data: "first"},
{Key: new("b"), Data: "second"},
},
SliceMapFieldWithMixedKeys: []ItemWithMixedKeys{
{Key1: ptr.To("a"), Key2: "x", Data: "first"},
{Key1: ptr.To("a"), Key2: "y", Data: "second"},
{Key1: new("a"), Key2: "x", Data: "first"},
{Key1: new("a"), Key2: "y", Data: "second"},
},
SliceMapFieldWithMultiplePtrKeys: []ItemWithMultiplePtrKeys{
{Key1: ptr.To("a"), Key2: ptr.To("x"), Data: "first"},
{Key1: ptr.To("a"), Key2: ptr.To("y"), Data: "second"},
{Key1: new("a"), Key2: new("x"), Data: "first"},
{Key1: new("a"), Key2: new("y"), Data: "second"},
},
PrimitivePointerListUniqueSet: []*string{ptr.To("aaa"), ptr.To("bbb")},
PrimitivePointerListUniqueSet: []*string{new("aaa"), new("bbb")},
PointerListUniqueMap: []*Item{
{Key: "key1", Data: "one"},
{Key: "key2", Data: "two"},
@@ -87,9 +86,9 @@ func TestUnique(t *testing.T) {
AtomicListUniqueMap: []Item{{Key: "single", Data: "one"}},
CustomUniqueListWithTypeSet: []string{"single"},
CustomUniqueListWithTypeMap: []Item{{Key: "single"}},
SliceMapFieldWithMixedKeys: []ItemWithMixedKeys{{Key1: ptr.To("a"), Key2: "b", Data: "one"}},
SliceMapFieldWithMultiplePtrKeys: []ItemWithMultiplePtrKeys{{Key1: ptr.To("a"), Key2: ptr.To("b"), Data: "one"}},
PrimitivePointerListUniqueSet: []*string{ptr.To("single")},
SliceMapFieldWithMixedKeys: []ItemWithMixedKeys{{Key1: new("a"), Key2: "b", Data: "one"}},
SliceMapFieldWithMultiplePtrKeys: []ItemWithMultiplePtrKeys{{Key1: new("a"), Key2: new("b"), Data: "one"}},
PrimitivePointerListUniqueSet: []*string{new("single")},
PointerListUniqueMap: []*Item{{Key: "single", Data: "one"}},
}).ExpectValid()
@@ -114,21 +113,21 @@ func TestUnique(t *testing.T) {
CustomUniqueListWithTypeSet: []string{"a", "b", "a"},
CustomUniqueListWithTypeMap: []Item{{Key: "a"}, {Key: "b"}, {Key: "a"}},
SliceMapFieldWithPtrKey: []PtrKeyStruct{
{Key: ptr.To("a"), Data: "first"},
{Key: ptr.To("b"), Data: "second"},
{Key: ptr.To("a"), Data: "third"},
{Key: new("a"), Data: "first"},
{Key: new("b"), Data: "second"},
{Key: new("a"), Data: "third"},
},
SliceMapFieldWithMixedKeys: []ItemWithMixedKeys{
{Key1: ptr.To("a"), Key2: "x", Data: "first"},
{Key1: ptr.To("a"), Key2: "y", Data: "second"},
{Key1: ptr.To("a"), Key2: "x", Data: "third"},
{Key1: new("a"), Key2: "x", Data: "first"},
{Key1: new("a"), Key2: "y", Data: "second"},
{Key1: new("a"), Key2: "x", Data: "third"},
},
SliceMapFieldWithMultiplePtrKeys: []ItemWithMultiplePtrKeys{
{Key1: ptr.To("a"), Key2: ptr.To("x"), Data: "first"},
{Key1: ptr.To("a"), Key2: ptr.To("y"), Data: "second"},
{Key1: ptr.To("a"), Key2: ptr.To("x"), Data: "third"},
{Key1: new("a"), Key2: new("x"), Data: "first"},
{Key1: new("a"), Key2: new("y"), Data: "second"},
{Key1: new("a"), Key2: new("x"), Data: "third"},
},
PrimitivePointerListUniqueSet: []*string{ptr.To("aaa"), ptr.To("bbb"), ptr.To("aaa")},
PrimitivePointerListUniqueSet: []*string{new("aaa"), new("bbb"), new("aaa")},
PointerListUniqueMap: []*Item{
{Key: "key1", Data: "one"},
{Key: "key2", Data: "two"},
@@ -165,7 +164,7 @@ func TestUnique(t *testing.T) {
// Test nil elements in pointer lists (should trigger Required errors)
st.Value(&Struct{
PrimitivePointerListUniqueSet: []*string{ptr.To("a"), nil, ptr.To("b")},
PrimitivePointerListUniqueSet: []*string{new("a"), nil, new("b")},
PointerListUniqueMap: []*Item{{Key: "key1"}, nil},
}).ExpectMatches(field.ErrorMatcher{}.ByType().ByField(), field.ErrorList{
field.Required(field.NewPath("primitivePointerListUniqueSet").Index(1), ""),
@@ -193,18 +192,18 @@ func TestRatcheting(t *testing.T) {
CustomUniqueListWithTypeSet: []string{"a", "b", "a"},
CustomUniqueListWithTypeMap: []Item{{Key: "a"}, {Key: "b"}, {Key: "a"}},
SliceMapFieldWithPtrKey: []PtrKeyStruct{
{Key: ptr.To("a"), Data: "first"},
{Key: ptr.To("b"), Data: "second"},
{Key: new("a"), Data: "first"},
{Key: new("b"), Data: "second"},
},
SliceMapFieldWithMixedKeys: []ItemWithMixedKeys{
{Key1: ptr.To("a"), Key2: "x", Data: "first"},
{Key1: ptr.To("a"), Key2: "y", Data: "second"},
{Key1: new("a"), Key2: "x", Data: "first"},
{Key1: new("a"), Key2: "y", Data: "second"},
},
SliceMapFieldWithMultiplePtrKeys: []ItemWithMultiplePtrKeys{
{Key1: ptr.To("a"), Key2: ptr.To("x"), Data: "first"},
{Key1: ptr.To("a"), Key2: ptr.To("y"), Data: "second"},
{Key1: new("a"), Key2: new("x"), Data: "first"},
{Key1: new("a"), Key2: new("y"), Data: "second"},
},
PrimitivePointerListUniqueSet: []*string{ptr.To("aaa"), ptr.To("bbb")},
PrimitivePointerListUniqueSet: []*string{new("aaa"), new("bbb")},
PointerListUniqueMap: []*Item{
{Key: "key1", Data: "one"},
{Key: "key2", Data: "two"},
@@ -229,18 +228,18 @@ func TestRatcheting(t *testing.T) {
CustomUniqueListWithTypeSet: []string{"a", "a", "b"},
CustomUniqueListWithTypeMap: []Item{{Key: "a"}, {Key: "a"}, {Key: "b"}},
SliceMapFieldWithPtrKey: []PtrKeyStruct{
{Key: ptr.To("b"), Data: "second"},
{Key: ptr.To("a"), Data: "first"},
{Key: new("b"), Data: "second"},
{Key: new("a"), Data: "first"},
},
SliceMapFieldWithMixedKeys: []ItemWithMixedKeys{
{Key1: ptr.To("a"), Key2: "y", Data: "second"},
{Key1: ptr.To("a"), Key2: "x", Data: "first"},
{Key1: new("a"), Key2: "y", Data: "second"},
{Key1: new("a"), Key2: "x", Data: "first"},
},
SliceMapFieldWithMultiplePtrKeys: []ItemWithMultiplePtrKeys{
{Key1: ptr.To("a"), Key2: ptr.To("y"), Data: "second"},
{Key1: ptr.To("a"), Key2: ptr.To("x"), Data: "first"},
{Key1: new("a"), Key2: new("y"), Data: "second"},
{Key1: new("a"), Key2: new("x"), Data: "first"},
},
PrimitivePointerListUniqueSet: []*string{ptr.To("bbb"), ptr.To("aaa")},
PrimitivePointerListUniqueSet: []*string{new("bbb"), new("aaa")},
PointerListUniqueMap: []*Item{
{Key: "key2", Data: "two"},
{Key: "key1", Data: "one"},
@@ -20,7 +20,6 @@ import (
"testing"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
)
func TestUpdateListTags(t *testing.T) {
@@ -304,7 +303,7 @@ func TestUpdateListTags(t *testing.T) {
old := base
old.PointerSliceNoSet = nil
cur := base
cur.PointerSliceNoSet = []*string{ptr.To("a")}
cur.PointerSliceNoSet = []*string{new("a")}
st.Value(&cur).OldValue(&old).ExpectMatches(matcher, field.ErrorList{
field.Invalid(field.NewPath("pointerSliceNoSet"), nil, "field cannot be set once created").WithOrigin("update"),
@@ -317,9 +316,9 @@ func TestUpdateListTags(t *testing.T) {
// Pointer listType=set + NoAddItem
{
old := base
old.PointerSetNoAdd = []*string{ptr.To("a"), ptr.To("b")}
old.PointerSetNoAdd = []*string{new("a"), new("b")}
cur := base
cur.PointerSetNoAdd = []*string{ptr.To("a"), ptr.To("b"), ptr.To("c")}
cur.PointerSetNoAdd = []*string{new("a"), new("b"), new("c")}
st.Value(&cur).OldValue(&old).ExpectMatches(matcher, field.ErrorList{
field.Forbidden(field.NewPath("pointerSetNoAdd").Index(2), "item may not be added").WithOrigin("update"),
@@ -346,7 +345,7 @@ func TestUpdateListTags(t *testing.T) {
// Nil element in pointer slice on update should trigger Required error from PtrSliceUpdate
{
old := base
old.PointerSetNoAdd = []*string{ptr.To("a")}
old.PointerSetNoAdd = []*string{new("a")}
cur := base
cur.PointerSetNoAdd = []*string{nil}