From 3d105327ed6efdac853b47c5d24e242cfe8c1018 Mon Sep 17 00:00:00 2001 From: ehab-eb Date: Mon, 4 Dec 2023 15:42:44 +0100 Subject: [PATCH 1/2] Moved function to common package --- catalog/update.go | 11 +---------- common/util.go | 9 +++++++++ common/util_test.go | 7 +++++++ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/catalog/update.go b/catalog/update.go index 88cead3bd5..d7367bf7c2 100644 --- a/catalog/update.go +++ b/catalog/update.go @@ -8,22 +8,13 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -func contains[T comparable](s []T, e T) bool { - for _, a := range s { - if a == e { - return true - } - } - return false -} - func updateFunctionFactory(pathPrefix string, updatable []string) func(context.Context, *schema.ResourceData, *common.DatabricksClient) error { return func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { patch := map[string]any{} for _, field := range updatable { // these fields cannot be set during creation - if d.IsNewResource() && !contains([]string{ + if d.IsNewResource() && !common.Contains([]string{ "owner", "delta_sharing_scope", "delta_sharing_recipient_token_lifetime_in_seconds", diff --git a/common/util.go b/common/util.go index cf6c3b4ed2..0188d72fd5 100644 --- a/common/util.go +++ b/common/util.go @@ -11,3 +11,12 @@ var ( func StringIsUUID(s string) bool { return uuidRegex.MatchString(s) } + +func Contains[T comparable](s []T, e T) bool { + for _, a := range s { + if a == e { + return true + } + } + return false +} diff --git a/common/util_test.go b/common/util_test.go index 08aa81a086..4f20280ad0 100644 --- a/common/util_test.go +++ b/common/util_test.go @@ -10,3 +10,10 @@ func TestStringIsUUID(t *testing.T) { assert.True(t, StringIsUUID("3f670caf-9a4b-4479-8143-1a0878da8f57")) assert.False(t, StringIsUUID("abc")) } + +func TestContains(t *testing.T) { + assert.True(t, Contains[string]([]string{"a", "b", "c"}, "a")) + assert.False(t, Contains[string]([]string{"a", "b", "c"}, "d")) + assert.True(t, Contains[int]([]int{1, 2, 3}, 1)) + assert.False(t, Contains[int]([]int{1, 2, 3}, 4)) +} From 480a5d09b302f1c53f79d0c182bcc0272bcd6984 Mon Sep 17 00:00:00 2001 From: ehab-eb Date: Tue, 5 Dec 2023 10:15:55 +0100 Subject: [PATCH 2/2] Replace custom `contains` function --- catalog/update.go | 3 ++- common/util.go | 9 --------- common/util_test.go | 7 ------- 3 files changed, 2 insertions(+), 17 deletions(-) diff --git a/catalog/update.go b/catalog/update.go index d7367bf7c2..6d9cb168eb 100644 --- a/catalog/update.go +++ b/catalog/update.go @@ -6,6 +6,7 @@ import ( "github.com/databricks/terraform-provider-databricks/common" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "golang.org/x/exp/slices" ) func updateFunctionFactory(pathPrefix string, updatable []string) func(context.Context, *schema.ResourceData, *common.DatabricksClient) error { @@ -14,7 +15,7 @@ func updateFunctionFactory(pathPrefix string, updatable []string) func(context.C for _, field := range updatable { // these fields cannot be set during creation - if d.IsNewResource() && !common.Contains([]string{ + if d.IsNewResource() && !slices.Contains([]string{ "owner", "delta_sharing_scope", "delta_sharing_recipient_token_lifetime_in_seconds", diff --git a/common/util.go b/common/util.go index 0188d72fd5..cf6c3b4ed2 100644 --- a/common/util.go +++ b/common/util.go @@ -11,12 +11,3 @@ var ( func StringIsUUID(s string) bool { return uuidRegex.MatchString(s) } - -func Contains[T comparable](s []T, e T) bool { - for _, a := range s { - if a == e { - return true - } - } - return false -} diff --git a/common/util_test.go b/common/util_test.go index 4f20280ad0..08aa81a086 100644 --- a/common/util_test.go +++ b/common/util_test.go @@ -10,10 +10,3 @@ func TestStringIsUUID(t *testing.T) { assert.True(t, StringIsUUID("3f670caf-9a4b-4479-8143-1a0878da8f57")) assert.False(t, StringIsUUID("abc")) } - -func TestContains(t *testing.T) { - assert.True(t, Contains[string]([]string{"a", "b", "c"}, "a")) - assert.False(t, Contains[string]([]string{"a", "b", "c"}, "d")) - assert.True(t, Contains[int]([]int{1, 2, 3}, 1)) - assert.False(t, Contains[int]([]int{1, 2, 3}, 4)) -}