From 78bd0a8618a2768753e66e289c5621242b9687a9 Mon Sep 17 00:00:00 2001 From: jiangpengcheng Date: Mon, 29 Jan 2024 11:07:56 +0800 Subject: [PATCH 1/3] [Improve] Use ptr for `TopicAutoCreationConfig.Allow` field --- pulsaradmin/pkg/admin/namespace_test.go | 26 +++++++++++-------- .../pkg/utils/topic_auto_creation_config.go | 2 +- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/pulsaradmin/pkg/admin/namespace_test.go b/pulsaradmin/pkg/admin/namespace_test.go index f934a96865..715102a9c5 100644 --- a/pulsaradmin/pkg/admin/namespace_test.go +++ b/pulsaradmin/pkg/admin/namespace_test.go @@ -31,6 +31,10 @@ func ptr(n int) *int { return &n } +func boolPtr(b bool) *bool { + return &b +} + func TestSetTopicAutoCreation(t *testing.T) { config := &config.Config{} admin, err := New(config) @@ -47,7 +51,7 @@ func TestSetTopicAutoCreation(t *testing.T) { name: "Set partitioned type topic auto creation", namespace: "public/default", config: utils.TopicAutoCreationConfig{ - Allow: true, + Allow: boolPtr(true), Type: utils.Partitioned, Partitions: ptr(3), }, @@ -57,7 +61,7 @@ func TestSetTopicAutoCreation(t *testing.T) { name: "Set partitioned type topic auto creation without partitions", namespace: "public/default", config: utils.TopicAutoCreationConfig{ - Allow: true, + Allow: boolPtr(true), Type: utils.Partitioned, }, errReason: "Invalid configuration for autoTopicCreationOverride. the detail is [defaultNumPartitions] " + @@ -67,7 +71,7 @@ func TestSetTopicAutoCreation(t *testing.T) { name: "Set partitioned type topic auto creation with partitions < 1", namespace: "public/default", config: utils.TopicAutoCreationConfig{ - Allow: true, + Allow: boolPtr(true), Type: utils.Partitioned, Partitions: ptr(-1), }, @@ -78,7 +82,7 @@ func TestSetTopicAutoCreation(t *testing.T) { name: "Set non-partitioned type topic auto creation", namespace: "public/default", config: utils.TopicAutoCreationConfig{ - Allow: true, + Allow: boolPtr(true), Type: utils.NonPartitioned, }, errReason: "", @@ -87,7 +91,7 @@ func TestSetTopicAutoCreation(t *testing.T) { name: "Set non-partitioned type topic auto creation with partitions", namespace: "public/default", config: utils.TopicAutoCreationConfig{ - Allow: true, + Allow: boolPtr(true), Type: utils.NonPartitioned, Partitions: ptr(3), }, @@ -98,7 +102,7 @@ func TestSetTopicAutoCreation(t *testing.T) { name: "Disable topic auto creation", namespace: "public/default", config: utils.TopicAutoCreationConfig{ - Allow: false, + Allow: boolPtr(false), }, errReason: "", }, @@ -106,7 +110,7 @@ func TestSetTopicAutoCreation(t *testing.T) { name: "Set topic auto creation on a non-exist namespace", namespace: "public/nonexist", config: utils.TopicAutoCreationConfig{ - Allow: true, + Allow: boolPtr(true), Type: utils.NonPartitioned, }, errReason: "Namespace does not exist", @@ -115,7 +119,7 @@ func TestSetTopicAutoCreation(t *testing.T) { name: "Set topic auto creation on a non-exist tenant", namespace: "non-exist/default", config: utils.TopicAutoCreationConfig{ - Allow: true, + Allow: boolPtr(true), Type: utils.NonPartitioned, }, errReason: "Tenant does not exist", @@ -149,14 +153,14 @@ func TestGetTopicAutoCreation(t *testing.T) { // set the topic auto creation config and get it err = admin.Namespaces().SetTopicAutoCreation(*namespace, utils.TopicAutoCreationConfig{ - Allow: true, + Allow: boolPtr(true), Type: utils.NonPartitioned, }) assert.Equal(t, nil, err) topicAutoCreation, err := admin.Namespaces().GetTopicAutoCreation(*namespace) assert.Equal(t, nil, err) expected := utils.TopicAutoCreationConfig{ - Allow: true, + Allow: boolPtr(true), Type: utils.NonPartitioned, } assert.Equal(t, expected, *topicAutoCreation) @@ -168,7 +172,7 @@ func TestGetTopicAutoCreation(t *testing.T) { topicAutoCreation, err = admin.Namespaces().GetTopicAutoCreation(*namespace) assert.Equal(t, nil, err) expected = utils.TopicAutoCreationConfig{ - Allow: false, + Allow: boolPtr(false), Type: "", } assert.Equal(t, expected, *topicAutoCreation) diff --git a/pulsaradmin/pkg/utils/topic_auto_creation_config.go b/pulsaradmin/pkg/utils/topic_auto_creation_config.go index 8444514250..9b8dfd7165 100644 --- a/pulsaradmin/pkg/utils/topic_auto_creation_config.go +++ b/pulsaradmin/pkg/utils/topic_auto_creation_config.go @@ -18,7 +18,7 @@ package utils type TopicAutoCreationConfig struct { - Allow bool `json:"allowAutoTopicCreation"` + Allow *bool `json:"allowAutoTopicCreation"` Type TopicType `json:"topicType"` Partitions *int `json:"defaultNumPartitions"` } From 6e9f035cfd62bdcb28526b6e6b69863d1f94232e Mon Sep 17 00:00:00 2001 From: jiangpengcheng Date: Thu, 14 Mar 2024 09:40:14 +0800 Subject: [PATCH 2/3] fix test --- pulsaradmin/pkg/admin/namespace_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pulsaradmin/pkg/admin/namespace_test.go b/pulsaradmin/pkg/admin/namespace_test.go index 715102a9c5..299fa3f52d 100644 --- a/pulsaradmin/pkg/admin/namespace_test.go +++ b/pulsaradmin/pkg/admin/namespace_test.go @@ -172,7 +172,7 @@ func TestGetTopicAutoCreation(t *testing.T) { topicAutoCreation, err = admin.Namespaces().GetTopicAutoCreation(*namespace) assert.Equal(t, nil, err) expected = utils.TopicAutoCreationConfig{ - Allow: boolPtr(false), + Allow: nil, Type: "", } assert.Equal(t, expected, *topicAutoCreation) From f1b97cbbf7d95e5bd8794f4c43e08693475938ff Mon Sep 17 00:00:00 2001 From: jiangpengcheng Date: Fri, 15 Mar 2024 15:31:41 +0800 Subject: [PATCH 3/3] add omitempty --- pulsaradmin/pkg/utils/topic_auto_creation_config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pulsaradmin/pkg/utils/topic_auto_creation_config.go b/pulsaradmin/pkg/utils/topic_auto_creation_config.go index 9b8dfd7165..387f6b4396 100644 --- a/pulsaradmin/pkg/utils/topic_auto_creation_config.go +++ b/pulsaradmin/pkg/utils/topic_auto_creation_config.go @@ -18,7 +18,7 @@ package utils type TopicAutoCreationConfig struct { - Allow *bool `json:"allowAutoTopicCreation"` + Allow *bool `json:"allowAutoTopicCreation,omitempty"` Type TopicType `json:"topicType"` - Partitions *int `json:"defaultNumPartitions"` + Partitions *int `json:"defaultNumPartitions,omitempty"` }