chore: remove usage of k8s.io/utils/pointer (#7209)

This commit is contained in:
Colin Adler
2023-04-19 15:53:47 -05:00
committed by GitHub
parent c2871e12aa
commit f60b5579a7
5 changed files with 17 additions and 23 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/coder/coder/coderd/audit" "github.com/coder/coder/coderd/audit"
"github.com/coder/coder/coderd/database" "github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/util/ptr"
) )
func structName(t reflect.Type) string { func structName(t reflect.Type) string {
@ -132,10 +133,10 @@ func convertDiffType(left, right any) (newLeft, newRight any, changed bool) {
if !typedLeft.Valid { if !typedLeft.Valid {
leftInt64Ptr = nil leftInt64Ptr = nil
} else { } 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 { if !right.(sql.NullInt64).Valid {
rightInt64Ptr = nil 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 // 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 // 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. // pointer is nil, it will be coerced to the zero value of the underlying type.
func derefPointer(ptr reflect.Value) reflect.Value { func derefPointer(ref reflect.Value) reflect.Value {
if !ptr.IsNil() { if !ref.IsNil() {
// Grab the value the pointer references. // Grab the value the pointer references.
ptr = ptr.Elem() ref = ref.Elem()
} else { } else {
// Coerce nil ptrs to zero'd values of their underlying type. // 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. // Recursively deref nested pointers.
if ptr.Kind() == reflect.Ptr { if ref.Kind() == reflect.Ptr {
return derefPointer(ptr) return derefPointer(ref)
} }
return ptr return ref
}
func ptr[T any](x T) *T {
return &x
} }

View File

@ -10,10 +10,10 @@ import (
"github.com/lib/pq" "github.com/lib/pq"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"k8s.io/utils/pointer"
"github.com/coder/coder/coderd/audit" "github.com/coder/coder/coderd/audit"
"github.com/coder/coder/coderd/database" "github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/util/ptr"
) )
func Test_diffValues(t *testing.T) { func Test_diffValues(t *testing.T) {
@ -82,14 +82,14 @@ func Test_diffValues(t *testing.T) {
runDiffValuesTests(t, table, []diffTest{ runDiffValuesTests(t, table, []diffTest{
{ {
name: "LeftNil", 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{ exp: audit.Map{
"bar": audit.OldNew{Old: "", New: "baz"}, "bar": audit.OldNew{Old: "", New: "baz"},
}, },
}, },
{ {
name: "RightNil", 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{ exp: audit.Map{
"bar": audit.OldNew{Old: "baz", New: ""}, "bar": audit.OldNew{Old: "baz", New: ""},
}, },

View File

@ -6,11 +6,11 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"k8s.io/utils/pointer"
"github.com/coder/coder/coderd/audit" "github.com/coder/coder/coderd/audit"
"github.com/coder/coder/coderd/coderdtest" "github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/coderd/database" "github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/util/ptr"
"github.com/coder/coder/codersdk" "github.com/coder/coder/codersdk"
"github.com/coder/coder/enterprise/coderd/coderdenttest" "github.com/coder/coder/enterprise/coderd/coderdenttest"
"github.com/coder/coder/enterprise/coderd/license" "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{ group, err = client.PatchGroup(ctx, group.ID, codersdk.PatchGroupRequest{
Name: "bye", Name: "bye",
AvatarURL: pointer.String("https://google.com"), AvatarURL: ptr.Ref("https://google.com"),
QuotaAllowance: pointer.Int(20), QuotaAllowance: ptr.Ref(20),
}) })
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, "bye", group.Name) require.Equal(t, "bye", group.Name)
@ -314,7 +314,7 @@ func TestPatchGroup(t *testing.T) {
group1, err = client.PatchGroup(ctx, group1.ID, codersdk.PatchGroupRequest{ group1, err = client.PatchGroup(ctx, group1.ID, codersdk.PatchGroupRequest{
Name: group2.Name, Name: group2.Name,
AvatarURL: pointer.String("https://google.com"), AvatarURL: ptr.Ref("https://google.com"),
}) })
require.Error(t, err) require.Error(t, err)
cerr, ok := codersdk.AsError(err) cerr, ok := codersdk.AsError(err)

1
go.mod
View File

@ -168,7 +168,6 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.0.0 gopkg.in/natefinch/lumberjack.v2 v2.0.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
gvisor.dev/gvisor v0.0.0-20221203005347-703fd9b7fbc0 gvisor.dev/gvisor v0.0.0-20221203005347-703fd9b7fbc0
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed
nhooyr.io/websocket v1.8.7 nhooyr.io/websocket v1.8.7
storj.io/drpc v0.0.33-0.20220622181519-9206537a4db7 storj.io/drpc v0.0.33-0.20220622181519-9206537a4db7
tailscale.com v1.32.2 tailscale.com v1.32.2

2
go.sum
View File

@ -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-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-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-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/b v1.0.0/go.mod h1:uZWcZfRj1BpYzfN9JTerzlNUnnPsV9O2ZA8JsRcubNg=
modernc.org/cc/v3 v3.32.4/go.mod h1:0R6jl1aZlIl2avnYfbfHBS1QB6/f+16mihBObaBC878= 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= modernc.org/ccgo/v3 v3.9.2/go.mod h1:gnJpy6NIVqkETT+L5zPsQFj7L2kkhfPMzOghRNv/CFo=