-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.go
378 lines (318 loc) · 10.6 KB
/
marshal.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
package declarations
import (
"bytes"
"fmt"
"io/fs"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/dave/jennifer/jen"
"github.com/go-git/go-billy/v5/util"
"github.com/korylprince/go-adm/git"
"github.com/korylprince/go-adm/replace"
"github.com/korylprince/go-adm/schema"
"github.com/korylprince/go-adm/text"
)
type EncodeOption func(*Encoder)
func WithReplacements(reps replace.Replacements) EncodeOption {
return func(e *Encoder) {
e.reps = reps
}
}
type Encoder struct {
f *jen.File
reps replace.Replacements
gn *schema.GlobalNamer
enums []jen.Code
structs []jen.Code
structMap map[string]string
}
func NewEncoder(f *jen.File, opts ...EncodeOption) *Encoder {
e := &Encoder{f: f, gn: schema.NewGlobalNamer(), structMap: make(map[string]string)}
for _, opt := range opts {
opt(e)
}
return e
}
func (e *Encoder) normalizeName(name string) string {
return e.reps.Replace(name)
}
func anyDictionaryType(key *schema.PayloadKey) jen.Code {
switch key.Type {
case schema.PayloadKeyTypeAny:
return jen.Any()
case schema.PayloadKeyTypeString:
return jen.String()
default:
panic(fmt.Errorf("ANY <dictionary>: unknown value type: %s", key.Type))
}
}
func (e *Encoder) schemaType(key *schema.PayloadKey) jen.Code {
var typ jen.Code = jen.Any()
switch key.Type {
case schema.PayloadKeyTypeString:
typ = jen.String()
case schema.PayloadKeyTypeInteger:
typ = jen.Int64()
case schema.PayloadKeyTypeReal:
typ = jen.Float64()
case schema.PayloadKeyTypeBoolean:
typ = jen.Bool()
case schema.PayloadKeyTypeDate:
typ = jen.Qual("time", "Time")
case schema.PayloadKeyTypeData:
typ = jen.Index().Byte()
case schema.PayloadKeyTypeArray:
if key.SubKeys[0].Type == schema.PayloadKeyTypeDictionary {
typ = jen.Index().Add(e.schemaType(key.SubKeys[0]))
} else {
// we want *[]string, not *[]*string, so we monkey patch the sub key's presence attribute
orig := key.SubKeys[0].Presence
key.SubKeys[0].Presence = schema.PayloadKeyPresenceRequired
typ = jen.Index().Add(e.schemaType(key.SubKeys[0]))
key.SubKeys[0].Presence = orig
}
case schema.PayloadKeyTypeDictionary:
if len(key.SubKeys) > 0 && key.SubKeys[0].Key == schema.KeyANY {
typ = jen.Map(jen.String()).Add(anyDictionaryType(key.SubKeys[0]))
} else {
typ = jen.Id(e.normalizeName(e.gn.KeyName(key)))
}
case schema.PayloadKeyTypeAny:
typ = jen.Any()
}
// make optional fields pointers
if key.Presence != schema.PayloadKeyPresenceRequired {
typ = jen.Op("*").Add(typ)
}
return typ
}
func (e *Encoder) renderEnum(parentName string, key *schema.PayloadKey) {
// render comment
if doc := strings.TrimSpace(key.Content); doc != "" {
e.enums = append(e.enums, jen.Comment(text.DocComment(key.Content)))
}
enumName := e.normalizeName(parentName + text.NormalizeName(key.Key))
// array with enum subkey
if key.Type == schema.PayloadKeyTypeArray && len(key.SubKeys) == 1 && len(key.SubKeys[0].Rangelist) > 0 {
key = key.SubKeys[0]
// render subkey comment
if doc := strings.TrimSpace(key.Content); doc != "" {
e.enums = append(e.enums, jen.Comment(text.DocComment(key.Content)))
}
}
// render type
switch key.Type {
case schema.PayloadKeyTypeString:
e.enums = append(e.enums, jen.Type().Id(enumName).String())
case schema.PayloadKeyTypeInteger:
e.enums = append(e.enums, jen.Type().Id(enumName).Int64())
default:
panic(fmt.Errorf("unknown key type: %s", key.Type))
}
// render consts
e.enums = append(e.enums, jen.Const().DefsFunc(func(g *jen.Group) {
for _, v := range key.Rangelist {
switch key.Type {
case schema.PayloadKeyTypeString:
g.Id(e.normalizeName(enumName + text.NormalizeName(v.String()))).Id(enumName).Op("=").Lit(v.String())
case schema.PayloadKeyTypeInteger:
g.Id(e.normalizeName(enumName + strconv.Itoa(int(v.Int64())))).Id(enumName).Op("=").Lit(int(v.Int64()))
}
}
}))
}
func (e *Encoder) renderSchema(s *schema.Schema) {
seen := make(map[*schema.PayloadKey]struct{})
var dfs func(parentName string, keys []*schema.PayloadKey) (fields []jen.Code)
dfs = func(parentName string, keys []*schema.PayloadKey) (fields []jen.Code) {
for _, key := range keys {
// render field comment
if doc := strings.TrimSpace(key.Content); doc != "" {
fields = append(fields, jen.Comment(text.DocComment(doc)))
}
// render json tag
tags := map[string]string{"json": key.Key}
if key.Presence == schema.PayloadKeyPresenceOptional {
tags["json"] = key.Key + ",omitempty"
}
// render required tag
if key.Presence == schema.PayloadKeyPresenceRequired {
tags["required"] = "true"
}
// render default tag
if key.Default.Value() != nil {
switch {
case key.Default.IsInt64():
tags["default"] = strconv.FormatInt(key.Default.Int64(), 10)
case key.Default.IsFloat64():
tags["default"] = strconv.FormatFloat(key.Default.Float64(), 'f', -1, 64)
case key.Default.IsString():
tags["default"] = key.Default.String()
}
}
// render field
fields = append(fields, jen.Id(e.normalizeName(text.NormalizeName(key.Key))).Add(e.schemaType(key)).Tag(tags))
// prevent infinite recursion
if _, ok := seen[key]; ok {
return
}
seen[key] = struct{}{}
// don't create structs for ANY <dictionary>s
if key.Type == schema.PayloadKeyTypeDictionary && len(key.SubKeys) == 1 && key.SubKeys[0].Key == schema.KeyANY {
return fields
}
if key.Type == schema.PayloadKeyTypeDictionary || (key.Type == schema.PayloadKeyTypeArray && len(key.SubKeys) == 1 && key.SubKeys[0].Type == schema.PayloadKeyTypeDictionary) {
keyName := e.normalizeName(e.gn.KeyName(key))
if keyName == "" {
keyName = e.normalizeName(text.NormalizeName(key.Key))
}
// recurse into fields
subfields := dfs(keyName, key.SubKeys)
// render struct for dictionary keys
if key.Type == schema.PayloadKeyTypeDictionary {
if doc := strings.TrimSpace(key.Content); doc != "" {
e.structs = append(e.structs, jen.Comment(text.DocComment(doc)))
}
e.structs = append(e.structs, jen.Type().Id(keyName).Struct(subfields...))
}
}
}
return fields
}
schemaName := e.normalizeName(e.gn.SchemaName(s))
// check if struct or ANY <dictionary>
isStruct := !(len(s.PayloadKeys) > 0 && s.PayloadKeys[0].Key == schema.KeyANY)
// recurse through payload keys, generating types along the way
var fields []jen.Code
if isStruct {
fields = dfs(schemaName, s.PayloadKeys)
}
// render schema comments
if doc := strings.TrimSpace(s.Description); doc != "" {
e.structs = append(e.structs, jen.Comment(text.DocComment(doc)))
}
if doc := strings.TrimSpace(s.Payload.Content); doc != "" {
e.structs = append(e.structs, jen.Comment(text.DocComment(doc)))
}
if isStruct {
// render code for schema struct itself
e.structs = append(e.structs, jen.Type().Id(schemaName).Struct(fields...))
} else {
// render comment for <dictionary> sub key
if doc := strings.TrimSpace(s.PayloadKeys[0].Content); doc != "" {
e.structs = append(e.structs, jen.Comment(text.DocComment(doc)))
}
// render map type
e.structs = append(e.structs, jen.Type().Id(schemaName).Map(jen.String()).Add(
anyDictionaryType(s.PayloadKeys[0]),
))
}
// render code for DeclarativeType method
if s.Payload.DeclarationType != "" {
rcvr := jen.Id("p").Op("*").Id(schemaName)
// don't use pointer receiver for ANY <dictionary>s
if !isStruct {
rcvr = jen.Id("p").Id(schemaName)
}
e.structs = append(e.structs, jen.Func().Parens(rcvr).Id("DeclarationType").Parens(nil).String().Block(
jen.Return().Lit(s.Payload.DeclarationType),
))
// add struct to id -> type map
e.structMap[schemaName] = s.Payload.DeclarationType
}
}
func (e *Encoder) Encode(schemas ...*schema.Schema) []byte {
e.gn.Register(schemas...)
// render enums
for _, s := range schemas {
seen := make(map[*schema.PayloadKey]struct{})
s.Iter(func(parent, key *schema.PayloadKey) {
// prevent seeing array enum twice
if _, ok := seen[key]; ok {
return
}
parentName := e.gn.SchemaName(s)
if parent != nil {
parentName = e.gn.KeyName(parent)
}
// normal enum
if len(key.Rangelist) > 0 {
e.renderEnum(parentName, key)
}
// array with enum subkey
if key.Type == schema.PayloadKeyTypeArray && len(key.SubKeys) == 1 && len(key.SubKeys[0].Rangelist) > 0 {
e.renderEnum(parentName, key)
seen[key.SubKeys[0]] = struct{}{}
}
})
}
for _, s := range schemas {
e.renderSchema(s)
}
e.f.Var().Id("DeclarationMap").Op("=").Map(jen.String()).Any().Values(jen.DictFunc(func(d jen.Dict) {
for typ, id := range e.structMap {
d[jen.Lit(id)] = jen.Id(typ).Values()
}
}))
for _, stmt := range append(e.enums, e.structs...) {
e.f.Add(stmt)
}
return nil
}
// GenerateFromGit generates Go types from the declarative schema at the git repo/commit/path using the optional replacements
// and outputs it at the given directory
func GenerateFromGit(repoURL, commit, path string, reps replace.Replacements, output string) error {
repo, err := git.New(repoURL, commit)
if err != nil {
return fmt.Errorf("could not check out repository: %w", err)
}
hash, err := repo.Hash()
if err != nil {
return fmt.Errorf("could not get hash: %w", err)
}
dirs, err := repo.ReadDir(path)
if err != nil {
return fmt.Errorf("could not read %s: %w", path, err)
}
for _, dir := range dirs {
if !dir.IsDir() {
continue
}
var schemas []*schema.Schema
if err = util.Walk(repo, repo.Join(path, dir.Name()), func(filepath string, info fs.FileInfo, _ error) error {
if !strings.HasSuffix(info.Name(), ".yaml") {
return nil
}
buf, err := util.ReadFile(repo, filepath)
if err != nil {
return fmt.Errorf("could not read %s: %w", filepath, err)
}
s, err := schema.New(buf)
if err != nil {
return fmt.Errorf("could not parse %s: %w", filepath, err)
}
schemas = append(schemas, s)
return nil
}); err != nil {
return err
}
f := jen.NewFile(dir.Name())
f.HeaderComment("DO NOT EDIT")
f.HeaderComment(fmt.Sprintf("generated from %s:%s/%s", repoURL, hash, filepath.Join(path, dir.Name())))
f.Const().Id("DeviceManagementGenerateHash").Op("=").Lit(hash)
NewEncoder(f, WithReplacements(reps)).Encode(schemas...)
buf := new(bytes.Buffer)
if err = f.Render(buf); err != nil {
return fmt.Errorf("could not render code: %w", err)
}
if err = os.MkdirAll(filepath.Join(output, dir.Name()), 0755); err != nil {
return fmt.Errorf("could not create output directory %s: %w", filepath.Join(".", dir.Name()), err)
}
if err = os.WriteFile(filepath.Join(output, dir.Name(), dir.Name()+".go"), buf.Bytes(), 0644); err != nil {
return fmt.Errorf("could not create write %s: %w", filepath.Join(output, dir.Name(), dir.Name()+".go"), err)
}
}
return nil
}