From c6a502efed6f089306f821b4452c6d8ea31efd79 Mon Sep 17 00:00:00 2001 From: Linh Vo Date: Mon, 6 Mar 2017 11:03:17 -0600 Subject: [PATCH] fix review --- client.go | 10 ---------- db.go | 6 +++--- frame.go | 6 +++--- pilosa.go | 15 ++++----------- 4 files changed, 10 insertions(+), 27 deletions(-) diff --git a/client.go b/client.go index bfe4dc57e..e7d0eaf85 100644 --- a/client.go +++ b/client.go @@ -194,11 +194,6 @@ func (c *Client) ExecuteQuery(ctx context.Context, db, query string, allowRedire return nil, ErrQueryRequired } - er := ValidateName(db) - if er != nil { - return nil, ErrName - } - // Encode query request. buf, err := proto.Marshal(&internal.QueryRequest{ DB: db, @@ -255,11 +250,6 @@ func (c *Client) ExecutePQL(ctx context.Context, db, query string) (interface{}, }.Encode(), } - er := ValidateName(db) - if er != nil { - return nil, ErrName - } - req, err := http.NewRequest("POST", u.String(), bytes.NewReader([]byte(query))) if err != nil { return nil, err diff --git a/db.go b/db.go index 9348c97b6..6ad81e16b 100644 --- a/db.go +++ b/db.go @@ -49,9 +49,9 @@ type DB struct { // NewDB returns a new instance of DB. func NewDB(path, name string) (*DB, error) { - err := ValidateName(name) - if err != nil { - return nil, err + validName := Exp.FindStringSubmatchIndex(name) + if len(validName) == 0 { + return nil, ErrName } return &DB{ diff --git a/frame.go b/frame.go index fa47517c5..368cde821 100644 --- a/frame.go +++ b/frame.go @@ -47,9 +47,9 @@ type Frame struct { // NewFrame returns a new instance of frame. func NewFrame(path, db, name string) (*Frame, error) { - err := ValidateName(name) - if err != nil { - return nil, err + validName := Exp.FindStringSubmatchIndex(name) + if len(validName) == 0 { + return nil, ErrName } return &Frame{ diff --git a/pilosa.go b/pilosa.go index 14e5ef203..b3cee7450 100644 --- a/pilosa.go +++ b/pilosa.go @@ -20,13 +20,16 @@ var ( ErrFrameNotFound = errors.New("frame not found") // ErrFrameRequired is returned when no frame is specified. - ErrName = errors.New("name restricted to [a-z0-9_-.]") + ErrName = errors.New("name restricted to [a-z0-9_-]") // ErrFragmentNotFound is returned when a fragment does not exist. ErrFragmentNotFound = errors.New("fragment not found") ErrQueryRequired = errors.New("query required") ) +// Regular expression to valuate db and frame's name +var Exp = regexp.MustCompile(`^([a-z0-9._-]{1,64}$)`) + // Profile represents vertical column in a database. // A profile can have a set of attributes attached to it. type Profile struct { @@ -79,13 +82,3 @@ func decodeProfile(pb *internal.Profile) *Profile { // TimeFormat is the go-style time format used to parse string dates. const TimeFormat = "2006-01-02T15:04" - -// Restrict name using regex -func ValidateName(name string) error { - expr := regexp.MustCompile(`^([a-z0-9._-]{1,64}$)`) - validName := expr.FindStringSubmatchIndex(name) - if len(validName) == 0 { - return ErrName - } - return nil -}