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