From c441e55bb2acc9b760a7936fa3d3ff547614cdb0 Mon Sep 17 00:00:00 2001 From: Michael Baird Date: Mon, 26 Jun 2017 14:56:27 -0500 Subject: [PATCH] Prefaced validValueDestination values Code cleanup --- handler.go | 2 +- input_definition.go | 31 +++++++++++++++++-------------- input_definition_test.go | 18 +++++++++--------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/handler.go b/handler.go index 7fae39217..71d6c25ef 100644 --- a/handler.go +++ b/handler.go @@ -1665,7 +1665,7 @@ func (h *Handler) JSONParser(req map[string]interface{}, index *Index, name stri columnLabel = field.Name } } - for key, _ := range req { + for key := range req { _, ok := validFields[key] if !ok { return nil, fmt.Errorf("field not found: %s", key) diff --git a/input_definition.go b/input_definition.go index 47236b1cf..56b5d7705 100644 --- a/input_definition.go +++ b/input_definition.go @@ -26,13 +26,14 @@ import ( "github.com/pilosa/pilosa/internal" ) +// Action Mapping types const ( - Mapping = "mapping" - ValueToRow = "value-to-row" - SingleRowBool = "single-row-boolean" + InputMapping = "mapping" + InputValueToRow = "value-to-row" + InputSingleRowBool = "single-row-boolean" ) -var ValidValueDestination = []string{Mapping, ValueToRow, SingleRowBool} +var validValueDestination = []string{InputMapping, InputValueToRow, InputSingleRowBool} // InputDefinition represents a container for the data input definition. type InputDefinition struct { @@ -108,13 +109,12 @@ func (i *InputDefinition) LoadDefinition(pb *internal.InputDefinition) error { if err := i.ValidateAction(action); err != nil { return err } - if action.ValueDestination == SingleRowBool && action.Frame != "" { + if action.ValueDestination == InputSingleRowBool && action.Frame != "" { val, ok := countRowID[action.Frame] if ok && val == action.RowID { return fmt.Errorf("duplicate rowID with other field: %v", action.RowID) - } else { - countRowID[action.Frame] = action.RowID } + countRowID[action.Frame] = action.RowID } actions = append(actions, Action{ Frame: action.Frame, @@ -124,7 +124,7 @@ func (i *InputDefinition) LoadDefinition(pb *internal.InputDefinition) error { }) } if field.PrimaryKey { - numPrimaryKey += 1 + numPrimaryKey++ } if numPrimaryKey > 1 { @@ -289,6 +289,7 @@ func (i *InputDefinitionInfo) Encode() (*internal.InputDefinition, error) { return &def, nil } +// AddFrame manually add frame to input definition. func (i *InputDefinition) AddFrame(frame InputFrame) error { i.frames = append(i.frames, frame) if err := i.saveMeta(); err != nil { @@ -296,19 +297,21 @@ func (i *InputDefinition) AddFrame(frame InputFrame) error { } return nil } + +// ValidateAction ensures the input definition action conforms to our specification. func (i *InputDefinition) ValidateAction(action *internal.InputDefinitionAction) error { if action.Frame == "" { return ErrFrameRequired } validValues := make(map[string]bool) - for _, val := range ValidValueDestination { + for _, val := range validValueDestination { validValues[val] = true } if _, ok := validValues[action.ValueDestination]; !ok { return fmt.Errorf("invalid ValueDestination: %s", action.ValueDestination) } switch action.ValueDestination { - case Mapping: + case InputMapping: if len(action.ValueMap) == 0 { return errors.New("valueMap required for map") } @@ -319,14 +322,14 @@ func (i *InputDefinition) ValidateAction(action *internal.InputDefinitionAction) // HandleAction Process the input data with its action and return a bit to be imported later // Note: if the Bit should not be set then nil is returned with no error // From the JSON marshalling the possible types are: float64, boolean, string -// TODO handle Timestams +// TODO handle Timestamps func HandleAction(a Action, value interface{}, colID uint64) (*Bit, error) { var err error var bit Bit bit.ColumnID = colID switch a.ValueDestination { - case Mapping: + case InputMapping: v, ok := value.(string) if !ok { return nil, fmt.Errorf("Mapping value must be a string %v", value) @@ -335,7 +338,7 @@ func HandleAction(a Action, value interface{}, colID uint64) (*Bit, error) { if !ok { return nil, fmt.Errorf("Value %s does not exist in definition map", v) } - case SingleRowBool: + case InputSingleRowBool: switch value.(type) { case bool: if value.(bool) { @@ -352,7 +355,7 @@ func HandleAction(a Action, value interface{}, colID uint64) (*Bit, error) { default: return nil, fmt.Errorf("single-row-boolean value %v must equate to a Bool", value) } - case ValueToRow: + case InputValueToRow: v, ok := value.(float64) if !ok { return nil, fmt.Errorf("value-to-row value must equate to an integer %v", value) diff --git a/input_definition_test.go b/input_definition_test.go index 0e078536e..8adcbc1cc 100644 --- a/input_definition_test.go +++ b/input_definition_test.go @@ -114,13 +114,13 @@ func TestInputDefinition_LoadDefinition(t *testing.T) { t.Fatalf("Expected invalid ValueDestination error, actual error: %s", err) } - act := pilosa.Action{Frame: "f", ValueDestination: pilosa.SingleRowBool, ValueMap: map[string]uint64{"Green": 1}} + act := pilosa.Action{Frame: "f", ValueDestination: pilosa.InputSingleRowBool, ValueMap: map[string]uint64{"Green": 1}} _, err = act.Encode() if !strings.Contains(err.Error(), "rowID required for single-row-boolean") { t.Fatalf("Expected rowID required for single-row-boolean error, actual error: %s", err) } - action = internal.InputDefinitionAction{Frame: "f", ValueDestination: pilosa.Mapping, RowID: 100} + action = internal.InputDefinitionAction{Frame: "f", ValueDestination: pilosa.InputMapping, RowID: 100} field = internal.InputDefinitionField{Name: "id", PrimaryKey: true, InputDefinitionActions: []*internal.InputDefinitionAction{&action}} def = &internal.InputDefinition{Name: "test", Frames: []*internal.Frame{&frames}, Fields: []*internal.InputDefinitionField{&field}} err = input.LoadDefinition(def) @@ -128,8 +128,8 @@ func TestInputDefinition_LoadDefinition(t *testing.T) { t.Fatalf("Expected valueMap required for map error, actual error: %s", err) } - action = internal.InputDefinitionAction{Frame: "f", ValueDestination: pilosa.SingleRowBool, RowID: 100} - action1 := internal.InputDefinitionAction{Frame: "f", ValueDestination: pilosa.SingleRowBool, RowID: 0} + action = internal.InputDefinitionAction{Frame: "f", ValueDestination: pilosa.InputSingleRowBool, RowID: 100} + action1 := internal.InputDefinitionAction{Frame: "f", ValueDestination: pilosa.InputSingleRowBool, RowID: 0} field1 := internal.InputDefinitionField{Name: "newID", PrimaryKey: true, InputDefinitionActions: []*internal.InputDefinitionAction{&action1}} def = &internal.InputDefinition{Name: "test", Frames: []*internal.Frame{&frames}, Fields: []*internal.InputDefinitionField{&field, &field1}} err = input.LoadDefinition(def) @@ -137,7 +137,7 @@ func TestInputDefinition_LoadDefinition(t *testing.T) { t.Fatalf("Expected duplicate primaryKey error, actual error: %s", err) } - action1 = internal.InputDefinitionAction{Frame: "f", ValueDestination: pilosa.SingleRowBool, RowID: 100} + action1 = internal.InputDefinitionAction{Frame: "f", ValueDestination: pilosa.InputSingleRowBool, RowID: 100} field1 = internal.InputDefinitionField{Name: "id", PrimaryKey: true, InputDefinitionActions: []*internal.InputDefinitionAction{&action1}} def = &internal.InputDefinition{Name: "test", Frames: []*internal.Frame{&frames}, Fields: []*internal.InputDefinitionField{&field, &field1}} err = input.LoadDefinition(def) @@ -145,7 +145,7 @@ func TestInputDefinition_LoadDefinition(t *testing.T) { t.Fatalf("Expected duplicate rowID with other field error, actual error: %s", err) } - action = internal.InputDefinitionAction{ValueDestination: pilosa.SingleRowBool, RowID: 100} + action = internal.InputDefinitionAction{ValueDestination: pilosa.InputSingleRowBool, RowID: 100} def = &internal.InputDefinition{Name: "test", Frames: []*internal.Frame{&frames}, Fields: []*internal.InputDefinitionField{&field}} err = input.LoadDefinition(def) if !strings.Contains(err.Error(), "frame required") { @@ -157,7 +157,7 @@ func TestHandleAction(t *testing.T) { var value interface{} colID := uint64(0) rowID := uint64(100) - action := pilosa.Action{ValueDestination: pilosa.SingleRowBool, RowID: &rowID} + action := pilosa.Action{ValueDestination: pilosa.InputSingleRowBool, RowID: &rowID} value = 1 b, err := pilosa.HandleAction(action, value, colID) @@ -206,7 +206,7 @@ func TestHandleAction(t *testing.T) { } } - action.ValueDestination = pilosa.ValueToRow + action.ValueDestination = pilosa.InputValueToRow rowID = 101 value = float64(25.0) b, err = pilosa.HandleAction(action, value, colID) @@ -221,7 +221,7 @@ func TestHandleAction(t *testing.T) { t.Fatalf("Expected Ignore values that are not type float64") } - action.ValueDestination = pilosa.Mapping + action.ValueDestination = pilosa.InputMapping value = "test" b, err = pilosa.HandleAction(action, value, colID) if b != nil {