Commit 4cb3081998311d51ef0d5ad2596a49b90d3fb29b
1 parent
66478e04fb
Exists in
master
and in
1 other branch
BuildPayload -> MakePayload
Showing
1 changed file
with
2 additions
and
2 deletions
Show diff stats
json_utility.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "encoding/json" | 4 | "encoding/json" |
5 | "errors" | 5 | "errors" |
6 | "fmt" | 6 | "fmt" |
7 | "io" | 7 | "io" |
8 | "net/http" | 8 | "net/http" |
9 | "sync" | 9 | "sync" |
10 | 10 | ||
11 | "gopkg.in/rana/ora.v4" | 11 | "gopkg.in/rana/ora.v4" |
12 | ) | 12 | ) |
13 | 13 | ||
14 | var mu = &sync.Mutex{} | 14 | var mu = &sync.Mutex{} |
15 | var payloads []Payload | 15 | var payloads []Payload |
16 | 16 | ||
17 | type LangMap map[string]map[string]string | 17 | type LangMap map[string]map[string]string |
18 | 18 | ||
19 | type Field struct { | 19 | type Field struct { |
20 | Parameter string `json:"param"` | 20 | Parameter string `json:"param"` |
21 | Type string `json:"type"` | 21 | Type string `json:"type"` |
22 | Visible bool `json:"visible"` | 22 | Visible bool `json:"visible"` |
23 | Editable bool `json:"editable"` | 23 | Editable bool `json:"editable"` |
24 | } | 24 | } |
25 | 25 | ||
26 | type CorrelationField struct { | 26 | type CorrelationField struct { |
27 | Result string `json:"result"` | 27 | Result string `json:"result"` |
28 | Elements []string `json:"elements"` | 28 | Elements []string `json:"elements"` |
29 | Type string `json:"type"` | 29 | Type string `json:"type"` |
30 | } | 30 | } |
31 | 31 | ||
32 | type Translation struct { | 32 | type Translation struct { |
33 | Language string `json:"language"` | 33 | Language string `json:"language"` |
34 | FieldsLabels map[string]string `json:"fieldsLabels"` | 34 | FieldsLabels map[string]string `json:"fieldsLabels"` |
35 | } | 35 | } |
36 | 36 | ||
37 | type Payload struct { | 37 | type Payload struct { |
38 | Type string `json:"type"` | 38 | Type string `json:"type"` |
39 | Method string `json:"method"` | 39 | Method string `json:"method"` |
40 | Params map[string]string `json:"params"` | 40 | Params map[string]string `json:"params"` |
41 | Lang []Translation `json:"lang"` | 41 | Lang []Translation `json:"lang"` |
42 | Fields []Field `json:"fields"` | 42 | Fields []Field `json:"fields"` |
43 | Correlations []CorrelationField `json:"correlationFields"` | 43 | Correlations []CorrelationField `json:"correlationFields"` |
44 | IdField string `json:"idField"` | 44 | IdField string `json:"idField"` |
45 | 45 | ||
46 | // Data holds JSON payload. It can't be used for itteration. | 46 | // Data holds JSON payload. It can't be used for itteration. |
47 | Data interface{} `json:"data"` | 47 | Data interface{} `json:"data"` |
48 | } | 48 | } |
49 | 49 | ||
50 | // InitTables loads all payloads in the payloads variable. | 50 | // InitTables loads all payloads in the payloads variable. |
51 | // Returns an error if it fails. | 51 | // Returns an error if it fails. |
52 | func InitTables(db *ora.Ses, project string) error { | 52 | func InitTables(db *ora.Ses, project string) error { |
53 | jsonbuf, err := fetchJSON(db, project) | 53 | jsonbuf, err := fetchJSON(db, project) |
54 | if err != nil { | 54 | if err != nil { |
55 | return err | 55 | return err |
56 | } | 56 | } |
57 | 57 | ||
58 | mu.Lock() | 58 | mu.Lock() |
59 | defer mu.Unlock() | 59 | defer mu.Unlock() |
60 | json.Unmarshal(jsonbuf, &payloads) | 60 | json.Unmarshal(jsonbuf, &payloads) |
61 | if len(payloads) == 0 { | 61 | if len(payloads) == 0 { |
62 | return errors.New("tables config is corrupt") | 62 | return errors.New("tables config is corrupt") |
63 | } | 63 | } |
64 | return nil | 64 | return nil |
65 | } | 65 | } |
66 | 66 | ||
67 | // ReloadTables reloads all payloads in the payloads variable. | 67 | // ReloadTables reloads all payloads in the payloads variable. |
68 | // Returns an error if it fails. | 68 | // Returns an error if it fails. |
69 | func ReloadTables(db *ora.Ses, project string) error { | 69 | func ReloadTables(db *ora.Ses, project string) error { |
70 | payloads = make([]Payload, 0) | 70 | payloads = make([]Payload, 0) |
71 | return InitTables(db, project) | 71 | return InitTables(db, project) |
72 | } | 72 | } |
73 | 73 | ||
74 | // DecodeJSON decodes JSON data from r to v. | 74 | // DecodeJSON decodes JSON data from r to v. |
75 | // Returns an error if it fails. | 75 | // Returns an error if it fails. |
76 | func DecodeJSON(r io.Reader, v interface{}) error { | 76 | func DecodeJSON(r io.Reader, v interface{}) error { |
77 | return json.NewDecoder(r).Decode(v) | 77 | return json.NewDecoder(r).Decode(v) |
78 | } | 78 | } |
79 | 79 | ||
80 | // NewPayload returs a payload sceleton for provided table. | 80 | // NewPayload returs a payload sceleton for provided table. |
81 | func NewPayload(r *http.Request, table string) Payload { | 81 | func NewPayload(r *http.Request, table string) Payload { |
82 | var pload Payload | 82 | var pload Payload |
83 | 83 | ||
84 | pload.Method = r.Method + " " + r.RequestURI | 84 | pload.Method = r.Method + " " + r.RequestURI |
85 | pload.Type = table | 85 | pload.Type = table |
86 | if table != "" { | 86 | if table != "" { |
87 | pload.Params = make(map[string]string, 0) | 87 | pload.Params = make(map[string]string, 0) |
88 | pload.Lang = translations(table) | 88 | pload.Lang = translations(table) |
89 | pload.Fields = fields(table) | 89 | pload.Fields = fields(table) |
90 | pload.IdField = id(table) | 90 | pload.IdField = id(table) |
91 | pload.Correlations = correlations(table) | 91 | pload.Correlations = correlations(table) |
92 | } | 92 | } |
93 | return pload | 93 | return pload |
94 | } | 94 | } |
95 | 95 | ||
96 | // BuildPayload returs a payload for provided table with populated Data field. | 96 | // MakePayload returs a payload for provided table with populated Data field. |
97 | func BuildPayload(r *http.Request, table string, data interface{}) Payload { | 97 | func MakePayload(r *http.Request, table string, data interface{}) Payload { |
98 | var pload Payload | 98 | var pload Payload |
99 | 99 | ||
100 | pload.Method = r.Method + " " + r.RequestURI | 100 | pload.Method = r.Method + " " + r.RequestURI |
101 | if table != "" { | 101 | if table != "" { |
102 | pload.Params = make(map[string]string, 0) | 102 | pload.Params = make(map[string]string, 0) |
103 | pload.Lang = translations(table) | 103 | pload.Lang = translations(table) |
104 | pload.Fields = fields(table) | 104 | pload.Fields = fields(table) |
105 | pload.IdField = id(table) | 105 | pload.IdField = id(table) |
106 | pload.Correlations = correlations(table) | 106 | pload.Correlations = correlations(table) |
107 | } | 107 | } |
108 | pload.Data = data | 108 | pload.Data = data |
109 | return pload | 109 | return pload |
110 | } | 110 | } |
111 | 111 | ||
112 | // DeliverPayload encodes payload to w. | 112 | // DeliverPayload encodes payload to w. |
113 | func DeliverPayload(w http.ResponseWriter, payload Payload) { | 113 | func DeliverPayload(w http.ResponseWriter, payload Payload) { |
114 | json.NewEncoder(w).Encode(payload) | 114 | json.NewEncoder(w).Encode(payload) |
115 | payload.Data = nil | 115 | payload.Data = nil |
116 | } | 116 | } |
117 | 117 | ||
118 | // translations returns a slice of translations for a payload/table of ptype type. | 118 | // translations returns a slice of translations for a payload/table of ptype type. |
119 | func translations(ptype string) []Translation { | 119 | func translations(ptype string) []Translation { |
120 | var translations []Translation | 120 | var translations []Translation |
121 | 121 | ||
122 | for _, pload := range payloads { | 122 | for _, pload := range payloads { |
123 | if pload.Type == ptype { | 123 | if pload.Type == ptype { |
124 | for _, t := range pload.Lang { | 124 | for _, t := range pload.Lang { |
125 | translations = append(translations, Translation{ | 125 | translations = append(translations, Translation{ |
126 | Language: t.Language, | 126 | Language: t.Language, |
127 | FieldsLabels: t.FieldsLabels, | 127 | FieldsLabels: t.FieldsLabels, |
128 | }) | 128 | }) |
129 | } | 129 | } |
130 | } | 130 | } |
131 | } | 131 | } |
132 | 132 | ||
133 | return translations | 133 | return translations |
134 | } | 134 | } |
135 | 135 | ||
136 | // fields returns a slice of fields for a payload/table of ptype type. | 136 | // fields returns a slice of fields for a payload/table of ptype type. |
137 | func fields(ptype string) []Field { | 137 | func fields(ptype string) []Field { |
138 | var fields []Field | 138 | var fields []Field |
139 | 139 | ||
140 | for _, pload := range payloads { | 140 | for _, pload := range payloads { |
141 | if pload.Type == ptype { | 141 | if pload.Type == ptype { |
142 | for _, f := range pload.Fields { | 142 | for _, f := range pload.Fields { |
143 | fields = append(fields, f) | 143 | fields = append(fields, f) |
144 | } | 144 | } |
145 | } | 145 | } |
146 | } | 146 | } |
147 | 147 | ||
148 | return fields | 148 | return fields |
149 | } | 149 | } |
150 | 150 | ||
151 | // id returns the name of ID field of a payload/table of ptype type. | 151 | // id returns the name of ID field of a payload/table of ptype type. |
152 | func id(ptype string) string { | 152 | func id(ptype string) string { |
153 | for _, pload := range payloads { | 153 | for _, pload := range payloads { |
154 | if pload.Type == ptype { | 154 | if pload.Type == ptype { |
155 | return pload.IdField | 155 | return pload.IdField |
156 | } | 156 | } |
157 | } | 157 | } |
158 | return "" | 158 | return "" |
159 | } | 159 | } |
160 | 160 | ||
161 | // correlations returns a slice of correlation fields for a payload/table of ptype type. | 161 | // correlations returns a slice of correlation fields for a payload/table of ptype type. |
162 | func correlations(ptype string) []CorrelationField { | 162 | func correlations(ptype string) []CorrelationField { |
163 | var corr []CorrelationField | 163 | var corr []CorrelationField |
164 | 164 | ||
165 | for _, pload := range payloads { | 165 | for _, pload := range payloads { |
166 | if pload.Type == ptype { | 166 | if pload.Type == ptype { |
167 | for _, c := range pload.Correlations { | 167 | for _, c := range pload.Correlations { |
168 | corr = append(corr, c) | 168 | corr = append(corr, c) |
169 | } | 169 | } |
170 | } | 170 | } |
171 | } | 171 | } |
172 | 172 | ||
173 | return corr | 173 | return corr |
174 | } | 174 | } |
175 | 175 | ||
176 | // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table. | 176 | // fetchJSON returns a byte slice of JSON configuration file from TABLES_CONFIG table. |
177 | // Returns an error if it fails. | 177 | // Returns an error if it fails. |
178 | func fetchJSON(db *ora.Ses, project string) ([]byte, error) { | 178 | func fetchJSON(db *ora.Ses, project string) ([]byte, error) { |
179 | db.SetCfg(db.Cfg().SetClob(ora.S)) | 179 | db.SetCfg(db.Cfg().SetClob(ora.S)) |
180 | stmt, err := db.Prep(`SELECT JSON_NCLOB FROM TABLES_CONFIG WHERE PROJEKAT = `+fmt.Sprintf("'%s'", project), ora.S) | 180 | stmt, err := db.Prep(`SELECT JSON_NCLOB FROM TABLES_CONFIG WHERE PROJEKAT = `+fmt.Sprintf("'%s'", project), ora.S) |
181 | defer stmt.Close() | 181 | defer stmt.Close() |
182 | if err != nil { | 182 | if err != nil { |
183 | return nil, err | 183 | return nil, err |
184 | } | 184 | } |
185 | 185 | ||
186 | rset, err := stmt.Qry() | 186 | rset, err := stmt.Qry() |
187 | if err != nil { | 187 | if err != nil { |
188 | return nil, err | 188 | return nil, err |
189 | } | 189 | } |
190 | 190 | ||
191 | var data string | 191 | var data string |
192 | if rset.Next() { | 192 | if rset.Next() { |
193 | data = rset.Row[0].(string) | 193 | data = rset.Row[0].(string) |
194 | } | 194 | } |
195 | 195 | ||
196 | //fmt.Println(data) | 196 | //fmt.Println(data) |
197 | return []byte(data), nil | 197 | return []byte(data), nil |
198 | } | 198 | } |
199 | 199 |