mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-10-06 19:07:50 +00:00
JSON parser
This commit is contained in:
parent
6d12478e6b
commit
d416d2c5d2
3 changed files with 90 additions and 11 deletions
33
handler.go
33
handler.go
|
|
@ -115,6 +115,7 @@ func NewRouter(handler *Handler) *mux.Router {
|
|||
router.HandleFunc("/index/{index}/frame/{frame}/restore", handler.handlePostFrameRestore).Methods("POST")
|
||||
router.HandleFunc("/index/{index}/frame/{frame}/time-quantum", handler.handlePatchFrameTimeQuantum).Methods("PATCH")
|
||||
router.HandleFunc("/index/{index}/frame/{frame}/views", handler.handleGetFrameViews).Methods("GET")
|
||||
router.HandleFunc("/index/{index}/input/{input-definition}", handler.handlePostInput).Methods("POST")
|
||||
router.HandleFunc("/index/{index}/input-definition/{input-definition}", handler.handleGetInputDefinition).Methods("GET")
|
||||
router.HandleFunc("/index/{index}/input-definition/{input-definition}", handler.handlePostInputDefinition).Methods("POST")
|
||||
router.HandleFunc("/index/{index}/input-definition/{input-definition}", handler.handleDeleteInputDefinition).Methods("DELETE")
|
||||
|
|
@ -1510,6 +1511,7 @@ func (h *Handler) handlePostInputDefinition(w http.ResponseWriter, r *http.Reque
|
|||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
// Find index.
|
||||
index := h.Holder.Index(indexName)
|
||||
|
|
@ -1603,3 +1605,34 @@ func (h *Handler) handleDeleteInputDefinition(w http.ResponseWriter, r *http.Req
|
|||
}
|
||||
|
||||
type postInputDefinitionResponse struct{}
|
||||
|
||||
func (h *Handler) handlePostInput(w http.ResponseWriter, r *http.Request) {
|
||||
indexName := mux.Vars(r)["index"]
|
||||
inputDefName := mux.Vars(r)["input-definition"]
|
||||
|
||||
// Find index.
|
||||
index := h.Holder.Index(indexName)
|
||||
if index == nil {
|
||||
http.Error(w, ErrIndexNotFound.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// Decode request.
|
||||
var reqs []interface{}
|
||||
err := json.NewDecoder(r.Body).Decode(&reqs)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
for _, req := range reqs {
|
||||
definition := index.inputDefinition(inputDefName)
|
||||
err = index.JSONParser(req.(map[string]interface{}), definition)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
47
index.go
47
index.go
|
|
@ -27,6 +27,7 @@ import (
|
|||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"github.com/pilosa/pilosa/internal"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Default index settings.
|
||||
|
|
@ -720,3 +721,49 @@ func (i *Index) openInputDefinition() error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func (i *Index) JSONParser(req map[string]interface{}, inputDef *InputDefinition) error {
|
||||
fmt.Println(i.Frames)
|
||||
for _, field := range inputDef.fields{
|
||||
if _, ok := req[field.Name]; !ok {
|
||||
return fmt.Errorf("field not found")
|
||||
}
|
||||
for _, action := range field.Actions {
|
||||
switch action.ValueDestination {
|
||||
case "map":
|
||||
err := i.MapAction(action.Frame, action.ValueMap, req[field.Name].(string))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error map value: %s", err)
|
||||
}
|
||||
case "stringToBool":
|
||||
fmt.Println("HERE")
|
||||
err := i.StringToBool(action.Frame, action.RowID, req[field.Name].(bool))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error map value: %s", err)
|
||||
}
|
||||
case "valueToRow":
|
||||
err := i.ValueToRow(action.Frame, field.Name, req[field.Name].(uint64))
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error map value: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
val := reflect.ValueOf(Action{})
|
||||
for i := 0; i < val.Type().NumField(); i++{
|
||||
fmt.Println(val.Type().Field(i).Type)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (i *Index) MapAction(frame string, valueMap map[string]uint64, value string) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Index) StringToBool(frame string, rowID uint64, value bool) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Index) ValueToRow(frame, name string, value uint64) error{
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -94,19 +94,19 @@ func (i *InputDefinition) LoadDefinition(pb *internal.InputDefinition) error {
|
|||
}
|
||||
|
||||
numPrimaryKey := 0
|
||||
countRowID := make(map[uint64]bool)
|
||||
countRowID := make(map[string]uint64)
|
||||
var actions []Action
|
||||
for _, field := range pb.Fields {
|
||||
var actions []Action
|
||||
for _, action := range field.Actions {
|
||||
if err := i.ValidateAction(action); err != nil {
|
||||
return err
|
||||
}
|
||||
if action.RowID != 0 {
|
||||
_, ok := countRowID[action.RowID]
|
||||
if !ok {
|
||||
countRowID[action.RowID] = true
|
||||
} else {
|
||||
if action.RowID != 0 && action.Frame != ""{
|
||||
val, ok := countRowID[action.Frame]
|
||||
if ok && val == action.RowID {
|
||||
return fmt.Errorf("duplicate rowID with other field: %s", action.RowID)
|
||||
} else {
|
||||
countRowID[action.Frame] = action.RowID
|
||||
}
|
||||
}
|
||||
actions = append(actions, Action{
|
||||
|
|
@ -116,7 +116,7 @@ func (i *InputDefinition) LoadDefinition(pb *internal.InputDefinition) error {
|
|||
RowID: action.RowID,
|
||||
})
|
||||
}
|
||||
|
||||
fmt.Println(actions)
|
||||
if field.PrimaryKey {
|
||||
numPrimaryKey += 1
|
||||
}
|
||||
|
|
@ -280,18 +280,17 @@ func (i *InputDefinition) ValidateAction(action *internal.Action) error {
|
|||
if _, ok := validValues[action.ValueDestination]; !ok {
|
||||
return fmt.Errorf("invalid ValueDestination: %s", action.ValueDestination)
|
||||
}
|
||||
|
||||
fmt.Println("ACTION", action.ValueDestination)
|
||||
switch action.ValueDestination {
|
||||
case "map":
|
||||
if len(action.ValueMap) == 0 {
|
||||
return errors.New("valueMap required for map")
|
||||
}
|
||||
case "stringToBool":
|
||||
fmt.Println("HERR", action.RowID)
|
||||
if action.RowID == 0 {
|
||||
return errors.New("rowID required for stringToBool")
|
||||
}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue