Commit 4994f13a4a8409aa1eb26b3d0d0f081949a780e5

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

correlationFields konacno!

Showing 2 changed files with 3 additions and 1 deletions   Show diff stats
File was created 1 TODO:
2 * list config
3
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:"correlationFields"`
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