-
Notifications
You must be signed in to change notification settings - Fork 1
/
circe.go
640 lines (506 loc) · 19.1 KB
/
circe.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package main
import "bufio"
import "os"
import "log"
import "fmt"
import "strings"
import "path"
import "reflect"
import "io/ioutil"
import "io"
import "errors"
import "go/parser"
import "go/token"
import "go/ast"
import "go/types"
import "unicode"
import (
"gopkg.in/yaml.v2"
)
func check(e error) {
if e != nil {
panic(e)
}
}
type dynimporter struct {
// paths which will be scanned for /<go package> during Import
paths []string
vendorDir string
}
var imported map[string]*types.Package = make(map[string]*types.Package)
func (importer dynimporter) Import(goPkgName string) (*types.Package, error) {
if v, ok := imported[goPkgName]; ok {
return v, nil
}
fmt.Println("\n\nImport requested", goPkgName)
if !strings.HasPrefix(goPkgName, "github.com/") && !strings.Contains(goPkgName, "k8s") {
fmt.Println("SKIPPING!", goPkgName)
return nil, errors.New("Skipping since it is outside k8s/openshift")
}
fmt.Println("\n\nProcessing import", goPkgName)
fsetBase := token.NewFileSet()
for _, srcDir := range importer.paths {
checkDir := path.Join(srcDir, goPkgName)
_, err := os.Stat(checkDir)
if err != nil {
if os.IsNotExist(err) {
continue
} else {
return nil, errors.New("Error scanning for package: " + err.Error())
}
}
nextImporter := importer
// Once a src directory is found in a particular src directory, prepend the vendor source
// directory if one has been specified.
if len(importer.vendorDir) > 0 {
vendorPath := path.Join(srcDir, importer.vendorDir)
paths := append([]string{vendorPath}, importer.paths...)
nextImporter = dynimporter{
paths: paths,
vendorDir: "",
}
}
asts, err := parser.ParseDir(fsetBase, checkDir, nil, parser.ParseComments)
conf := types.Config{IgnoreFuncBodies: true, Importer: nextImporter, DisableUnusedImportCheck: true}
conf.Error = func(err error) {
log.Println("Error during check 2: ", err)
}
for name, pkgAst := range asts {
fmt.Printf("Processing pkgAst: %s", name)
// A package may contain, e.g. v1 and v1_test. Ignore the test package.
if strings.Contains(name, "_test") {
continue
}
var fs []*ast.File
for _, f := range pkgAst.Files {
fs = append(fs, f)
}
pkg, err := conf.Check(goPkgName, fsetBase, fs, nil)
if err != nil {
log.Println("Overall check error 2: ", err)
}
imported[goPkgName] = pkg
return pkg, nil
}
}
return nil, errors.New("Unable to find source package: " + goPkgName)
}
func toLowerCamelcase(name string) string {
runes := []rune(name)
newRunes := make([]rune, len(runes))
toggle := true
// TLS -> tls
// SomeValue -> someValue
// someValue -> someValue
for i, r := range runes {
if unicode.IsLower(r) {
toggle = false
}
if toggle && unicode.IsUpper(r) {
// If "TLSVerify", we want tlsVerify, so look ahead unless we are at the end
if i == 0 || len(runes) == i+1 || unicode.IsLower(runes[i+1]) == false {
newRunes[i] = unicode.ToLower(r)
} else {
newRunes[i] = r
}
} else {
newRunes[i] = r
}
}
return string(newRunes)
}
func copy(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}
type StructGen struct {
goPkgDir string
javaPkgDir string
pkg string
beanPkg string
config ModelConfig
outputDone map[string]bool
}
// If there is a direct mapping or helper class for a type in Java land, add it to this
// map so that no effort will be made trying to map the structure into Java.
var simpleJavaTypeMap = map[string]string{
"RawExtension": "Bean",
"Quantity": "Quantity",
"Secret": "Secret",
"Interface": "Bean",
"Duration": "Duration",
}
func out(depth int, msg string) {
for i := 0; i < depth; i++ {
fmt.Print("\t")
}
fmt.Println(msg)
}
func (sg *StructGen) getJavaType(depth int, typ types.Type) string {
typeSplit := strings.Split(typ.String(), ".")
typeName := typeSplit[len(typeSplit)-1] // networkingconfig_types.NetworkConfig -> NetworkConfig ; uint32 -> uint32
typeName = strings.Trim(typeName, "*") // ignore pointer vs non-pointer
out(depth, "Attempt to coerce type to java: "+typeName)
if simpleType, ok := simpleJavaTypeMap[typeName]; ok {
out(depth, " Coerced to simple type: "+simpleType)
return simpleType
}
switch ct := typ.(type) {
case *types.Basic:
if strings.HasPrefix(typeName, "uint") || strings.HasPrefix(typeName, "int") || strings.HasPrefix(typeName, "rune") || strings.HasPrefix(typeName, "byte") {
// We're not trying to match exact int size. That is left of to the user.
return "Long"
} else if typeName == "string" {
return "String"
} else if typeName == "bool" {
return "Boolean"
} else if strings.HasPrefix(typeName, "float") {
return "Double"
} else {
panic(fmt.Sprintf("I don't know how to translate: %s %T", ct.String(), typ))
panic("I don't know how to translate type: " + typeName)
}
case *types.Slice:
return "List<" + sg.getJavaType(depth, ct.Elem()) + ">"
case *types.Array:
return "List<" + sg.getJavaType(depth, ct.Elem()) + ">"
case *types.Map:
return "Map<" + sg.getJavaType(depth, ct.Key()) + "," + sg.getJavaType(depth, ct.Elem()) + ">"
case *types.Pointer:
return sg.getJavaType(depth, ct.Elem())
case *types.Named:
// e.g. 'type SomeNamedType struct' OR 'type SomeNamedType string|uint32...' <==basic
switch ut := ct.Underlying().(type) {
case *types.Struct:
out(depth, " Coercing to complex struct...")
sg.outputStruct(depth+1, typeName, ut, "")
return typeName
default:
out(depth, " Coercing to java type")
return sg.getJavaType(depth, ut)
}
default:
panic(fmt.Sprintf("I don't know how to translate type: %s", reflect.TypeOf(typ)))
}
}
func underscoreVersion(version string) string {
return strings.Replace(version, ".", "_", -1)
}
func (sg *StructGen) outputStruct(depth int, structName string, underlyingStruct *types.Struct, classNameOverride string) string {
goPkgSplit := strings.Split(strings.TrimRight(sg.goPkgDir, "/"), "/")
apiVersion := sg.config.KubeVersion
if len(apiVersion) == 0 {
apiVersion = goPkgSplit[len(goPkgSplit)-1]
if strings.HasPrefix(apiVersion, "v") == false {
panic("Unable to autodetect apiVersion for package (add kube_version in guide.yaml): " + sg.config.PkgDir)
}
}
version_underscore := underscoreVersion(apiVersion)
modulePackage := fmt.Sprintf("%s.%s", sg.pkg, version_underscore)
modulePkgDir := path.Join(sg.javaPkgDir, version_underscore)
structId := modulePackage + "." + structName
_, ok := sg.outputDone[structId]
if ok {
// if the struct has already been output for this package
return modulePackage
} else {
sg.outputDone[structId] = true
}
if len(classNameOverride) == 0 {
classNameOverride = structName
}
os.MkdirAll(modulePkgDir, 0755)
javaFilename := path.Join(modulePkgDir, classNameOverride+".java")
out(depth, " Opening file: "+javaFilename)
javaFile, err := os.OpenFile(javaFilename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0750)
check(err)
defer javaFile.Close()
jw := bufio.NewWriter(javaFile)
jw.WriteString("// GENERATED FILE -- DO NOT ALTER (circe.go)\n\n")
jw.WriteString(fmt.Sprintf("package %s;\n", modulePackage))
jw.WriteString("import " + sg.beanPkg + ".*;\n")
jw.WriteString("import com.github.openshift.circe.yaml.*;\n")
jw.WriteString("import java.util.*;\n\n")
jw.WriteString(fmt.Sprintf("public interface %s extends Bean {\n\n", classNameOverride))
ezDefaults := make([]string, 0)
writeGetterMethodSig := func(methodType string, fieldName string, inline bool, jsonName string) {
jw.WriteString(fmt.Sprintf("\t@YamlPropertyName(value=%q)\n", jsonName))
ezDefaults = append(ezDefaults, fmt.Sprintf("\t\t@YamlPropertyName(value=%q)\n", jsonName))
if inline {
jw.WriteString("\t@YamlPropertyInline\n")
ezDefaults = append(ezDefaults, "\t\t@YamlPropertyInline\n")
}
jw.WriteString(fmt.Sprintf("\t%s get%s() throws Exception;\n\n", methodType, fieldName))
ezDefaults = append(ezDefaults, fmt.Sprintf("\t\tdefault %s get%s() throws Exception { return null; }\n\n", methodType, fieldName))
}
for fi := 0; fi < underlyingStruct.NumFields(); fi++ {
fieldVar := underlyingStruct.Field(fi)
out(depth, fmt.Sprintf("Processing field: %s", fieldVar))
out(depth, "Testing: "+fieldVar.Type().String())
if strings.HasSuffix(fieldVar.Type().String(), "TypeMeta") {
out(depth, "Skipping TypeMeta")
jw.WriteString(fmt.Sprintf("\tdefault String getKind() { return %q; }\n", structName))
if len(sg.config.KubeGroup) > 0 {
apiVersion = sg.config.KubeGroup + "/" + apiVersion
}
jw.WriteString(fmt.Sprintf("\tdefault String getApiVersion() { return %q; }\n", apiVersion))
continue
}
if strings.HasSuffix(fieldVar.Type().String(), "ObjectMeta") {
out(depth, "Processing ObjectMeta")
jw.WriteString("\t@YamlPropertyIgnore\n")
jw.WriteString(fmt.Sprintf("\tdefault String _getGeneratorNamespaceHint() { return %q; }\n", sg.config.KubeNamespace))
jw.WriteString("\t@YamlPropertyIgnore\n")
jw.WriteString(fmt.Sprintf("\tdefault String _getGeneratorNameHint() { return %q; }\n", sg.config.KubeName))
if sg.config.List || sg.config.Map {
jw.WriteString("\tObjectMeta getMetadata() throws Exception;\n")
} else {
jw.WriteString("\tdefault ObjectMeta getMetadata() throws Exception { return new ObjectMeta(_getGeneratorNamespaceHint(), _getGeneratorNameHint()); }\n")
}
continue
}
var tag reflect.StructTag = reflect.StructTag(underlyingStruct.Tag(fi))
jsonTag := tag.Get("json")
jsonName := strings.Split(jsonTag, ",")[0]
if len(jsonName) == 0 {
jsonName = toLowerCamelcase(fieldVar.Name())
}
if len(jsonName) > 0 {
if strings.HasSuffix(fieldVar.Type().String(), "runtime.Object") {
if strings.HasPrefix(fieldVar.Type().String(), "[]") {
writeGetterMethodSig("List<Bean>", fieldVar.Name(), false, jsonName)
} else {
writeGetterMethodSig("Bean", fieldVar.Name(), false, jsonName)
}
continue
}
out(depth, "Found jsonName: "+jsonName)
if jsonName == "status" {
out(depth, "Skipping status field")
continue
}
if jsonName == "-" {
// https://github.com/openshift/origin/blob/29f688498ec658a22daac7dbe37502d05b7af64d/pkg/image/apiserver/admission/apis/imagepolicy/v1/types.go#L119
out(depth, "Skipping internal field")
continue
}
javaType := sg.getJavaType(depth, fieldVar.Type())
inline := fieldVar.Anonymous() || strings.Contains(jsonTag, ",inline")
writeGetterMethodSig(javaType, fieldVar.Name(), inline, jsonName)
} else {
panic(fmt.Sprintf("Unable to find json name for: %s", fieldVar.String()))
}
out(depth, fieldVar.String())
out(depth, "")
}
jw.WriteString(fmt.Sprintf("\tinterface EZ extends %s {\n\n", classNameOverride))
for _, ezDefault := range ezDefaults {
jw.WriteString(ezDefault)
}
jw.WriteString("\t}\n\n") // close EZ interface
jw.WriteString("}\n") // close 'public interface ... {'
jw.Flush()
return modulePackage
}
type ModelConfig struct {
// Override the name of the Java class name
Class string `yaml:"class"`
// The go package
PkgDir string `yaml:"package"`
// Vendor directory with which to satisfy dependencies
VendorDir string `yaml:"vendor"`
// The name of the go type to model
GoType string `yaml:"go_type"`
// Change the name of the generate java method
InterfaceMethodName string `yaml:"interface_method_name"`
KubeGroup string `yaml:"kube_group"`
KubeVersion string `yaml:"kube_version"`
KubeName string `yaml:"kube_name"`
KubeNamespace string `yaml:"kube_namespace"`
ModelOnly bool `yaml:"model_only"`
List bool `yaml:"list"`
Map bool `yaml:"map"`
// Elements that should only be modeled
SubModels []ModelConfig `yaml:"sub_models"`
}
type Unit struct {
// The name of the Java interface that must be implemented for the Unit
Class string `yaml:"class"`
// Human name for the Unit
Name string `yaml:"name"`
// Which version of OpenShift the Unit is appropriate for
Version string `yaml:"version"`
// The ModelConfig elements that make up the Unit
Elements []ModelConfig `yaml:"elements"`
JavaImports []string `yaml:"imports"`
}
type GuideYaml struct {
Units []Unit `yaml:"units"`
}
func main() {
yamlFile, err := ioutil.ReadFile("guide.yaml")
check(err)
guide := GuideYaml{}
yaml.Unmarshal(yamlFile, &guide)
fmt.Println(fmt.Sprintf("Found %d units", len(guide.Units)))
outputDir := "render/src/generated/java"
os.RemoveAll(outputDir)
basePkg := "com.github.openshift.circe.gen"
basePackageDir := path.Join(outputDir, strings.Replace(basePkg, ".", "/", -1))
beanPkg := "com.github.openshift.circe.beans"
gopath := os.Getenv("GOPATH")
if len(gopath) == 0 {
fmt.Println("Please set GOPATH environment variable before running (colon delimited list is supported)")
os.Exit(1)
}
// Each entry in the gopath should have a 'src' directory within it. Populate a list of directories the
// importer should search.
importerPaths := make([]string, 0)
for _, entry := range strings.Split(gopath, ":") {
importerPaths = append(importerPaths, path.Join(entry, "src"))
}
fmt.Println(fmt.Sprintf("Loaded go paths: %v", importerPaths))
os.MkdirAll(basePackageDir, 0750)
unitsEnumType, err := os.OpenFile(path.Join(basePackageDir, "UnitType.java"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0750)
check(err)
unitEnumWriter := bufio.NewWriter(unitsEnumType)
unitEnumWriter.WriteString("// GENERATED FILE -- DO NOT ALTER (circe.go)\n\n")
unitEnumWriter.WriteString("package " + basePkg + ";\n\n")
unitEnumWriter.WriteString("\npublic enum UnitType {\n\n")
for _, unit := range guide.Units {
className := unit.Class
// Within a unit, definitions can be ordered by the renderer to ensure they are populated
// on the cluster in specific order. Their order in the source yaml is honored.
renderOrderHint := 1
renderModel := func(modelConfig ModelConfig) {
// If the go type has a Java type already associated, don't bother generating java
if _, ok := simpleJavaTypeMap[modelConfig.GoType]; ok {
return
}
// Create an importer that will search go elements for the main package
// Also specify vendor directory which element may have specified.
importer := dynimporter{
paths: importerPaths,
vendorDir: modelConfig.VendorDir,
}
goPkgDir := modelConfig.PkgDir
pkg, err := importer.Import(goPkgDir)
check(err)
className := modelConfig.GoType
if len(modelConfig.Class) > 0 {
className = modelConfig.Class
}
shortPkgName := strings.ToLower(className)
packageName := fmt.Sprintf("%s.%s", basePkg, shortPkgName)
javaPkgDir := path.Join(basePackageDir, shortPkgName)
os.MkdirAll(javaPkgDir, 0750)
scope := pkg.Scope()
obj := scope.Lookup(modelConfig.GoType)
fmt.Println("Loaded", modelConfig.GoType, "=>", obj.String())
named := obj.Type().(*types.Named) // .Type() returns the type of the language element. We assume it is a named type.
underlyingStruct := named.Underlying().(*types.Struct) // The underlying type of the object should be a struct
structName := obj.Name() // obj.Name() example: "NetworkConfig"
sg := StructGen{
goPkgDir: goPkgDir,
javaPkgDir: javaPkgDir,
pkg: packageName,
beanPkg: beanPkg,
config: modelConfig,
outputDone: make(map[string]bool),
}
out(0, "Processing unit: "+unit.Name+"=>"+structName)
modulePackage := sg.outputStruct(1, structName, underlyingStruct, modelConfig.Class)
unit.JavaImports = append(unit.JavaImports, modulePackage)
}
fmt.Println("Generating unit: " + className)
for _, modelConfig := range unit.Elements {
renderModel(modelConfig)
for _, sub := range modelConfig.SubModels {
sub.ModelOnly = true
renderModel(sub)
}
}
unitUnderscoreVersion := underscoreVersion(unit.Version)
unitPkgDir := path.Join(basePackageDir, "units", unitUnderscoreVersion)
unitPkg := strings.Join([]string{basePkg, "units", unitUnderscoreVersion}, ".")
unitClassName := strings.Join([]string{basePkg, "units", unitUnderscoreVersion, className, "class"}, ".")
unitHumanName := fmt.Sprintf("%s-%s", unit.Version, unit.Name)
os.MkdirAll(unitPkgDir, 0755)
javaFile, err := os.OpenFile(path.Join(unitPkgDir, className+".java"), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0750)
check(err)
unitEnumWriter.WriteString(fmt.Sprintf("\t%s_%s(%s, %q, %q),\n", unitUnderscoreVersion, unit.Name, unitClassName, unit.Name, unitHumanName))
jw := bufio.NewWriter(javaFile)
jw.WriteString("// GENERATED FILE -- DO NOT ALTER (circe.go)\n\n")
jw.WriteString("package " + unitPkg + ";\n\n")
jw.WriteString("import java.util.*;\n")
jw.WriteString("import com.github.openshift.circe.yaml.*;\n")
for _, packageName := range unit.JavaImports {
jw.WriteString("import " + packageName + ".*;\n")
}
jw.WriteString("import " + beanPkg + ".*;\n")
ezDefaults := make([]string, 0)
writeMethodSig := func(methodType string, methodName string) {
ezDefaults = append(ezDefaults, fmt.Sprintf("\t\t@RenderOrder(value =\"%04d\")\n", renderOrderHint))
ezDefaults = append(ezDefaults, fmt.Sprintf("\t\tdefault %s %s() throws Exception { return null; }\n\n", methodType, methodName))
jw.WriteString(fmt.Sprintf("\t@RenderOrder(value =\"%04d\")\n", renderOrderHint))
jw.WriteString(fmt.Sprintf("\t%s %s() throws Exception;\n\n", methodType, methodName))
renderOrderHint = renderOrderHint + 1
}
jw.WriteString("\npublic interface " + className + " extends UnitBase {\n\n")
for _, oc := range unit.Elements {
if oc.ModelOnly == false {
className := oc.GoType
if len(oc.Class) > 0 {
className = oc.Class
}
methodName := "get" + className
if len(oc.InterfaceMethodName) > 0 {
methodName = oc.InterfaceMethodName
}
javaType := className
if oc.List {
javaType = "KubeList<" + className + ">"
methodName = methodName + "List"
} else if oc.Map {
javaType = "Map<String," + className + ">"
methodName = methodName + "Map"
}
writeMethodSig(javaType, methodName)
}
}
jw.WriteString(fmt.Sprintf("\tinterface EZ extends %s {\n\n", className))
for _, ezDefault := range ezDefaults {
jw.WriteString(ezDefault)
}
jw.WriteString("\t}\n\n") // close EZ interface
jw.WriteString("\n}\n")
jw.Flush()
javaFile.Close()
}
unitEnumWriter.WriteString("\t;\n\n") // end the enum element list
unitEnumWriter.WriteString("\tpublic Class<?> mustImplementClass;\n\n")
unitEnumWriter.WriteString("\tpublic String unitName;\n\n")
unitEnumWriter.WriteString("\tpublic String humanName;\n\n")
unitEnumWriter.WriteString("\tUnitType(Class<?> mustImplementClass, String unitName, String humanName) { this.mustImplementClass = mustImplementClass; this.unitName = unitName; this.humanName = humanName; }\n")
unitEnumWriter.WriteString("\n}\n")
unitEnumWriter.Flush()
unitsEnumType.Close()
return
}