diff --git a/enterprise/audit/diff.go b/enterprise/audit/diff.go index 5542ebd2a9a2f..ca5c454e51b54 100644 --- a/enterprise/audit/diff.go +++ b/enterprise/audit/diff.go @@ -10,6 +10,7 @@ import ( "github.com/coder/coder/coderd/audit" "github.com/coder/coder/coderd/database" + "github.com/coder/coder/coderd/util/ptr" ) func structName(t reflect.Type) string { @@ -132,10 +133,10 @@ func convertDiffType(left, right any) (newLeft, newRight any, changed bool) { if !typedLeft.Valid { leftInt64Ptr = nil } else { - leftInt64Ptr = ptr(typedLeft.Int64) + leftInt64Ptr = ptr.Ref(typedLeft.Int64) } - rightInt64Ptr = ptr(right.(sql.NullInt64).Int64) + rightInt64Ptr = ptr.Ref(right.(sql.NullInt64).Int64) if !right.(sql.NullInt64).Valid { rightInt64Ptr = nil } @@ -209,23 +210,19 @@ func flattenStructFields(leftV, rightV reflect.Value) ([]fieldDiff, error) { // derefPointer deferences a reflect.Value that is a pointer to its underlying // value. It dereferences recursively until it finds a non-pointer value. If the // pointer is nil, it will be coerced to the zero value of the underlying type. -func derefPointer(ptr reflect.Value) reflect.Value { - if !ptr.IsNil() { +func derefPointer(ref reflect.Value) reflect.Value { + if !ref.IsNil() { // Grab the value the pointer references. - ptr = ptr.Elem() + ref = ref.Elem() } else { // Coerce nil ptrs to zero'd values of their underlying type. - ptr = reflect.Zero(ptr.Type().Elem()) + ref = reflect.Zero(ref.Type().Elem()) } // Recursively deref nested pointers. - if ptr.Kind() == reflect.Ptr { - return derefPointer(ptr) + if ref.Kind() == reflect.Ptr { + return derefPointer(ref) } - return ptr -} - -func ptr[T any](x T) *T { - return &x + return ref } diff --git a/enterprise/audit/diff_internal_test.go b/enterprise/audit/diff_internal_test.go index 2dd9300ab4438..d53a9ad1f3e82 100644 --- a/enterprise/audit/diff_internal_test.go +++ b/enterprise/audit/diff_internal_test.go @@ -10,10 +10,10 @@ import ( "github.com/lib/pq" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "k8s.io/utils/pointer" "github.com/coder/coder/coderd/audit" "github.com/coder/coder/coderd/database" + "github.com/coder/coder/coderd/util/ptr" ) func Test_diffValues(t *testing.T) { @@ -82,14 +82,14 @@ func Test_diffValues(t *testing.T) { runDiffValuesTests(t, table, []diffTest{ { name: "LeftNil", - left: foo{Bar: nil}, right: foo{Bar: pointer.StringPtr("baz")}, + left: foo{Bar: nil}, right: foo{Bar: ptr.Ref("baz")}, exp: audit.Map{ "bar": audit.OldNew{Old: "", New: "baz"}, }, }, { name: "RightNil", - left: foo{Bar: pointer.StringPtr("baz")}, right: foo{Bar: nil}, + left: foo{Bar: ptr.Ref("baz")}, right: foo{Bar: nil}, exp: audit.Map{ "bar": audit.OldNew{Old: "baz", New: ""}, }, diff --git a/enterprise/coderd/groups_test.go b/enterprise/coderd/groups_test.go index f0fc6f95c2847..48efe1611448b 100644 --- a/enterprise/coderd/groups_test.go +++ b/enterprise/coderd/groups_test.go @@ -6,11 +6,11 @@ import ( "github.com/google/uuid" "github.com/stretchr/testify/require" - "k8s.io/utils/pointer" "github.com/coder/coder/coderd/audit" "github.com/coder/coder/coderd/coderdtest" "github.com/coder/coder/coderd/database" + "github.com/coder/coder/coderd/util/ptr" "github.com/coder/coder/codersdk" "github.com/coder/coder/enterprise/coderd/coderdenttest" "github.com/coder/coder/enterprise/coderd/license" @@ -149,8 +149,8 @@ func TestPatchGroup(t *testing.T) { group, err = client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{ Name: "bye", - AvatarURL: pointer.String("https://google.com"), - QuotaAllowance: pointer.Int(20), + AvatarURL: ptr.Ref("https://google.com"), + QuotaAllowance: ptr.Ref(20), }) require.NoError(t, err) require.Equal(t, "bye", group.Name) @@ -314,7 +314,7 @@ func TestPatchGroup(t *testing.T) { group1, err = client.PatchGroup(ctx, group1.ID, codersdk.PatchGroupRequest{ Name: group2.Name, - AvatarURL: pointer.String("https://google.com"), + AvatarURL: ptr.Ref("https://google.com"), }) require.Error(t, err) cerr, ok := codersdk.AsError(err) diff --git a/go.mod b/go.mod index e63edefb10da4..7fd2abec225c4 100644 --- a/go.mod +++ b/go.mod @@ -168,7 +168,6 @@ require ( gopkg.in/natefinch/lumberjack.v2 v2.0.0 gopkg.in/yaml.v3 v3.0.1 gvisor.dev/gvisor v0.0.0-20221203005347-703fd9b7fbc0 - k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed nhooyr.io/websocket v1.8.7 storj.io/drpc v0.0.33-0.20220622181519-9206537a4db7 tailscale.com v1.32.2 diff --git a/go.sum b/go.sum index c3ead3f4c48fe..f20d603a5f775 100644 --- a/go.sum +++ b/go.sum @@ -2948,8 +2948,6 @@ k8s.io/kubernetes v1.13.0/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk= k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= -k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed h1:jAne/RjBTyawwAy0utX5eqigAwz/lQhTmy+Hr/Cpue4= -k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= modernc.org/b v1.0.0/go.mod h1:uZWcZfRj1BpYzfN9JTerzlNUnnPsV9O2ZA8JsRcubNg= modernc.org/cc/v3 v3.32.4/go.mod h1:0R6jl1aZlIl2avnYfbfHBS1QB6/f+16mihBObaBC878= modernc.org/ccgo/v3 v3.9.2/go.mod h1:gnJpy6NIVqkETT+L5zPsQFj7L2kkhfPMzOghRNv/CFo=