Blame view
json_utility.go
4.37 KB
ea858b8a7 refactoring |
1 |
package webutility |
64041a2ea first commit |
2 3 4 |
import ( "net/http" |
8dbe745c3 merged tables uti... |
5 6 |
"encoding/json" "errors" |
8dbe745c3 merged tables uti... |
7 |
"io" |
1d0f61553 can't fetch clob |
8 |
//"io/ioutil" |
17a4d0447 mutex lock for pa... |
9 |
"sync" |
1d0f61553 can't fetch clob |
10 11 12 |
//"gopkg.in/rana/ora.v3" "gopkg.in/rana/ora.v4" |
64041a2ea first commit |
13 |
) |
17a4d0447 mutex lock for pa... |
14 |
var mu = &sync.Mutex{} |
6ec91280b working on docume... |
15 |
var payloads []payloadBuff |
8dbe745c3 merged tables uti... |
16 |
|
64041a2ea first commit |
17 18 19 20 21 22 23 24 |
type LangMap map[string]map[string]string type Field struct { Parameter string `json:"param"` Type string `json:"type"` Visible bool `json:"visible"` Editable bool `json:"editable"` } |
8dbe745c3 merged tables uti... |
25 26 27 28 29 30 31 |
type CorrelationField struct { Result string `json:"result"` Elements []string `json:"elements"` Type string `json:"type"` } type Translation struct { |
ecec68b18 updated todo list |
32 |
Language string `json:"language"` |
8dbe745c3 merged tables uti... |
33 |
FieldsLabels map[string]string `json:"fieldsLabels"` |
64041a2ea first commit |
34 |
} |
4a51e54d7 simplified |
35 |
type payloadBuff struct { |
8dbe745c3 merged tables uti... |
36 |
Type string `json:"tableType"` |
64041a2ea first commit |
37 38 |
Method string `json:"method"` Params map[string]string `json:"params"` |
8dbe745c3 merged tables uti... |
39 |
Lang []Translation `json:"lang"` |
64041a2ea first commit |
40 |
Fields []Field `json:"fields"` |
c430f3af5 fixed camel case ... |
41 |
Correlations []CorrelationField `json:"correlationFields"` |
64041a2ea first commit |
42 |
IdField string `json:"idField"` |
6ec91280b working on docume... |
43 |
|
64041a2ea first commit |
44 45 46 |
// Data can only hold slices of any type. It can't be used for itteration Data interface{} `json:"data"` } |
4a51e54d7 simplified |
47 48 49 50 51 52 53 |
type Payload struct { 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"` |
e1fbb41f9 added comments |
54 |
|
4a51e54d7 simplified |
55 56 57 |
// Data can only hold slices of any type. It can't be used for itteration Data interface{} `json:"data"` } |
6ec91280b working on docume... |
58 |
// NewPayload returs a payload sceleton for provided table. |
8dbe745c3 merged tables uti... |
59 60 |
func NewPayload(r *http.Request, table string) Payload { var pload Payload |
64041a2ea first commit |
61 |
|
4a51e54d7 simplified |
62 |
pload.Method = r.Method + " " + r.URL.Path |
90f4ed079 sped up loadTable() |
63 64 |
if table != "" { pload.Params = make(map[string]string, 0) |
6ec91280b working on docume... |
65 66 67 68 |
pload.Lang = translations(table) pload.Fields = fields(table) pload.IdField = id(table) pload.Correlations = correlations(table) |
90f4ed079 sped up loadTable() |
69 |
} |
8dbe745c3 merged tables uti... |
70 71 |
return pload } |
64041a2ea first commit |
72 |
|
6ec91280b working on docume... |
73 |
// DeliverPayload encodes payload to w. |
8dbe745c3 merged tables uti... |
74 75 76 |
func DeliverPayload(w http.ResponseWriter, payload Payload) { json.NewEncoder(w).Encode(payload) payload.Data = nil |
64041a2ea first commit |
77 |
} |
6ec91280b working on docume... |
78 79 80 |
// translations returns a slice of translations for a payload/table of ptype type. func translations(ptype string) []Translation { var translations []Translation |
8dbe745c3 merged tables uti... |
81 |
|
6ec91280b working on docume... |
82 83 |
for _, pload := range payloads { if pload.Type == ptype { |
8dbe745c3 merged tables uti... |
84 |
for _, t := range pload.Lang { |
8dbe745c3 merged tables uti... |
85 86 87 88 89 90 |
translations = append(translations, Translation{ Language: t.Language, FieldsLabels: t.FieldsLabels, }) } } |
64041a2ea first commit |
91 |
} |
8dbe745c3 merged tables uti... |
92 93 94 |
return translations } |
6ec91280b working on docume... |
95 96 97 |
// fields returns a slice of fields for a payload/table of ptype type. func fields(ptype string) []Field { var fields []Field |
8dbe745c3 merged tables uti... |
98 |
|
6ec91280b working on docume... |
99 100 |
for _, pload := range payloads { if pload.Type == ptype { |
8dbe745c3 merged tables uti... |
101 102 103 104 105 106 107 108 |
for _, f := range pload.Fields { fields = append(fields, f) } } } return fields } |
6ec91280b working on docume... |
109 110 111 112 |
// id returns the name of ID field of a payload/table of ptype type. func id(ptype string) string { for _, pload := range payloads { if pload.Type == ptype { |
8dbe745c3 merged tables uti... |
113 114 115 116 117 |
return pload.IdField } } return "" } |
6ec91280b working on docume... |
118 119 120 |
// correlations returns a slice of correlation fields for a payload/table of ptype type. func correlations(ptype string) []CorrelationField { var corr []CorrelationField |
8dbe745c3 merged tables uti... |
121 |
|
6ec91280b working on docume... |
122 123 124 125 |
for _, pload := range payloads { if pload.Type == ptype { for _, c := range pload.Correlations { corr = append(corr, c) |
8dbe745c3 merged tables uti... |
126 127 128 |
} } } |
6ec91280b working on docume... |
129 |
return corr |
8dbe745c3 merged tables uti... |
130 |
} |
6ec91280b working on docume... |
131 132 |
// InitTables loads all payloads in the payloads variable. // Returns an error if it fails. |
8dbe745c3 merged tables uti... |
133 |
func InitTables(db *ora.Ses, project string) error { |
1d0f61553 can't fetch clob |
134 135 136 137 |
jsonbuf, err := fetchJSON(db, project) if err != nil { return err } |
17a4d0447 mutex lock for pa... |
138 139 |
mu.Lock() defer mu.Unlock() |
1d0f61553 can't fetch clob |
140 |
json.Unmarshal([]byte(jsonbuf), &payloads) |
6ec91280b working on docume... |
141 |
if len(payloads) == 0 { |
8dbe745c3 merged tables uti... |
142 143 |
return errors.New("tables config is corrupt") } |
8dbe745c3 merged tables uti... |
144 145 |
return nil } |
6ec91280b working on docume... |
146 147 |
// fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table. // Returns an error if it fails. |
1d0f61553 can't fetch clob |
148 149 |
func fetchJSON(db *ora.Ses, project string) (string, error) { stmt, err := db.Prep(`SELECT JSON_CLOB FROM TABLES_CONFIG WHERE PROJEKAT` + EqualQuotes(project), ora.S) |
8dbe745c3 merged tables uti... |
150 |
defer stmt.Close() |
8dbe745c3 merged tables uti... |
151 |
if err != nil { |
1d0f61553 can't fetch clob |
152 |
return "", err |
8dbe745c3 merged tables uti... |
153 154 155 156 |
} rset, err := stmt.Qry() if err != nil { |
1d0f61553 can't fetch clob |
157 |
return "", err |
8dbe745c3 merged tables uti... |
158 |
} |
1d0f61553 can't fetch clob |
159 |
var data string |
8dbe745c3 merged tables uti... |
160 |
if rset.Next() { |
1d0f61553 can't fetch clob |
161 |
data = rset.Row[0].(string) |
64041a2ea first commit |
162 |
} |
1d0f61553 can't fetch clob |
163 |
return data, nil |
64041a2ea first commit |
164 |
} |
6ec91280b working on docume... |
165 166 |
// DecodeJSON decodes JSON data from r to v. // Returns an error if it fails. |
d5192378d added json decodi... |
167 168 169 |
func DecodeJSON(r io.Reader, v interface{}) error { return json.NewDecoder(r).Decode(v) } |