wrap regexp in function

This commit is contained in:
Linh Vo 2017-03-06 12:31:51 -06:00
parent 28580fd354
commit 4e31d02979
3 changed files with 18 additions and 7 deletions

6
db.go
View file

@ -49,9 +49,9 @@ type DB struct {
// NewDB returns a new instance of DB.
func NewDB(path, name string) (*DB, error) {
validName := Exp.FindStringSubmatchIndex(name)
if len(validName) == 0 {
return nil, ErrName
err := ValidateName(name)
if err != nil {
return nil, err
}
return &DB{

View file

@ -47,9 +47,9 @@ type Frame struct {
// NewFrame returns a new instance of frame.
func NewFrame(path, db, name string) (*Frame, error) {
validName := Exp.FindStringSubmatchIndex(name)
if len(validName) == 0 {
return nil, ErrName
err := ValidateName(name)
if err != nil {
return nil, err
}
return &Frame{

View file

@ -28,7 +28,8 @@ var (
)
// Regular expression to valuate db and frame's name
var Exp = regexp.MustCompile(`^([a-z0-9._-]{1,64}$)`)
// Todo: remove . when frame doesn't require . for topN
var nameRegexp = 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.
@ -82,3 +83,13 @@ 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 {
validName := nameRegexp.Match([]byte(name))
if validName == false{
return ErrName
}
return nil
}