-
Notifications
You must be signed in to change notification settings - Fork 411
Expand file tree
/
Copy pathschema.go
More file actions
316 lines (264 loc) · 9.19 KB
/
schema.go
File metadata and controls
316 lines (264 loc) · 9.19 KB
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
package util
import (
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/gertd/go-pluralize"
"github.com/invopop/jsonschema"
"github.com/loft-sh/devspace/pkg/devspace/config/versions"
)
const nameFieldName = "name"
const versionFieldName = "version"
const groupKey = "group"
const groupNameKey = "group_name"
const prefixSeparator = "/"
const anchorSeparator = "-"
var pluralizeClient = pluralize.NewClient()
func GenerateSchema(configInstance interface{}, repository, configGoPackage string) *jsonschema.Schema {
r := new(jsonschema.Reflector)
r.AllowAdditionalProperties = true
r.PreferYAMLSchema = true
r.RequiredFromJSONSchemaTags = true
r.YAMLEmbeddedStructs = false
r.ExpandedStruct = true
if repository != "" && configGoPackage != "" {
err := r.AddGoComments(repository, configGoPackage)
if err != nil {
panic(err)
}
}
return r.Reflect(configInstance)
}
func GenerateReference(schema *jsonschema.Schema, basePath string) {
versionField, ok := schema.Properties.Get(versionFieldName)
if ok {
if fieldSchema, ok := versionField.(*jsonschema.Schema); ok {
versionEnum := []string{}
for version := range versions.VersionLoader {
versionEnum = append(versionEnum, version)
}
sort.SliceStable(versionEnum, func(a, b int) bool {
majorA, _ := strconv.Atoi(string(versionEnum[a][1]))
majorB, _ := strconv.Atoi(string(versionEnum[b][1]))
minorA, _ := strconv.Atoi(string(versionEnum[a][6:]))
minorB, _ := strconv.Atoi(string(versionEnum[b][6:]))
if majorA == majorB {
return minorA > minorB
} else {
return majorA > majorB
}
})
fieldSchema.Enum = []interface{}{}
for _, version := range versionEnum {
fieldSchema.Enum = append(fieldSchema.Enum, version)
}
}
}
createSections(basePath, "", schema, schema.Definitions, 1, false)
}
func createSections(basePath, prefix string, schema *jsonschema.Schema, definitions jsonschema.Definitions, depth int, parentIsNameObjectMap bool) string {
partialImports := &[]string{}
content := ""
headlinePrefix := headingPrefix(depth)
anchorPrefix := strings.TrimPrefix(strings.ReplaceAll(prefix, prefixSeparator, anchorSeparator), anchorSeparator)
groups := map[string]*Group{}
for _, fieldName := range schema.Properties.Keys() {
if parentIsNameObjectMap && fieldName == nameFieldName {
continue
}
field, ok := schema.Properties.Get(fieldName)
if ok {
if fieldSchema, ok := field.(*jsonschema.Schema); ok {
fieldContent := ""
fieldFile := fmt.Sprintf("%s%s%s.mdx", basePath, prefix, fieldName)
fieldFileReference := fieldFile
fieldType := "object"
isNameObjectMap := false
groupID, _ := fieldSchema.Extras[groupKey].(string)
expandable := false
var patternPropertySchema *jsonschema.Schema
var nestedSchema *jsonschema.Schema
ref := ""
if fieldSchema.Type == "array" {
ref = fieldSchema.Items.Ref
fieldType = "object[]"
} else if patternPropertySchema, ok = fieldSchema.PatternProperties[".*"]; ok {
ref = patternPropertySchema.Ref
isNameObjectMap = true
} else if fieldSchema.Ref != "" {
ref = fieldSchema.Ref
}
if ref != "" {
refSplit := strings.Split(ref, "/")
nestedSchema, ok = definitions[refSplit[len(refSplit)-1]]
if ok {
newPrefix := prefix + fieldName + prefixSeparator
createSections(basePath, newPrefix, nestedSchema, definitions, depth+1, isNameObjectMap)
fieldFileReference = fmt.Sprintf("%s%s%s_reference.mdx", basePath, prefix, fieldName)
fieldContent = GetPartialImport(fieldFileReference, fieldFile) + "\n\n" + fmt.Sprintf(TemplatePartialUse, GetPartialImportName(fieldFileReference))
expandable = true
}
}
required := contains(schema.Required, fieldName)
fieldDefault := ""
fieldType = fieldSchema.Type
if fieldType == "" && fieldSchema.OneOf != nil {
for _, oneOfType := range fieldSchema.OneOf {
if fieldType != "" {
fieldType = fieldType + "|"
}
fieldType = fieldType + oneOfType.Type
}
}
if isNameObjectMap {
fieldNameSingular := pluralizeClient.Singular(fieldName)
fieldType = "<" + fieldNameSingular + "_name>:"
if patternPropertySchema != nil && patternPropertySchema.Type != "" {
fieldType = fieldType + patternPropertySchema.Type
} else {
fieldType = fieldType + "object"
}
}
if fieldType == "array" {
if fieldSchema.Items.Type == "" {
fieldType = "object[]"
} else {
fieldType = fieldSchema.Items.Type + "[]"
}
}
fieldPartial := fmt.Sprintf(TemplatePartialUse, GetPartialImportName(fieldFileReference))
if ref != "" {
if isNameObjectMap && nestedSchema != nil {
nameField, ok := nestedSchema.Properties.Get(nameFieldName)
if ok {
if nameFieldSchema, ok := nameField.(*jsonschema.Schema); ok {
fieldNameSingular := pluralizeClient.Singular(fieldName)
nameFieldRequired := true
nameFieldDefault := ""
nameFieldEnumValues := GetEumValues(nameFieldSchema, nameFieldRequired, &nameFieldDefault)
anchorName := anchorPrefix + fieldName + anchorSeparator + nameFieldName
fieldPartial = fmt.Sprintf(TemplateConfigField, true, "open", headlinePrefix, "<"+fieldNameSingular+"_"+nameFieldName+">", nameFieldRequired, "string", nameFieldDefault, nameFieldEnumValues, anchorName, nameFieldSchema.Description, fieldPartial)
fieldType = "<" + fieldNameSingular + "_name>:object"
}
}
}
anchorName := anchorPrefix + fieldName
fieldContent = GetPartialImport(fieldFileReference, fieldFile) + "\n\n" + fmt.Sprintf(TemplateConfigField, true, " open", headlinePrefix, fieldName, false, fieldType, "", "", anchorName, fieldSchema.Description, fieldPartial)
fieldPartial = fmt.Sprintf(TemplateConfigField, true, "", headlinePrefix, fieldName, false, fieldType, "", "", anchorName, fieldSchema.Description, fieldPartial)
} else {
if fieldType == "boolean" {
if required {
fieldDefault = "true"
required = false
} else {
fieldDefault = "false"
boolDefault, ok := fieldSchema.Default.(bool)
if ok && boolDefault {
fieldDefault = "true"
}
}
} else if fieldType == "integer" {
intDefault, ok := fieldSchema.Default.(int)
if ok {
fieldDefault = strconv.Itoa(intDefault)
} else {
fieldDefault = ""
}
} else {
fieldDefault, ok = fieldSchema.Default.(string)
if !ok {
fieldDefault = ""
}
}
enumValues := GetEumValues(fieldSchema, required, &fieldDefault)
anchorName := anchorPrefix + fieldName
fieldContent = fmt.Sprintf(TemplateConfigField, expandable, " open", headlinePrefix, fieldName, required, fieldType, fieldDefault, enumValues, anchorName, fieldSchema.Description, fieldContent)
}
err := os.MkdirAll(filepath.Dir(fieldFileReference), os.ModePerm)
if err != nil {
panic(err)
}
err = os.WriteFile(fieldFile, []byte(fieldContent), os.ModePerm)
if err != nil {
panic(err)
}
if groupID != "" {
groupID = strings.ToLower(groupID)
group, ok := groups[groupID]
if !ok {
group = &Group{
File: fmt.Sprintf("%s%sgroup_%s.mdx", basePath, prefix, groupID),
Imports: &[]string{},
}
groups[groupID] = group
groupPartial := fmt.Sprintf(TemplatePartialUse, GetPartialImportName(group.File))
content = content + "\n\n" + groupPartial
*partialImports = append(*partialImports, group.File)
}
if groupName, ok := fieldSchema.Extras[groupNameKey]; ok {
group.Name = groupName.(string)
}
group.Content = group.Content + fieldPartial
*group.Imports = append(*group.Imports, fieldFileReference)
} else {
content = content + "\n\n" + fieldPartial
*partialImports = append(*partialImports, fieldFileReference)
}
}
}
}
ProcessGroups(groups)
if prefix == "" {
prefix = "reference"
} else {
prefix = strings.TrimSuffix(prefix, "/") + "_reference"
}
pageFile := fmt.Sprintf("%s%s.mdx", basePath, strings.TrimSuffix(prefix, "/"))
importContent := ""
for _, partialFile := range *partialImports {
importContent = importContent + GetPartialImport(partialFile, pageFile)
}
content = fmt.Sprintf("%s%s", importContent, content)
err := os.WriteFile(pageFile, []byte(content), os.ModePerm)
if err != nil {
panic(err)
}
return content
}
func headingPrefix(depth int) string {
headingLevel := depth + 1
if headingLevel > 6 {
headingLevel = 6
}
return strings.Repeat("#", headingLevel) + " "
}
func GetEumValues(fieldSchema *jsonschema.Schema, required bool, fieldDefault *string) string {
enumValues := ""
if fieldSchema.Enum != nil {
for i, enumVal := range fieldSchema.Enum {
enumValString, ok := enumVal.(string)
if ok {
if i == 0 && !required && *fieldDefault == "" {
*fieldDefault = enumValString
}
if enumValues != "" {
enumValues = enumValues + "<br/>"
}
enumValues = enumValues + enumValString
}
}
enumValues = fmt.Sprintf("<span>%s</span>", enumValues)
}
return enumValues
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}