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