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