From 521b284dae84ce5ca9df42ddb267d9c448f5185f Mon Sep 17 00:00:00 2001 From: Nathan Mills Date: Wed, 22 Jan 2020 02:32:16 -0700 Subject: [PATCH] Add RetentionPolicy parsing (#201) --- pkg/pulsar/utils/backlog_quota.go | 17 +++++++++++++++++ pkg/pulsar/utils/namespace_name.go | 6 +++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pkg/pulsar/utils/backlog_quota.go b/pkg/pulsar/utils/backlog_quota.go index 17139e7..ccfce49 100644 --- a/pkg/pulsar/utils/backlog_quota.go +++ b/pkg/pulsar/utils/backlog_quota.go @@ -17,6 +17,8 @@ package utils +import "github.com/pkg/errors" + type BacklogQuota struct { Limit int64 `json:"limit"` Policy RetentionPolicy `json:"policy"` @@ -40,3 +42,18 @@ const ( ProducerException RetentionPolicy = "producer_exception" ConsumerBacklogEviction RetentionPolicy = "consumer_backlog_eviction" ) + +func ParseRetentionPolicy(str string) (RetentionPolicy, error) { + switch str { + case ProducerException.String(): + return ProducerException, nil + case ConsumerBacklogEviction.String(): + return ConsumerBacklogEviction, nil + default: + return "", errors.Errorf("Invalid retention policy %s", str) + } +} + +func (s RetentionPolicy) String() string { + return string(s) +} diff --git a/pkg/pulsar/utils/namespace_name.go b/pkg/pulsar/utils/namespace_name.go index fbf36ac..f35c66e 100644 --- a/pkg/pulsar/utils/namespace_name.go +++ b/pkg/pulsar/utils/namespace_name.go @@ -65,12 +65,12 @@ func validateNamespaceName(tenant, namespace string) error { return errors.Errorf("Invalid tenant or namespace. [%s/%s]", tenant, namespace) } - ok := checkName(tenant) + ok := CheckName(tenant) if !ok { return errors.Errorf("Tenant name include unsupported special chars. tenant : [%s]", tenant) } - ok = checkName(namespace) + ok = CheckName(namespace) if !ok { return errors.Errorf("Namespace name include unsupported special chars. namespace : [%s]", namespace) } @@ -83,7 +83,7 @@ func validateNamespaceName(tenant, namespace string) error { // and % is allowed as part of valid URL encoding const PATTEN = "^[-=:.\\w]*$" -func checkName(name string) bool { +func CheckName(name string) bool { patten, err := regexp.Compile(PATTEN) if err != nil { return false