-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_utils.go
398 lines (343 loc) · 9.18 KB
/
generate_utils.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package generate
import (
"fmt"
"regexp"
"strconv"
"strings"
"unicode"
"github.com/swaggest/jsonschema-go"
)
var Imports []string = []string{"time"}
var GoPrimitive map[string]bool = map[string]bool{
"string": true,
"bool": true,
"float64": true,
"int": true,
}
var PrimitiveAndCustomTypes map[string]string = map[string]string{
"string": "string",
"boolean": "bool",
"number": "float64",
"integer": "int",
"date-time": "time.Time",
}
var ComplexTypes map[string]string = map[string]string{
"array": "[]",
}
var RefsToIgnore map[string]bool = map[string]bool{
"#json-schema-directive": true,
}
var KeysToIgnore map[string]bool = map[string]bool{
"$schema": true,
}
var Aliases map[string][]string = map[string][]string{
"OscalCompleteSchema": {"OscalModels"},
}
const headerComment string = `/*
This file was auto-generated with go-oscal.
To regenerate:
go-oscal generate \
--input-file <path_to_oscal_json_schema_file> \
--output-file <name_of_go_types_file> // the path to this file must already exist \
--tags json,yaml // the tags to add to the Go structs \
--pkg <name_of_your_go_package> // defaults to "main"
For more information on how to use go-oscal: go-oscal --help
Source: https://github.com/defenseunicorns/go-oscal
*/`
// commonInitialisms is a set of common initialisms.
// Only add entries that are highly unlikely to be non-initialisms.
// For instance, "ID" is fine (Freudian code is rare), but "AND" is not.
var commonInitialisms = map[string]bool{
"API": true,
"ASCII": true,
"CPU": true,
"CSS": true,
"DNS": true,
"EOF": true,
"GUID": true,
"HTML": true,
"HTTP": true,
"HTTPS": true,
"ID": true,
"IP": true,
"JSON": true,
"LHS": true,
"QPS": true,
"RAM": true,
"RHS": true,
"RPC": true,
"SLA": true,
"SMTP": true,
"SSH": true,
"TLS": true,
"TTL": true,
"UI": true,
"UID": true,
"UUID": true,
"URI": true,
"URL": true,
"UTF8": true,
"VM": true,
"XML": true,
"NTP": true,
"DB": true,
}
var intToWordMap = []string{
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
}
// addPointerIfOptionalNonPrimitive adds a pointer to the type if the field is optional
func addPointerIfOptionalNonPrimitive(required bool, t string) string {
if required || GoPrimitive[t] {
return t
}
return "*" + t
}
// buildStructs builds the structs for each definition in the schema
func buildTagString(tags []string, field string, required bool) string {
tagStrings := []string{}
tagSuffix := ",omitempty"
// No tags, return empty string
if len(tags) == 0 {
return ""
}
// If required, remove omitempty
if required {
tagSuffix = ""
}
// Build tag string for each tag
for _, tag := range tags {
tagStrings = append(tagStrings, fmt.Sprintf("%s:\"%s%s\"", tag, field, tagSuffix))
}
return "`" + strings.Join(tagStrings, " ") + "`"
}
// getRef builds a ref string from a schema
func getRef(schema jsonschema.Schema) (string, error) {
if schema.Ref != nil {
return *schema.Ref, nil
} else if schema.ID != nil {
return *schema.ID, nil
} else if schema.Title != nil {
return getRefWithName(getNameFromTitle(*schema.Title)), nil
}
return "", fmt.Errorf("unable to get ref from schema")
}
func getNameFromTitle(title string) string {
split := strings.Split(title, " ")
name := ""
for _, s := range split {
name += FmtFieldName(s)
}
return name
}
func getRefWithName(name string) string {
return "#/definitions/" + name
}
// getCustomTypeKey returns the custom type key if the schema has a custom type
func getCustomTypeKey(schema jsonschema.Schema) string {
// If the schema has a format, check if it's a custom type
if schema.Format != nil {
if _, ok := PrimitiveAndCustomTypes[*schema.Format]; ok {
return *schema.Format
}
}
return ""
}
// getJsonOrCustomType returns the json type of the schema
func getJsonOrCustomType(schema jsonschema.Schema) string {
// if the schema has a custom type, return the custom type
if importType := getCustomTypeKey(schema); importType != "" {
return importType
}
if schema.Type != nil {
return string(*schema.Type.SimpleTypes)
}
return ""
}
// isPrimitiveOrCustomJsonType returns true if the type is a primitive type or a custom type
func isPrimitiveOrCustomJsonType(t string) bool {
lower := strings.ToLower(t)
return PrimitiveAndCustomTypes[lower] != ""
}
// getGoType returns the Go type for a given JSON type
func getGoType(t string) string {
lower := strings.ToLower(t)
if PrimitiveAndCustomTypes[lower] != "" {
return PrimitiveAndCustomTypes[lower]
}
return ComplexTypes[lower]
}
// populateSchema unmarshals the OSCAL JSON schema file into a jsonschema.Schema object
func populateSchema(oscalSchema []byte) (jsonschema.Schema, error) {
schema := jsonschema.Schema{}
err := schema.UnmarshalJSON(oscalSchema)
return schema, err
}
// getNameFromRef returns the name of the struct from a ref
func getNameFromRef(ref string) string {
pattern := regexp.MustCompile("[/_]")
splitPathSeperator := pattern.Split(ref, -1)
splitFileExt := strings.Split(splitPathSeperator[len(splitPathSeperator)-1], ".")
return FmtFieldName(splitFileExt[0])
}
// getDefinitionMap generates a map of definitions from the OSCAL JSON schema file.
// The key is the $id or $ref, and the value is the schemaOrBool object.
func getDefinitionMap(schema jsonschema.Schema) (map[string]jsonschema.Schema, error) {
result := make(map[string]jsonschema.Schema)
for definition, item := range schema.Definitions {
typeObj := *item.TypeObject
// If the object has no Parent, set the parent to the schema
if typeObj.Parent == nil {
typeObj.Parent = &schema
}
// If the object has an ID, use that as the key, otherwise use the definition name
if typeObj.ID != nil {
result[*item.TypeObject.ID] = typeObj
} else {
result["#/definitions/"+definition] = typeObj
}
}
if len(result) == 0 {
return nil, fmt.Errorf("no definitions found in the OSCAL JSON schema file, please verify the OSCAL JSON schema file is valid")
}
return result, nil
}
/*
FmtFieldName formats a string as a struct key
Example:
FmtFieldName("foo_id")
Output: FooID
*/
func FmtFieldName(s string) string {
runes := []rune(s)
for len(runes) > 0 && !unicode.IsLetter(runes[0]) && !unicode.IsDigit(runes[0]) {
runes = runes[1:]
}
if len(runes) == 0 {
return "_"
}
s = stringifyFirstChar(string(runes))
name := lintFieldName(s)
runes = []rune(name)
for i, c := range runes {
ok := unicode.IsLetter(c) || unicode.IsDigit(c)
if i == 0 {
ok = unicode.IsLetter(c)
}
if !ok {
runes[i] = '_'
}
}
s = string(runes)
s = strings.Trim(s, "_")
if len(s) == 0 {
return "_"
}
result := ""
for _, v := range strings.Split(s, "_") {
runes := []rune(v)
runes[0] = unicode.ToUpper(runes[0])
result += string(runes)
}
return result
}
func lintFieldName(name string) string {
// Fast path for simple cases: "_" and all lowercase.
if name == "_" {
return name
}
allLower := true
for _, r := range name {
if !unicode.IsLower(r) {
allLower = false
break
}
}
if allLower {
runes := []rune(name)
if u := strings.ToUpper(name); commonInitialisms[u] {
copy(runes[0:], []rune(u))
} else {
runes[0] = unicode.ToUpper(runes[0])
}
return string(runes)
}
allUpperWithUnderscore := true
for _, r := range name {
if !unicode.IsUpper(r) && r != '_' {
allUpperWithUnderscore = false
break
}
}
if allUpperWithUnderscore {
name = strings.ToLower(name)
}
// Split camelCase at any lower->upper transition, and split on underscores.
// Check each word for common initialisms.
runes := []rune(name)
w, i := 0, 0 // index of start of word, scan
for i+1 <= len(runes) {
eow := false // whether we hit the end of a word
if i+1 == len(runes) {
eow = true
} else if runes[i+1] == '_' {
// underscore; shift the remainder forward over any run of underscores
eow = true
n := 1
for i+n+1 < len(runes) && runes[i+n+1] == '_' {
n++
}
// Leave at most one underscore if the underscore is between two digits
if i+n+1 < len(runes) && unicode.IsDigit(runes[i]) && unicode.IsDigit(runes[i+n+1]) {
n--
}
copy(runes[i+1:], runes[i+n+1:])
runes = runes[:len(runes)-n]
} else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) {
// lower -> non-lower
eow = true
}
i++
if !eow {
continue
}
// [w,i) is a word.
word := string(runes[w:i])
if u := strings.ToUpper(word); commonInitialisms[u] {
// All the common initialisms are ASCII,
// so we can replace the bytes exactly.
copy(runes[w:], []rune(u))
} else if strings.ToLower(word) == word {
// already all lowercase, and not the first word, so uppercase the first character.
runes[w] = unicode.ToUpper(runes[w])
}
w = i
}
return string(runes)
}
// convert first character ints to strings
func stringifyFirstChar(str string) string {
first := str[:1]
i, err := strconv.ParseInt(first, 10, 8)
if err != nil {
return str
}
return intToWordMap[i] + "_" + str[1:]
}
// buildImportString returns a string of imports for the Go file
func buildImportString() string {
imports := "import (\n"
for _, imp := range Imports {
imports += fmt.Sprintf("\t\"%s\"\n", imp)
}
imports += ")\n"
return imports
}