Replace validation error with regex for cockroach namespace (#1270)

Co-authored-by: Asim Aslam <asim@aslam.me>
This commit is contained in:
ben-toogood 2020-03-01 22:09:06 +00:00 committed by GitHub
parent d8377e09c9
commit 9200c70202
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,9 +5,9 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"net/url" "net/url"
"regexp"
"strings" "strings"
"time" "time"
"unicode"
"github.com/lib/pq" "github.com/lib/pq"
log "github.com/micro/go-micro/v2/logger" log "github.com/micro/go-micro/v2/logger"
@ -206,16 +206,17 @@ func (s *sqlStore) configure() error {
prefix = DefaultPrefix prefix = DefaultPrefix
} }
for _, r := range namespace { // store.namespace must only contain letters
if !unicode.IsLetter(r) { reg, err := regexp.Compile("[^a-zA-Z0-9]+")
return errors.New("store.namespace must only contain letters") if err != nil {
} return errors.New("error compiling regex for namespace")
} }
namespace = reg.ReplaceAllString(namespace, "")
source := nodes[0] source := nodes[0]
// check if it is a standard connection string eg: host=%s port=%d user=%s password=%s dbname=%s sslmode=disable // 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 // 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 err != nil {
if !strings.Contains(source, " ") { if !strings.Contains(source, " ") {
source = fmt.Sprintf("host=%s", source) source = fmt.Sprintf("host=%s", source)