Commit 8202fb0dbc81c60ff3e8f27235b53c8823dbc8e9
1 parent
b291ac8c49
Exists in
master
and in
1 other branch
json payload simplyfied
Showing
2 changed files
with
3 additions
and
2 deletions
Show diff stats
json_utility.go
1 | package restutility | 1 | package restutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "net/http" | 4 | "net/http" |
5 | "strings" | 5 | "strings" |
6 | ) | 6 | ) |
7 | 7 | ||
8 | type LangMap map[string]map[string]string | 8 | type LangMap map[string]map[string]string |
9 | 9 | ||
10 | type Field struct { | 10 | type Field struct { |
11 | Parameter string `json:"param"` | 11 | Parameter string `json:"param"` |
12 | Type string `json:"type"` | 12 | Type string `json:"type"` |
13 | Visible bool `json:"visible"` | 13 | Visible bool `json:"visible"` |
14 | Editable bool `json:"editable"` | 14 | Editable bool `json:"editable"` |
15 | } | 15 | } |
16 | 16 | ||
17 | type JSONParams struct { | 17 | type JSONParams struct { |
18 | Lang LangMap | 18 | Lang LangMap |
19 | Fields []Field | 19 | Fields []Field |
20 | IdField string | 20 | IdField string |
21 | Correlations []CorrelationField `json:"correlation_fields"` | 21 | Correlations []CorrelationField `json:"correlation_fields"` |
22 | } | 22 | } |
23 | 23 | ||
24 | type JSONPayload struct { | 24 | type JSONPayload struct { |
25 | Method string `json:"method"` | 25 | Method string `json:"method"` |
26 | Params map[string]string `json:"params"` | 26 | Params map[string]string `json:"params"` |
27 | Lang LangMap `json:"lang"` | 27 | Lang LangMap `json:"lang"` |
28 | Fields []Field `json:"fields"` | 28 | Fields []Field `json:"fields"` |
29 | Correlations []CorrelationField `json:"correlation_fields"` | 29 | Correlations []CorrelationField `json:"correlation_fields"` |
30 | IdField string `json:"idField"` | 30 | IdField string `json:"idField"` |
31 | // Data can only hold slices of any type. It can't be used for itteration | 31 | // Data can only hold slices of any type. It can't be used for itteration |
32 | Data interface{} `json:"data"` | 32 | Data interface{} `json:"data"` |
33 | } | 33 | } |
34 | 34 | ||
35 | func NewJSONParams(lang LangMap, | 35 | func NewJSONParams(lang LangMap, |
36 | fields []Field, | 36 | fields []Field, |
37 | id string, | 37 | id string, |
38 | correlations []CorrelationField) JSONParams { | 38 | correlations []CorrelationField) JSONParams { |
39 | 39 | ||
40 | var jp JSONParams | 40 | var jp JSONParams |
41 | 41 | ||
42 | jp.Lang = lang | 42 | jp.Lang = lang |
43 | jp.Fields = fields | 43 | jp.Fields = fields |
44 | jp.IdField = id | 44 | jp.IdField = id |
45 | jp.Correlations = correlations | 45 | jp.Correlations = correlations |
46 | 46 | ||
47 | return jp | 47 | return jp |
48 | } | 48 | } |
49 | 49 | ||
50 | func NewJSONPayload(r *http.Request, params JSONParams) JSONPayload { | 50 | func NewJSONPayload(r *http.Request, table string) JSONPayload { |
51 | var obj JSONPayload | 51 | var obj JSONPayload |
52 | params := loadTable(table) | ||
52 | obj.Method = strings.ToLower(r.Method + " " + r.URL.Path) | 53 | obj.Method = strings.ToLower(r.Method + " " + r.URL.Path) |
53 | obj.Params = make(map[string]string, 0) | 54 | obj.Params = make(map[string]string, 0) |
54 | obj.Lang = make(map[string]map[string]string, 0) | 55 | obj.Lang = make(map[string]map[string]string, 0) |
55 | obj.Fields = make([]Field, 0) | 56 | obj.Fields = make([]Field, 0) |
56 | obj.IdField = params.IdField | 57 | obj.IdField = params.IdField |
57 | obj.Correlations = params.Correlations | 58 | obj.Correlations = params.Correlations |
58 | 59 | ||
59 | for k, m := range params.Lang { | 60 | for k, m := range params.Lang { |
60 | obj.Lang[k] = m | 61 | obj.Lang[k] = m |
61 | } | 62 | } |
62 | for _, f := range params.Fields { | 63 | for _, f := range params.Fields { |
63 | obj.Fields = append(obj.Fields, f) | 64 | obj.Fields = append(obj.Fields, f) |
64 | } | 65 | } |
65 | 66 | ||
66 | return obj | 67 | return obj |
67 | } | 68 | } |
68 | 69 | ||
69 | 70 |
tables_utility.go
1 | package restutility | 1 | package restutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "encoding/json" | 4 | "encoding/json" |
5 | "errors" | 5 | "errors" |
6 | ) | 6 | ) |
7 | 7 | ||
8 | type TableConfig struct { | 8 | type TableConfig struct { |
9 | Tables []Table | 9 | Tables []Table |
10 | } | 10 | } |
11 | 11 | ||
12 | type Table struct { | 12 | type Table struct { |
13 | TableType string `json:"tableType"` | 13 | TableType string `json:"tableType"` |
14 | Translations []TableTranslation `json:"translations"` | 14 | Translations []TableTranslation `json:"translations"` |
15 | TableFields []Field `json:"tableFields"` | 15 | TableFields []Field `json:"tableFields"` |
16 | Correlations []CorrelationField `json:"correlation_fields"` | 16 | Correlations []CorrelationField `json:"correlation_fields"` |
17 | IdField string `json:"idField"` | 17 | IdField string `json:"idField"` |
18 | } | 18 | } |
19 | 19 | ||
20 | type CorrelationField struct { | 20 | type CorrelationField struct { |
21 | Result string `json:"result"` | 21 | Result string `json:"result"` |
22 | Elements []string `json:"elements"` | 22 | Elements []string `json:"elements"` |
23 | Type string `json:"type"` | 23 | Type string `json:"type"` |
24 | } | 24 | } |
25 | 25 | ||
26 | type TableTranslation struct { | 26 | type TableTranslation struct { |
27 | Language string `json:"language"` | 27 | Language string `json:"language"` |
28 | FieldsLabels map[string]string `json:"fieldsLabels"` | 28 | FieldsLabels map[string]string `json:"fieldsLabels"` |
29 | } | 29 | } |
30 | 30 | ||
31 | func (tl TableConfig) LoadTranslations(tableType string) LangMap { | 31 | func (tl TableConfig) LoadTranslations(tableType string) LangMap { |
32 | translations := make(LangMap, 0) | 32 | translations := make(LangMap, 0) |
33 | 33 | ||
34 | for _, table := range tl.Tables { | 34 | for _, table := range tl.Tables { |
35 | if tableType == table.TableType { | 35 | if tableType == table.TableType { |
36 | for _, t := range table.Translations { | 36 | for _, t := range table.Translations { |
37 | translations[t.Language] = t.FieldsLabels | 37 | translations[t.Language] = t.FieldsLabels |
38 | } | 38 | } |
39 | } | 39 | } |
40 | } | 40 | } |
41 | 41 | ||
42 | return translations | 42 | return translations |
43 | } | 43 | } |
44 | 44 | ||
45 | func (tl TableConfig) LoadFields(tableType string) []Field { | 45 | func (tl TableConfig) LoadFields(tableType string) []Field { |
46 | fields := make([]Field, 0) | 46 | fields := make([]Field, 0) |
47 | 47 | ||
48 | for _, table := range tl.Tables { | 48 | for _, table := range tl.Tables { |
49 | if tableType == table.TableType { | 49 | if tableType == table.TableType { |
50 | for _, f := range table.TableFields { | 50 | for _, f := range table.TableFields { |
51 | fields = append(fields, f) | 51 | fields = append(fields, f) |
52 | } | 52 | } |
53 | } | 53 | } |
54 | } | 54 | } |
55 | 55 | ||
56 | return fields | 56 | return fields |
57 | } | 57 | } |
58 | 58 | ||
59 | func (tl TableConfig) LoadIdField(tableType string) string { | 59 | func (tl TableConfig) LoadIdField(tableType string) string { |
60 | for _, table := range tl.Tables { | 60 | for _, table := range tl.Tables { |
61 | if tableType == table.TableType { | 61 | if tableType == table.TableType { |
62 | return table.IdField | 62 | return table.IdField |
63 | } | 63 | } |
64 | } | 64 | } |
65 | return "" | 65 | return "" |
66 | } | 66 | } |
67 | 67 | ||
68 | func (tl TableConfig) LoadCorrelations(tableType string) []CorrelationField { | 68 | func (tl TableConfig) LoadCorrelations(tableType string) []CorrelationField { |
69 | resp := make([]CorrelationField, 0) | 69 | resp := make([]CorrelationField, 0) |
70 | 70 | ||
71 | for _, table := range tl.Tables { | 71 | for _, table := range tl.Tables { |
72 | if tableType == table.TableType { | 72 | if tableType == table.TableType { |
73 | for _, f := range table.Correlations { | 73 | for _, f := range table.Correlations { |
74 | resp = append(resp, f) | 74 | resp = append(resp, f) |
75 | } | 75 | } |
76 | } | 76 | } |
77 | } | 77 | } |
78 | 78 | ||
79 | return resp | 79 | return resp |
80 | } | 80 | } |
81 | 81 | ||
82 | var _tables TableConfig | 82 | var _tables TableConfig |
83 | var _prevProject string | 83 | var _prevProject string |
84 | 84 | ||
85 | func InitTables(jsonbuf []byte) error { | 85 | func InitTables(jsonbuf []byte) error { |
86 | json.Unmarshal(jsonbuf, &_tables.Tables) | 86 | json.Unmarshal(jsonbuf, &_tables.Tables) |
87 | if len(_tables.Tables) == 0 { | 87 | if len(_tables.Tables) == 0 { |
88 | return errors.New("tables config is corrupt") | 88 | return errors.New("tables config is corrupt") |
89 | } | 89 | } |
90 | return nil | 90 | return nil |
91 | } | 91 | } |
92 | 92 | ||
93 | func LoadTable(table string) JSONParams { | 93 | func loadTable(table string) JSONParams { |
94 | return NewJSONParams(_tables.LoadTranslations(table), | 94 | return NewJSONParams(_tables.LoadTranslations(table), |
95 | _tables.LoadFields(table), | 95 | _tables.LoadFields(table), |
96 | _tables.LoadIdField(table), | 96 | _tables.LoadIdField(table), |
97 | _tables.LoadCorrelations(table)) | 97 | _tables.LoadCorrelations(table)) |
98 | } | 98 | } |
99 | 99 | ||
100 | 100 |