/
jsonCommon.go
334 lines (280 loc) · 9.97 KB
/
jsonCommon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package main
import (
"encoding/json"
"io"
"mime/multipart"
"net/http"
"path/filepath"
"strconv"
"strings"
"github.com/openmpp/go/ompp/db"
"github.com/openmpp/go/ompp/helper"
"github.com/openmpp/go/ompp/omppLog"
)
// set json response headers: Content-Type: application/json
func jsonSetHeaders(w http.ResponseWriter, r *http.Request) {
// if Content-Type not set then use json
if _, isSet := w.Header()["Content-Type"]; !isSet {
w.Header().Set("Content-Type", "application/json")
}
// if request from localhost then allow response to any protocol or port
/*
if strings.HasPrefix(r.Host, "localhost") {
if _, isSet := w.Header()["Access-Control-Allow-Origin"]; !isSet {
w.Header().Set("Access-Control-Allow-Origin", "*")
}
}
*/
}
// jsonResponse set json response headers and writes src as json into w response writer.
// On error it writes 500 internal server error response.
func jsonResponse(w http.ResponseWriter, r *http.Request, src interface{}) {
jsonSetHeaders(w, r)
err := json.NewEncoder(w).Encode(src)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// jsonResponseBytes set json response headers and writes src bytes into w response writer.
// If src bytes empty then "{}" is written.
func jsonResponseBytes(w http.ResponseWriter, r *http.Request, src []byte) {
jsonSetHeaders(w, r)
if len(src) <= 0 {
w.Write([]byte("{}"))
}
w.Write(src)
}
// jsonCellWriter writes each row of data page into response as list of comma separated json values.
// It is an append to response and response headers must already be set.
func jsonCellWriter(w http.ResponseWriter, enc *json.Encoder, cvtCell func(interface{}) (interface{}, error)) func(src interface{}) (bool, error) {
isNext := false
// write data page into response as list of comma separated json values
cvtWr := func(src interface{}) (bool, error) {
if isNext {
w.Write([]byte{','}) // until the last separate array items with , comma
}
val := src // id's cell
var err error
// convert cell from id's to code if converter specified
if cvtCell != nil {
if val, err = cvtCell(src); err != nil {
return false, err
}
}
// write actual value
if err := enc.Encode(val); err != nil {
return false, err
}
isNext = true
return true, nil
}
return cvtWr
}
// jsonRequestDecode validate Content-Type: application/json and decode json body.
// Destination for json decode: dst must be a pointer.
// If isRequired is true then json body is required else it can be empty by default.
// On error it writes error response 400 or 415 and return false.
func jsonRequestDecode(w http.ResponseWriter, r *http.Request, isRequired bool, dst interface{}) bool {
// json body expected
if !strings.Contains(r.Header.Get("Content-Type"), "application/json") {
http.Error(w, "Expected Content-Type: application/json", http.StatusUnsupportedMediaType)
return false
}
// decode json
err := json.NewDecoder(r.Body).Decode(dst)
if err != nil {
if err == io.EOF {
if !isRequired {
return true // empty default values
} else {
http.Error(w, "Invalid (empty) json at "+r.URL.String(), http.StatusBadRequest)
return false
}
}
omppLog.Log("Json decode error at " + r.URL.String() + ": " + err.Error())
http.Error(w, "Json decode error at "+r.URL.String(), http.StatusBadRequest)
return false
}
return true // completed OK
}
// jsonMultipartDecode decode json part of multipart form reader.
// It does move to next part, check part form name and decode json content from part body.
// Destination for json decode: dst must be a pointer.
// On error it writes error response 400 or 415 and return false.
func jsonMultipartDecode(w http.ResponseWriter, mr *multipart.Reader, name string, dst interface{}) bool {
// open next part and check part name
part, err := mr.NextPart()
if err == io.EOF {
http.Error(w, "Invalid (empty) next part of multipart form "+name, http.StatusBadRequest)
return false
}
if err != nil {
http.Error(w, "Failed to get next part of multipart form "+name+" : "+err.Error(), http.StatusBadRequest)
return false
}
defer part.Close()
if part.FormName() != name {
http.Error(w, "Invalid part of multipart form, expected name: "+name, http.StatusBadRequest)
return false
}
// decode json
err = json.NewDecoder(part).Decode(dst)
if err != nil {
if err == io.EOF {
http.Error(w, "Invalid (empty) json part of multipart form "+name, http.StatusBadRequest)
return false
}
http.Error(w, "Json decode error at part of multipart form "+name, http.StatusBadRequest)
return false
}
return true // completed OK
}
// jsonRequestToFile validate Content-Type: application/json and copy request body into output file as is.
// If output file already exists then it is truncated.
// On error it writes error response 400 or 500 and return false.
func jsonRequestToFile(w http.ResponseWriter, r *http.Request, outPath string) bool {
// json body expected
if !strings.Contains(r.Header.Get("Content-Type"), "application/json") {
http.Error(w, "Expected Content-Type: application/json", http.StatusUnsupportedMediaType)
return false
}
// create or truncate output file
fName := filepath.Base(outPath)
err := helper.SaveTo(outPath, r.Body)
if err != nil {
omppLog.Log("Error: unable to write into ", outPath, err)
http.Error(w, "Error: unable to write into "+fName, http.StatusInternalServerError)
return false
}
return true // completed OK
}
// model type metadata, "unpacked" during marshaled to json (range enums restored)
type typeMetaUnpack struct {
TypeDicRow *db.TypeDicRow // model type rows: type_dic join to model_type_dic
Enum []db.TypeEnumRow // type enum rows: type_enum_lst join to model_type_dic
}
// copy of ModelMeta, using alias for TypeMeta to do a special range type marshaling
type modelMetaUnpack struct {
Model *db.ModelDicRow // model_dic table row
Type []typeMetaUnpack // types metadata: type name and enums
Param []db.ParamMeta // parameters metadata: parameter name, type, dimensions
Table []db.TableMeta // output tables metadata: table name, dimensions, accumulators, expressions
Entity []db.EntityMeta // model entities and attributes
Group []db.GroupMeta // groups of parameters or output tables
}
func copyModelMetaToUnpack(meta *db.ModelMeta) *modelMetaUnpack {
if meta == nil {
return nil
}
mcp := modelMetaUnpack{
Model: &meta.Model,
Type: make([]typeMetaUnpack, len(meta.Type)),
Param: meta.Param,
Table: meta.Table,
Entity: meta.Entity,
Group: meta.Group,
}
for k := range meta.Type {
mcp.Type[k].TypeDicRow = &meta.Type[k].TypeDicRow
mcp.Type[k].Enum = meta.Type[k].Enum
}
return &mcp
}
// marshal type row and type enums[] to json, "unpack" range enums which may be not loaded from database
func (src *typeMetaUnpack) MarshalJSON() ([]byte, error) {
tm := struct {
*db.TypeDicRow
Enum []db.TypeEnumRow
}{
TypeDicRow: src.TypeDicRow,
Enum: src.Enum,
}
// if it is a range and enums not loaded from database then create enums
if tm.IsRange && len(tm.Enum) <= 0 {
n := 1 + tm.MaxEnumId - tm.MinEnumId
tm.Enum = make([]db.TypeEnumRow, n)
for k := 0; k < n; k++ {
nId := tm.MinEnumId + k
tm.Enum[k] = db.TypeEnumRow{
ModelId: tm.ModelId,
TypeId: tm.TypeId,
EnumId: nId,
Name: strconv.Itoa(nId),
}
}
}
return json.Marshal(tm)
}
type aDescrNote struct {
LangCode *string // lang_code VARCHAR(32) NOT NULL
Descr *string // descr VARCHAR(255) NOT NULL
Note *string // note VARCHAR(32000)
}
// typeEnumDescrNote is join of type_enum_lst, model_type_dic, type_enum_txt
type typeEnumDescrNote struct {
Enum *db.TypeEnumRow // type enum row: type_enum_lst join to model_type_dic
DescrNote aDescrNote // from type_enum_txt
}
// TypeDescrNote is join of type_dic_txt, model_type_dic, type_dic_txt
type typeUnpackDescrNote struct {
Type *db.TypeDicRow // model type row: type_dic join to model_type_dic
DescrNote *aDescrNote // from type_dic_txt
TypeEnumTxt []typeEnumDescrNote // type enum text rows: type_enum_txt join to model_type_dic
langCode string // language for description and notes
}
// marshal type text metadata to json, "unpack" range enums which may be not loaded from database
func (src *typeUnpackDescrNote) MarshalJSON() ([]byte, error) {
tm := struct {
Type *db.TypeDicRow
DescrNote *aDescrNote
TypeEnumTxt []typeEnumDescrNote // type enum text rows: type_enum_txt join to model_type_dic
}{
Type: src.Type,
DescrNote: src.DescrNote,
TypeEnumTxt: src.TypeEnumTxt,
}
// if type not a range or enums loaded from database then use standard json marshal
if !tm.Type.IsRange {
return json.Marshal(tm)
}
if len(tm.TypeEnumTxt) > 0 {
return json.Marshal(tm) // all range enums are loaded from database
}
// else it is a range type and there no enums: marshal array of [min, max] enum Id, Name, Descr
n := 1 + (tm.Type.MaxEnumId - tm.Type.MinEnumId)
tm.TypeEnumTxt = make([]typeEnumDescrNote, n)
emptyNote := ""
for k := 0; k < n; k++ {
nId := k + tm.Type.MinEnumId
et := typeEnumDescrNote{
Enum: &db.TypeEnumRow{
ModelId: tm.Type.ModelId,
TypeId: tm.Type.TypeId,
EnumId: nId,
Name: strconv.Itoa(nId),
},
DescrNote: aDescrNote{
LangCode: &src.langCode,
Note: &emptyNote,
},
}
et.DescrNote.Descr = &et.Enum.Name // for range type enum code same as description and same as enum id
tm.TypeEnumTxt[k] = et
}
return json.Marshal(tm)
}
// marshal enum text to json, if not description or notes not epty, otherwise return "DescrNote": null
func (src typeEnumDescrNote) MarshalJSON() ([]byte, error) {
tm := struct {
Enum *db.TypeEnumRow
DescrNote *aDescrNote
}{
Enum: src.Enum,
}
if src.DescrNote.Descr != nil && *src.DescrNote.Descr != "" || src.DescrNote.Note != nil && *src.DescrNote.Note != "" {
tm.DescrNote = &src.DescrNote
}
return json.Marshal(tm)
}