Commit d5192378d388d41c65898fc66ac5739fffc674f1

Authored by Marko Tikvić
1 parent e77b75ec6c
Exists in master and in 1 other branch v2

added json decoding shorthand

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