From 66478e04fb0d28d69edb9df67bd3379e6da1f061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Mon, 13 Nov 2017 10:22:13 +0100 Subject: [PATCH] payload now returns type --- json_utility.go | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/json_utility.go b/json_utility.go index 6f82409..4f66a9b 100644 --- a/json_utility.go +++ b/json_utility.go @@ -3,6 +3,7 @@ package webutility import ( "encoding/json" "errors" + "fmt" "io" "net/http" "sync" @@ -11,7 +12,7 @@ import ( ) var mu = &sync.Mutex{} -var payloads []payloadBuff +var payloads []Payload type LangMap map[string]map[string]string @@ -33,20 +34,8 @@ type Translation struct { FieldsLabels map[string]string `json:"fieldsLabels"` } -type payloadBuff struct { - Type string `json:"tableType"` - Method string `json:"method"` - Params map[string]string `json:"params"` - Lang []Translation `json:"lang"` - Fields []Field `json:"fields"` - Correlations []CorrelationField `json:"correlationFields"` - IdField string `json:"idField"` - - // Data can only hold slices of any type. It can't be used for itteration - Data interface{} `json:"data"` -} - type Payload struct { + Type string `json:"type"` Method string `json:"method"` Params map[string]string `json:"params"` Lang []Translation `json:"lang"` @@ -54,8 +43,7 @@ type Payload struct { Correlations []CorrelationField `json:"correlationFields"` IdField string `json:"idField"` - // Data contains JSON payload. - // It can't be used for itteration + // Data holds JSON payload. It can't be used for itteration. Data interface{} `json:"data"` } @@ -79,7 +67,7 @@ func InitTables(db *ora.Ses, project string) error { // ReloadTables reloads all payloads in the payloads variable. // Returns an error if it fails. func ReloadTables(db *ora.Ses, project string) error { - payloads = make([]payloadBuff, 0) + payloads = make([]Payload, 0) return InitTables(db, project) } @@ -93,8 +81,8 @@ func DecodeJSON(r io.Reader, v interface{}) error { func NewPayload(r *http.Request, table string) Payload { var pload Payload - //pload.Method = r.Method + " " + r.URL.Path pload.Method = r.Method + " " + r.RequestURI + pload.Type = table if table != "" { pload.Params = make(map[string]string, 0) pload.Lang = translations(table) @@ -105,11 +93,10 @@ func NewPayload(r *http.Request, table string) Payload { return pload } -// MakePayload returs a payload for provided table with populated Data field. -func MakePayload(r *http.Request, table string, data interface{}) Payload { +// BuildPayload returs a payload for provided table with populated Data field. +func BuildPayload(r *http.Request, table string, data interface{}) Payload { var pload Payload - //pload.Method = r.Method + " " + r.URL.Path pload.Method = r.Method + " " + r.RequestURI if table != "" { pload.Params = make(map[string]string, 0) @@ -190,7 +177,7 @@ func correlations(ptype string) []CorrelationField { // Returns an error if it fails. func fetchJSON(db *ora.Ses, project string) ([]byte, error) { db.SetCfg(db.Cfg().SetClob(ora.S)) - stmt, err := db.Prep(`SELECT JSON_NCLOB FROM TABLES_CONFIG WHERE PROJEKAT`+EqualQuotes(project), ora.S) + stmt, err := db.Prep(`SELECT JSON_NCLOB FROM TABLES_CONFIG WHERE PROJEKAT = `+fmt.Sprintf("'%s'", project), ora.S) defer stmt.Close() if err != nil { return nil, err -- 1.8.1.2