-
Notifications
You must be signed in to change notification settings - Fork 21
/
config_field.go
213 lines (184 loc) · 5.68 KB
/
config_field.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
package common
import (
_ "embed"
"encoding/json"
"fmt"
)
type ConfigField struct {
Readonly bool `json:"readonly"`
Sensitive bool `json:"sensitive"`
Nullable bool `json:"nullable"`
FieldValueType FieldValueType `json:"type"`
ItemFields map[string]ConfigField `json:"fields"`
ItemKeyField string `json:"key_field"`
ItemType map[string]FieldValueType `json:"item_type"`
Description map[string]string `json:"description"`
SensitiveExclusions map[string]bool `json:"sensitive_exclusions,omitempty"`
ApiField string `json:"api_field"`
}
func (c ConfigField) GetIsSensitiveForSchema() bool {
return c.Sensitive || (c.SensitiveExclusions != nil && len(c.SensitiveExclusions) > 0)
}
func (c ConfigField) GetIsSensitive(service string) bool {
if !c.Sensitive {
if c.SensitiveExclusions != nil {
_, ok := c.SensitiveExclusions[service]
return ok
}
return false
}
return true
}
func NewconfigField() ConfigField {
field := ConfigField{}
field.FieldValueType = String
field.Readonly = false
field.Sensitive = false
field.Nullable = true
field.Description = make(map[string]string)
field.ApiField = ""
field.ItemKeyField = ""
field.ItemType = make(map[string]FieldValueType)
field.SensitiveExclusions = nil
return field
}
var (
//go:embed fields.json
configFieldsJson []byte
//go:embed auth-fields.json
authFieldsJson []byte
//go:embed destination-fields.json
destinationFieldsJson []byte
configFields = make(map[string]ConfigField)
authFields = make(map[string]ConfigField)
destinationFields = make(map[string]ConfigField)
configFieldsByService = make(map[string]map[string]ConfigField)
authFieldsByService = make(map[string]map[string]ConfigField)
destinationFieldsByService = make(map[string]map[string]ConfigField)
destinationSchemaFields = make(map[string]map[string]bool)
)
func GetFieldsForService(service string) (map[string]ConfigField, error) {
if len(configFieldsByService) == 0 {
panic("Fields for config are not loaded")
}
if r, ok := configFieldsByService[service]; ok {
return r, nil
}
if _, ok := destinationSchemaFields[service]; ok {
return map[string]ConfigField{}, nil
}
return nil, fmt.Errorf("Unknown service: %v\n It seems like `%v` service is not yet supported in this provider version. \nPlease update to latest or wait for next release (if you are using latest already).", service, service)
}
func GetAuthFieldsForService(service string) map[string]ConfigField {
if len(authFieldsByService) == 0 {
panic("Fields for auth are not loaded")
}
if r, ok := authFieldsByService[service]; ok {
return r
}
return map[string]ConfigField{}
}
func GetDestinationFieldsForService(service string) map[string]ConfigField {
if len(destinationFieldsByService) == 0 {
panic("Fields for destination config are not loaded")
}
if r, ok := destinationFieldsByService[service]; ok {
return r
}
return map[string]ConfigField{}
}
func GetDestinationSchemaFields() map[string]map[string]bool {
if len(destinationSchemaFields) == 0 {
panic("Fields for config are not loaded")
}
return destinationSchemaFields
}
func GetAuthFieldsMap() map[string]ConfigField {
if len(authFields) == 0 {
panic("Fields for auth are not loaded")
}
return authFields
}
func GetConfigFieldsMap() map[string]ConfigField {
if len(configFields) == 0 {
panic("Fields for config are not loaded")
}
return configFields
}
func GetDestinationFieldsMap() map[string]ConfigField {
if len(destinationFields) == 0 {
panic("Fields for destination config are not loaded")
}
return destinationFields
}
func LoadAuthFieldsMap() {
if len(authFields) == 0 {
readAuthFieldsFromJson(&authFields)
}
fillAuthFieldsByService()
}
func LoadConfigFieldsMap() {
if len(configFields) == 0 {
readFieldsFromJson(&configFields)
}
fillFieldsByService()
}
func LocaDestinationFieldsMap() {
if len(destinationFields) == 0 {
readDestinationFieldsFromJson(&destinationFields)
}
fillDestinationFieldsByService()
}
func readAuthFieldsFromJson(target *map[string]ConfigField) {
if err := json.Unmarshal(authFieldsJson, target); err != nil {
panic(err)
}
}
func readFieldsFromJson(target *map[string]ConfigField) {
err := json.Unmarshal(configFieldsJson, target)
if err != nil {
panic(err)
}
// handle and remove destination schema fields
handleDestinationSchemaField("schema")
handleDestinationSchemaField("table")
handleDestinationSchemaField("schema_prefix")
}
func readDestinationFieldsFromJson(target *map[string]ConfigField) {
err := json.Unmarshal(destinationFieldsJson, target)
if err != nil {
panic(err)
}
}
func handleDestinationSchemaField(fieldName string) {
if schema, ok := configFields[fieldName]; ok {
for k := range schema.Description {
if _, ok := destinationSchemaFields[k]; !ok {
destinationSchemaFields[k] = make(map[string]bool)
}
destinationSchemaFields[k][fieldName] = true
}
delete(configFields, fieldName)
}
}
func fillDestinationFieldsByService() {
destinationFieldsByService = breakByService(destinationFields)
}
func fillFieldsByService() {
configFieldsByService = breakByService(configFields)
}
func fillAuthFieldsByService() {
authFieldsByService = breakByService(authFields)
}
func breakByService(source map[string]ConfigField) map[string]map[string]ConfigField {
result := make(map[string]map[string]ConfigField)
for k, v := range source {
for service := range v.Description {
if _, ok := result[service]; !ok {
result[service] = make(map[string]ConfigField)
}
result[service][k] = v
}
}
return result
}