mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-07 17:15:56 +00:00
remove duplicate code, move ValidateName out of validateOptions
This commit is contained in:
parent
b2db339741
commit
9abdb29269
4 changed files with 25 additions and 20 deletions
11
db.go
11
db.go
|
|
@ -86,6 +86,11 @@ func (db *DB) SetColumnLabel(v string) error {
|
|||
db.mu.Lock()
|
||||
defer db.mu.Unlock()
|
||||
|
||||
// Make sure columnLabel is valid name
|
||||
err := ValidateName(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Ignore if no change occurred.
|
||||
if v == "" || db.columnLabel == v {
|
||||
return nil
|
||||
|
|
@ -369,6 +374,12 @@ func (db *DB) createFrame(name string, opt FrameOptions) (*Frame, error) {
|
|||
}
|
||||
|
||||
// Update options.
|
||||
if opt.RowLabel != "" {
|
||||
err := ValidateName(opt.RowLabel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
f.SetRowLabel(opt.RowLabel)
|
||||
|
||||
// Add to database's frame lookup.
|
||||
|
|
|
|||
5
frame.go
5
frame.go
|
|
@ -110,6 +110,11 @@ func (f *Frame) SetRowLabel(v string) error {
|
|||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
// Make sure rowLabel is valid name
|
||||
err := ValidateName(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Ignore if no change occurred.
|
||||
if v == "" || f.rowLabel == v {
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ func TestPostDBRequestUnmarshalJSON(t *testing.T) {
|
|||
{json: `{"db": "d", "option": {}}`, err: "Unknown key: option:map[]"},
|
||||
{json: `{"db": "d", "options": {"columnLabel": "test"}}`, expected: postDBRequest{DB: "d", Options: DBOptions{ColumnLabel: "test"}}},
|
||||
{json: `{"db": "d", "options": {"columnLabl": "test"}}`, err: "invalid key for options {columnLabl:test}"},
|
||||
{json: `{"db": "d", "options": {"columnLabel": "////"}}`, err: "invalid columnLabel value: ////"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
actual := &postDBRequest{}
|
||||
|
|
@ -52,12 +51,11 @@ func TestPostFrameRequestUnmarshalJSON(t *testing.T) {
|
|||
err string
|
||||
}{
|
||||
{json: `{"db": "d", "frame":"f", "options": {}}`, expected: postFrameRequest{DB: "d", Frame: "f", Options: FrameOptions{}}},
|
||||
{json: `{"db": "d", "options": {}}`, err: "db required and must be a string"},
|
||||
{json: `{"db": "d", "options": {}}`, err: "frame required and must be a string"},
|
||||
{json: `{"db": "d", "frame":"f", "options": 4}`, err: "options is not map[string]interface{}"},
|
||||
{json: `{"db": "d", "frame":"f", "option": {}}`, err: "Unknown key: {option:map[]}"},
|
||||
{json: `{"db": "d", "frame":"f", "options": {"rowLabel": "test"}}`, expected: postFrameRequest{DB: "d", Frame: "f", Options: FrameOptions{RowLabel: "test"}}},
|
||||
{json: `{"db": "d", "frame":"f", "options": {"rowLabl": "test"}}`, err: "invalid key for options {rowLabl:test}"},
|
||||
{json: `{"db": "d", "frame":"f", "options": {"rowLabel": "////"}}`, err: "invalid rowLabel value: ////"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
actual := &postFrameRequest{}
|
||||
|
|
|
|||
25
handler.go
25
handler.go
|
|
@ -359,11 +359,7 @@ func (p *postDBRequest) UnmarshalJSON(b []byte) error {
|
|||
}
|
||||
p.DB = val
|
||||
case "options":
|
||||
options, ok := data["options"].(map[string]interface{})
|
||||
if !ok {
|
||||
return errors.New("options is not map[string]interface{}")
|
||||
}
|
||||
value, err := validateOptions(options, "columnLabel")
|
||||
value, err := validateOptions(data, "columnLabel")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -380,7 +376,11 @@ func (p *postDBRequest) UnmarshalJSON(b []byte) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func validateOptions(options map[string]interface{}, field string) (string, error) {
|
||||
func validateOptions(data map[string]interface{}, field string) (string, error) {
|
||||
options, ok := data["options"].(map[string]interface{})
|
||||
if !ok {
|
||||
return "", errors.New("options is not map[string]interface{}")
|
||||
}
|
||||
var optionValue string
|
||||
if len(options) == 0 {
|
||||
optionValue = ""
|
||||
|
|
@ -392,12 +392,7 @@ func validateOptions(options map[string]interface{}, field string) (string, erro
|
|||
if !ok {
|
||||
return "", fmt.Errorf("invalid option %v: {%v:%v}", field, k, v)
|
||||
}
|
||||
err := ValidateName(val)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("invalid %v value: %v", field, v)
|
||||
|
||||
}
|
||||
optionValue = options[field].(string)
|
||||
optionValue = val
|
||||
default:
|
||||
return "", fmt.Errorf("invalid key for options {%v:%v}", k, v)
|
||||
}
|
||||
|
|
@ -593,11 +588,7 @@ func (p *postFrameRequest) UnmarshalJSON(b []byte) error {
|
|||
p.Frame = val
|
||||
|
||||
case "options":
|
||||
options, ok := data["options"].(map[string]interface{})
|
||||
if !ok {
|
||||
return errors.New("options is not map[string]interface{}")
|
||||
}
|
||||
value, err := validateOptions(options, "rowLabel")
|
||||
value, err := validateOptions(data, "rowLabel")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue