Skip to content

Commit f60b557

Browse files
authored
chore: remove usage of k8s.io/utils/pointer (#7209)
1 parent c2871e1 commit f60b557

File tree

5 files changed

+17
-23
lines changed

5 files changed

+17
-23
lines changed

enterprise/audit/diff.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/coder/coder/coderd/audit"
1212
"github.com/coder/coder/coderd/database"
13+
"github.com/coder/coder/coderd/util/ptr"
1314
)
1415

1516
func structName(t reflect.Type) string {
@@ -132,10 +133,10 @@ func convertDiffType(left, right any) (newLeft, newRight any, changed bool) {
132133
if !typedLeft.Valid {
133134
leftInt64Ptr = nil
134135
} else {
135-
leftInt64Ptr = ptr(typedLeft.Int64)
136+
leftInt64Ptr = ptr.Ref(typedLeft.Int64)
136137
}
137138

138-
rightInt64Ptr = ptr(right.(sql.NullInt64).Int64)
139+
rightInt64Ptr = ptr.Ref(right.(sql.NullInt64).Int64)
139140
if !right.(sql.NullInt64).Valid {
140141
rightInt64Ptr = nil
141142
}
@@ -209,23 +210,19 @@ func flattenStructFields(leftV, rightV reflect.Value) ([]fieldDiff, error) {
209210
// derefPointer deferences a reflect.Value that is a pointer to its underlying
210211
// value. It dereferences recursively until it finds a non-pointer value. If the
211212
// pointer is nil, it will be coerced to the zero value of the underlying type.
212-
func derefPointer(ptr reflect.Value) reflect.Value {
213-
if !ptr.IsNil() {
213+
func derefPointer(ref reflect.Value) reflect.Value {
214+
if !ref.IsNil() {
214215
// Grab the value the pointer references.
215-
ptr = ptr.Elem()
216+
ref = ref.Elem()
216217
} else {
217218
// Coerce nil ptrs to zero'd values of their underlying type.
218-
ptr = reflect.Zero(ptr.Type().Elem())
219+
ref = reflect.Zero(ref.Type().Elem())
219220
}
220221

221222
// Recursively deref nested pointers.
222-
if ptr.Kind() == reflect.Ptr {
223-
return derefPointer(ptr)
223+
if ref.Kind() == reflect.Ptr {
224+
return derefPointer(ref)
224225
}
225226

226-
return ptr
227-
}
228-
229-
func ptr[T any](x T) *T {
230-
return &x
227+
return ref
231228
}

enterprise/audit/diff_internal_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
"github.com/lib/pq"
1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/require"
13-
"k8s.io/utils/pointer"
1413

1514
"github.com/coder/coder/coderd/audit"
1615
"github.com/coder/coder/coderd/database"
16+
"github.com/coder/coder/coderd/util/ptr"
1717
)
1818

1919
func Test_diffValues(t *testing.T) {
@@ -82,14 +82,14 @@ func Test_diffValues(t *testing.T) {
8282
runDiffValuesTests(t, table, []diffTest{
8383
{
8484
name: "LeftNil",
85-
left: foo{Bar: nil}, right: foo{Bar: pointer.StringPtr("baz")},
85+
left: foo{Bar: nil}, right: foo{Bar: ptr.Ref("baz")},
8686
exp: audit.Map{
8787
"bar": audit.OldNew{Old: "", New: "baz"},
8888
},
8989
},
9090
{
9191
name: "RightNil",
92-
left: foo{Bar: pointer.StringPtr("baz")}, right: foo{Bar: nil},
92+
left: foo{Bar: ptr.Ref("baz")}, right: foo{Bar: nil},
9393
exp: audit.Map{
9494
"bar": audit.OldNew{Old: "baz", New: ""},
9595
},

enterprise/coderd/groups_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66

77
"github.com/google/uuid"
88
"github.com/stretchr/testify/require"
9-
"k8s.io/utils/pointer"
109

1110
"github.com/coder/coder/coderd/audit"
1211
"github.com/coder/coder/coderd/coderdtest"
1312
"github.com/coder/coder/coderd/database"
13+
"github.com/coder/coder/coderd/util/ptr"
1414
"github.com/coder/coder/codersdk"
1515
"github.com/coder/coder/enterprise/coderd/coderdenttest"
1616
"github.com/coder/coder/enterprise/coderd/license"
@@ -149,8 +149,8 @@ func TestPatchGroup(t *testing.T) {
149149

150150
group, err = client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{
151151
Name: "bye",
152-
AvatarURL: pointer.String("https://google.com"),
153-
QuotaAllowance: pointer.Int(20),
152+
AvatarURL: ptr.Ref("https://google.com"),
153+
QuotaAllowance: ptr.Ref(20),
154154
})
155155
require.NoError(t, err)
156156
require.Equal(t, "bye", group.Name)
@@ -314,7 +314,7 @@ func TestPatchGroup(t *testing.T) {
314314

315315
group1, err = client.PatchGroup(ctx, group1.ID, codersdk.PatchGroupRequest{
316316
Name: group2.Name,
317-
AvatarURL: pointer.String("https://google.com"),
317+
AvatarURL: ptr.Ref("https://google.com"),
318318
})
319319
require.Error(t, err)
320320
cerr, ok := codersdk.AsError(err)

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ require (
168168
gopkg.in/natefinch/lumberjack.v2 v2.0.0
169169
gopkg.in/yaml.v3 v3.0.1
170170
gvisor.dev/gvisor v0.0.0-20221203005347-703fd9b7fbc0
171-
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed
172171
nhooyr.io/websocket v1.8.7
173172
storj.io/drpc v0.0.33-0.20220622181519-9206537a4db7
174173
tailscale.com v1.32.2

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,8 +2948,6 @@ k8s.io/kubernetes v1.13.0/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk=
29482948
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
29492949
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
29502950
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
2951-
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed h1:jAne/RjBTyawwAy0utX5eqigAwz/lQhTmy+Hr/Cpue4=
2952-
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
29532951
modernc.org/b v1.0.0/go.mod h1:uZWcZfRj1BpYzfN9JTerzlNUnnPsV9O2ZA8JsRcubNg=
29542952
modernc.org/cc/v3 v3.32.4/go.mod h1:0R6jl1aZlIl2avnYfbfHBS1QB6/f+16mihBObaBC878=
29552953
modernc.org/ccgo/v3 v3.9.2/go.mod h1:gnJpy6NIVqkETT+L5zPsQFj7L2kkhfPMzOghRNv/CFo=

0 commit comments

Comments
 (0)