From 9200c7020247b1eab6227747da8baff22b7490e7 Mon Sep 17 00:00:00 2001 From: ben-toogood Date: Sun, 1 Mar 2020 22:09:06 +0000 Subject: [PATCH] Replace validation error with regex for cockroach namespace (#1270) Co-authored-by: Asim Aslam --- store/cockroach/cockroach.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/store/cockroach/cockroach.go b/store/cockroach/cockroach.go index 0e0dc38e..63e16f15 100644 --- a/store/cockroach/cockroach.go +++ b/store/cockroach/cockroach.go @@ -5,9 +5,9 @@ import ( "database/sql" "fmt" "net/url" + "regexp" "strings" "time" - "unicode" "github.com/lib/pq" log "github.com/micro/go-micro/v2/logger" @@ -206,16 +206,17 @@ func (s *sqlStore) configure() error { prefix = DefaultPrefix } - for _, r := range namespace { - if !unicode.IsLetter(r) { - return errors.New("store.namespace must only contain letters") - } + // store.namespace must only contain letters + reg, err := regexp.Compile("[^a-zA-Z0-9]+") + if err != nil { + return errors.New("error compiling regex for namespace") } + namespace = reg.ReplaceAllString(namespace, "") source := nodes[0] // check if it is a standard connection string eg: host=%s port=%d user=%s password=%s dbname=%s sslmode=disable // if err is nil which means it would be a URL like postgre://xxxx?yy=zz - _, err := url.Parse(source) + _, err = url.Parse(source) if err != nil { if !strings.Contains(source, " ") { source = fmt.Sprintf("host=%s", source)