From d66628295471b832e377f7a6867a083f9f771b82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Mon, 9 Oct 2017 15:24:05 +0200 Subject: [PATCH] cleaned up --- http_utility.go | 15 ++++++++------ json_utility.go | 52 +++++++++++++++++++++++------------------------ list_config.go | 62 +++++++++++++++++++++++++++----------------------------- select_config.go | 1 - sql_sequrity.go | 4 +--- 5 files changed, 65 insertions(+), 69 deletions(-) diff --git a/http_utility.go b/http_utility.go index 2fc3b2d..c6df81a 100644 --- a/http_utility.go +++ b/http_utility.go @@ -5,12 +5,14 @@ import ( "net/http" ) -const templateHttpErr500_EN = "An internal server error has occurred." -const templateHttpErr500_RS = "Došlo je do greške na serveru." -const templateHttpErr400_EN = "Bad request: invalid request body." -const templateHttpErr400_RS = "Neispravan zahtev." -const templateHttpErr401_EN = "Unauthorized request." -const templateHttpErr401_RS = "Neautorizovan zahtev." +const ( + templateHttpErr500_EN = "An internal server error has occurred." + templateHttpErr500_RS = "Došlo je do greške na serveru." + templateHttpErr400_EN = "Bad request: invalid request body." + templateHttpErr400_RS = "Neispravan zahtev." + templateHttpErr401_EN = "Unauthorized request." + templateHttpErr401_RS = "Neautorizovan zahtev." +) type httpError struct { Error []HttpErrorDesc `json:"error"` @@ -65,6 +67,7 @@ func NotFoundHandler(w http.ResponseWriter, req *http.Request) { }) } +// SetDefaultHeaders set's default headers for an HTTP response. func SetDefaultHeaders(w http.ResponseWriter) { w.Header().Set("Access-Control-Allow-Origin", "*") diff --git a/json_utility.go b/json_utility.go index f178804..da294a9 100644 --- a/json_utility.go +++ b/json_utility.go @@ -1,15 +1,12 @@ package webutility import ( - //"fmt" "encoding/json" "errors" "io" "net/http" - //"io/ioutil" "sync" - //"gopkg.in/rana/ora.v3" "gopkg.in/rana/ora.v4" ) @@ -57,10 +54,34 @@ type Payload struct { Correlations []CorrelationField `json:"correlationFields"` IdField string `json:"idField"` - // Data can only hold slices of any type. It can't be used for itteration + // Data contains JSON payload. + // It can't be used for itteration Data interface{} `json:"data"` } +// InitTables loads all payloads in the payloads variable. +// Returns an error if it fails. +func InitTables(db *ora.Ses, project string) error { + jsonbuf, err := fetchJSON(db, project) + if err != nil { + return err + } + + mu.Lock() + defer mu.Unlock() + json.Unmarshal(jsonbuf, &payloads) + if len(payloads) == 0 { + return errors.New("tables config is corrupt") + } + return nil +} + +// DecodeJSON decodes JSON data from r to v. +// Returns an error if it fails. +func DecodeJSON(r io.Reader, v interface{}) error { + return json.NewDecoder(r).Decode(v) +} + // NewPayload returs a payload sceleton for provided table. func NewPayload(r *http.Request, table string) Payload { var pload Payload @@ -140,23 +161,6 @@ func correlations(ptype string) []CorrelationField { return corr } -// InitTables loads all payloads in the payloads variable. -// Returns an error if it fails. -func InitTables(db *ora.Ses, project string) error { - jsonbuf, err := fetchJSON(db, project) - if err != nil { - return err - } - - mu.Lock() - defer mu.Unlock() - json.Unmarshal(jsonbuf, &payloads) - if len(payloads) == 0 { - return errors.New("tables config is corrupt") - } - return nil -} - // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table. // Returns an error if it fails. func fetchJSON(db *ora.Ses, project string) ([]byte, error) { @@ -180,9 +184,3 @@ func fetchJSON(db *ora.Ses, project string) ([]byte, error) { //fmt.Println(data) return []byte(data), nil } - -// DecodeJSON decodes JSON data from r to v. -// Returns an error if it fails. -func DecodeJSON(r io.Reader, v interface{}) error { - return json.NewDecoder(r).Decode(v) -} diff --git a/list_config.go b/list_config.go index ef2c302..f7b8af5 100644 --- a/list_config.go +++ b/list_config.go @@ -1,6 +1,5 @@ package webutility -//import "gopkg.in/rana/ora.v3" import "gopkg.in/rana/ora.v4" type ListOptions struct { @@ -29,15 +28,14 @@ type Dropdown struct { FiltersField string `json:"filtersField"` IDField string `json:"idField"` LabelField string `json:"labelField"` - } type ListGraph struct { ObjectType string `json:"objectType"` - X string `json:"xField"` - Y string `json:"yField"` + X string `json:"xField"` + Y string `json:"yField"` GroupField string `json:"groupField"` - Label string `json:"label"` + Label string `json:"label"` } type ListActions struct { @@ -126,7 +124,7 @@ func GetListConfigObjectIDField(db *ora.Ses, otype string) string { stmt, err = db.Prep(`SELECT ID_FIELD FROM LIST_CONFIG_ID_FIELD - WHERE OBJECT_TYPE = '` + otype + `'`, + WHERE OBJECT_TYPE = '`+otype+`'`, ora.S) defer stmt.Close() @@ -151,14 +149,14 @@ func GetListConfigObjectIDField(db *ora.Ses, otype string) string { func newDefaultList(objType string) ListConfig { list := ListConfig{ ObjectType: objType, - Title: objType, - LazyLoad: false, + Title: objType, + LazyLoad: false, Options: ListOptions{ - GlobalFilter: true, - LocalFilters: true, + GlobalFilter: true, + LocalFilters: true, RemoteFilters: false, - Pagination: true, - PageSize: 20, + Pagination: true, + PageSize: 20, }, Filters: nil, Actions: ListActions{ @@ -166,10 +164,10 @@ func newDefaultList(objType string) ListConfig { Update: false, Delete: false, Export: false, - Print: false, - Graph: false, + Print: false, + Graph: false, }, - Parent: nil, + Parent: nil, Navigation: nil, } @@ -223,7 +221,7 @@ func getListNavigation(db *ora.Ses, listObjType string) ([]ListNavNode, error) { a.OBJECT_TYPE, a.PARENT_OBJECT_TYPE, a.LABEL, a.ICON, a.PARENT_FILTER_FIELD, b.PARENT_ID_FIELD, b.RB FROM LIST_CONFIG_NAVIGATION b JOIN LIST_CONFIG_CHILD a ON b.PARENT_CHILD_ID = a.PARENT_CHILD_ID - WHERE b.LIST_OBJECT_TYPE = '`+listObjType+`' + WHERE b.LIST_OBJECT_TYPE = '` + listObjType + `' ORDER BY b.RB ASC` stmt, err = db.Prep(query, ora.S, ora.S, ora.S, ora.S, ora.S) @@ -238,12 +236,12 @@ func getListNavigation(db *ora.Ses, listObjType string) ([]ListNavNode, error) { } for rset.Next() { resp = append(resp, ListNavNode{ - ObjectType: rset.Row[0].(string), - ParentObjectType: rset.Row[1].(string), - LabelField: rset.Row[2].(string), - Icon: rset.Row[3].(string), + ObjectType: rset.Row[0].(string), + ParentObjectType: rset.Row[1].(string), + LabelField: rset.Row[2].(string), + Icon: rset.Row[3].(string), ParentFilterField: rset.Row[4].(string), - ParentIDField: rset.Row[5].(string), + ParentIDField: rset.Row[5].(string), // RB is ignored }) } @@ -379,8 +377,8 @@ func getFiltersByFilterField(db *ora.Ses, filtersField string) ([]_filter, error } for rset.Next() { resp = append(resp, _filter{ - Type: rset.Row[0].(string), - Label: rset.Row[1].(string), + Type: rset.Row[0].(string), + Label: rset.Row[1].(string), DefaultValues: rset.Row[2].(string), }) } @@ -428,7 +426,7 @@ func sortFilters(filters []ListFilter) { var temp ListFilter for !done { done = true - for i := 0; i < len(filters) - 1; i++ { + for i := 0; i < len(filters)-1; i++ { if filters[i].Position > filters[i+1].Position { done = false temp = filters[i] @@ -462,10 +460,10 @@ func getListGraph(db *ora.Ses, objType string) ([]ListGraph, error) { for rset.Next() { resp = append(resp, ListGraph{ ObjectType: rset.Row[0].(string), - X: rset.Row[1].(string), - Y: rset.Row[2].(string), + X: rset.Row[1].(string), + Y: rset.Row[2].(string), GroupField: rset.Row[3].(string), - Label: rset.Row[4].(string), + Label: rset.Row[4].(string), }) } if rset.Err() != nil { @@ -534,8 +532,8 @@ func getListParent(db *ora.Ses, objType string) ([]ListParentNode, error) { } for rset.Next() { resp = append(resp, ListParentNode{ - ObjectType: rset.Row[0].(string), - LabelField: rset.Row[1].(string), + ObjectType: rset.Row[0].(string), + LabelField: rset.Row[1].(string), FilterField: rset.Row[2].(string), }) } @@ -568,10 +566,10 @@ func getListPivot(db *ora.Ses, objType string) ([]ListPivot, error) { } for rset.Next() { resp = append(resp, ListPivot{ - ObjectType: rset.Row[0].(string), - GroupField: rset.Row[1].(string), + ObjectType: rset.Row[0].(string), + GroupField: rset.Row[1].(string), DistinctField: rset.Row[2].(string), - Value: rset.Row[3].(string), + Value: rset.Row[3].(string), }) } if rset.Err() != nil { diff --git a/select_config.go b/select_config.go index 680acdb..7797503 100644 --- a/select_config.go +++ b/select_config.go @@ -1,6 +1,5 @@ package webutility -//import "gopkg.in/rana/ora.v3" import "gopkg.in/rana/ora.v4" type SelectConfig struct { diff --git a/sql_sequrity.go b/sql_sequrity.go index 6ddecb4..f0fd689 100644 --- a/sql_sequrity.go +++ b/sql_sequrity.go @@ -1,8 +1,6 @@ package webutility -import ( - "strings" -) +import "strings" var patern string = "\"';&*<>=\\`:" -- 1.8.1.2