Skip to content

Commit

Permalink
[Improve] Use ptr for TopicAutoCreationConfig.Allow field
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangpengcheng committed Jan 29, 2024
1 parent 5768f00 commit 9aef851
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
26 changes: 15 additions & 11 deletions pulsaradmin/pkg/admin/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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),
},
Expand All @@ -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] " +
Expand All @@ -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),
},
Expand All @@ -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: "",
Expand All @@ -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),
},
Expand All @@ -98,15 +102,15 @@ func TestSetTopicAutoCreation(t *testing.T) {
name: "Disable topic auto creation",
namespace: "public/default",
config: utils.TopicAutoCreationConfig{
Allow: false,
Allow: boolPtr(false),
},
errReason: "",
},
{
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",
Expand All @@ -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",
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pulsaradmin/pkg/utils/topic_auto_creation_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 9aef851

Please sign in to comment.