-
Notifications
You must be signed in to change notification settings - Fork 5
/
definition.go
364 lines (321 loc) · 8.97 KB
/
definition.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//Author xc, Created on 2019-03-28 20:00
//{COPYRIGHTS}
package definition
import (
"encoding/json"
"errors"
"log"
"strings"
"github.com/digimakergo/digimaker/core/util"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
var LocationColumns []string = []string{
"id",
"parent_id",
"main_id",
"hierarchy",
"depth",
"identifier_path",
"content_type",
"content_id",
"language",
"identifier_path",
"name",
"is_hidden",
"is_invisible",
"priority",
"uid",
"section",
"p",
}
type ContentTypeList map[string]map[string]ContentType
//ValidationRule defines rule for a field's validation. eg. max length
type VaidationRule map[string]interface{}
type FieldParameters map[string]interface{}
type ContentType struct {
Name string `json:"name"`
TableName string `json:"table_name"`
RelationData []string `json:"relation_data"`
NamePattern string `json:"name_pattern"`
HasVersion bool `json:"has_version"`
HasLocation bool `json:"has_location"`
AllowedTypes []string `json:"allowed_types"`
Fields []FieldDef `json:"fields"`
DataFields []DataField `json:"data_fields"`
//All fields where identifier is the key.
FieldMap map[string]FieldDef `json:"-"`
FieldIdentifierList []string `json:"-"`
hasRelationlist int //cache of hasRelationlist, 1(yes), -1(no), 0(not set)
}
func (c *ContentType) HasDataField(identifier string) bool {
result := false
for _, item := range c.DataFields {
if item.Identifier == identifier {
result = true
break
}
}
return result
}
func (c ContentType) HasRelationlist() bool {
if c.hasRelationlist == 0 {
hasRelationList := false
for _, field := range c.FieldMap {
if field.FieldType == "relationlist" {
hasRelationList = true
break
}
}
if hasRelationList {
c.hasRelationlist = 1
} else {
c.hasRelationlist = -1
}
}
if c.hasRelationlist == 1 {
return true
} else {
return false
}
}
func (c *ContentType) Init(fieldCallback ...func(*FieldDef)) {
//set all fields into FieldMap
fieldMap := map[string]FieldDef{}
identifierList := []string{}
for i, field := range c.Fields {
identifier := field.Identifier
if len(fieldCallback) > 0 {
fieldCallback[0](&field)
}
fieldMap[identifier] = field
identifierList = append(identifierList, identifier)
//get sub fields
subFields := field.GetSubFields(fieldCallback...)
c.Fields[i] = field
for subIdentifier, subField := range subFields {
fieldMap[subIdentifier] = subField
identifierList = append(identifierList, subIdentifier)
}
}
c.FieldMap = fieldMap
c.FieldIdentifierList = identifierList
}
//Content field definition
type FieldDef struct {
Identifier string `json:"identifier"`
Name string `json:"name"`
FieldType string `json:"type"`
DefaultValue interface{} `json:"default_value"` //eg. checkbox 1 means checked
Required bool `json:"required"`
Description string `json:"description"`
IsOutput bool `json:"is_output"`
Parameters FieldParameters `json:"parameters"`
Children []FieldDef `json:"children"`
}
type DataField struct {
Identifier string `json:"identifier"`
FieldType string `json:"fieldtype"`
Name string `json:"name"`
}
func (cf *FieldDef) GetSubFields(callback ...func(*FieldDef)) map[string]FieldDef {
return getSubFields(cf, callback...)
}
func getSubFields(cf *FieldDef, callback ...func(*FieldDef)) map[string]FieldDef {
if cf.Children == nil {
return nil
} else {
result := map[string]FieldDef{}
for i, field := range cf.Children {
identifier := field.Identifier
if len(callback) > 0 {
callback[0](&field)
}
//get children under child
children := getSubFields(&field, callback...)
cf.Children[i] = field
for _, item := range children {
identifier2 := item.Identifier
result[identifier2] = item
}
result[identifier] = field
}
return result
}
}
//ContentTypeDefinition Content types which defined in contenttype.json
var contentTypeDefinition ContentTypeList
//LoadDefinition Load all setting in file into memory.
func LoadDefinition() error {
//Load contenttype.json into ContentTypeDefinition
var def map[string]ContentType
err := util.UnmarshalData(util.ConfigPath()+"/contenttype.json", &def)
if err != nil {
return err
}
for identifier, _ := range def {
cDef := def[identifier]
cDef.Init()
def[identifier] = cDef
}
contentTypeDefinition = map[string]map[string]ContentType{"default": def}
//todo: use config or scan folder.
//todo nb: the translation can be add later but listener should be there
loadTranslations([]string{"nor-NO", "eng-GB"})
return nil
}
//load translation based on existing definition
//todo: use locale folder
func loadTranslations(languages []string) {
for _, language := range languages {
var def map[string]ContentType
util.UnmarshalData(util.ConfigPath()+"/contenttype.json", &def)
//todo: formalize this: use folder, and loop through language
viper := viper.New()
viper.AddConfigPath(util.ConfigPath())
filename := "contenttype_" + language
viper.SetConfigName(filename)
viper.ReadInConfig()
viper.WatchConfig()
translationObj := viper.AllSettings()
str, _ := json.Marshal(translationObj)
var translation = map[string][]map[string]string{}
json.Unmarshal(str, &translation)
viper.OnConfigChange(func(e fsnotify.Event) {
if e.Name == util.ConfigPath()+"/"+filename+".json" {
log.Println("Translation changed. file: " + filename)
translationObj := viper.AllSettings()
str, _ = json.Marshal(translationObj)
json.Unmarshal(str, &translation)
loadTranslation(def, translation)
}
})
loadTranslation(def, translation)
//set language related definition
contentTypeDefinition[language] = def
}
}
func loadTranslation(def map[string]ContentType, translation map[string][]map[string]string) {
for contenttype, contenttypeDef := range def {
translist, ok := translation[contenttype]
if !ok {
continue
}
origFields := contentTypeDefinition["default"][contenttype].FieldMap
contenttypeDef.Init(func(field *FieldDef) {
//translate name
context := "field/" + field.Identifier + "/name"
value := getTranslation(context, translist)
origField := origFields[field.Identifier]
if value != "" {
field.Name = value
} else {
field.Name = origField.Name
}
//translate description
context = "field/" + field.Identifier + "/description"
value = getTranslation(context, translist)
if value != "" {
field.Description = value
} else {
field.Description = origField.Description
}
//translate parameters
for key, param := range field.Parameters {
switch param.(type) {
case string:
value = getTranslation("field/"+field.Identifier+"/parameters/"+key, translist)
if value != "" {
field.Parameters[key] = value
} else {
field.Parameters[key] = origField.Parameters[key]
}
break
}
}
})
def[contenttype] = contenttypeDef
}
}
func getTranslation(context string, translist []map[string]string) string {
value := ""
for i := range translist {
if translist[i]["context"] == context {
value = translist[i]["translation"]
break
}
}
return value
}
func GetDefinitionList() ContentTypeList {
return contentTypeDefinition
}
//Get a definition of a contenttype
func GetDefinition(contentType string, language ...string) (ContentType, error) {
languageStr := "default"
if len(language) > 0 {
languageStr = language[0]
}
definition, ok := contentTypeDefinition[languageStr]
if !ok {
log.Println("Language " + languageStr + " doesn't exist. use default.")
definition = contentTypeDefinition["default"]
}
result, ok := definition[contentType]
if ok {
return result, nil
} else {
return ContentType{}, errors.New("Content type doesn't exist: " + contentType)
}
}
//Get fields based on path pattern including container,
//separated by /
//. eg. article/relations, report/step1
func GetFields(typePath string) (map[string]FieldDef, error) {
arr := strings.Split(typePath, "/")
def, err := GetDefinition(arr[0])
if err != nil {
return nil, err
}
if len(arr) == 1 {
return def.FieldMap, nil
} else {
//get first level field
name := arr[1]
var currentField FieldDef
for _, field := range def.Fields {
if field.Identifier == name {
currentField = field
}
}
if currentField.Identifier == "" {
return nil, errors.New(name + " doesn't exist.")
}
//get end level field
for i := 2; i < len(arr); i++ {
name = arr[i]
found := false
for _, field := range currentField.Children {
if field.Identifier == name {
currentField = field
found = true
break
}
}
if !found {
return nil, errors.New(name + " doesn't exist.")
}
}
if currentField.FieldType != "container" {
return nil, errors.New("End field is not a container")
}
//get subfields of end level
return currentField.GetSubFields(), nil
}
}
func init() {
err := LoadDefinition()
if err != nil {
log.Fatal(err.Error())
}
}