...
1BEGIN {
2 print("package " pkgname)
3 print("")
4 print("import (")
5 print(" k8sRuntime \"k8s.io/apimachinery/pkg/runtime\"")
6 print(" \"sigs.k8s.io/controller-runtime/pkg/conversion\"")
7 print(")")
8 print("")
9
10 # This chunk is all static; so why doesn't it go in a separate
11 # non-generated file? Well, it's just here to make the actual
12 # .ConvertFrom() and .ConvertTo() methods below simpler. They could be
13 # inlined in the actual methods--but in my (LukeShu's) opinion, that
14 # makes the AWK code too difficult to edit; so they're split out to
15 # here. But splitting them out further and moving them out of the AWK
16 # would be too much--it'd create more things to keep in-sync and
17 # smearing the implementation across multiple files would make things
18 # harder, not easier.
19 print("func convertFrom(src conversion.Hub, dst conversion.Convertible) error {")
20 print(" scheme := conversionScheme()")
21 print(" var cur k8sRuntime.Object = src")
22 print(" for i := len(conversionIntermediates) - 1; i >= 0; i-- {")
23 print(" gv := conversionIntermediates[i]")
24 print(" var err error")
25 print(" cur, err = scheme.ConvertToVersion(cur, gv)")
26 print(" if err != nil {")
27 print(" return err")
28 print(" }")
29 print(" }")
30 print(" return scheme.Convert(cur, dst, nil)")
31 print("}")
32 print("")
33 print("func convertTo(src conversion.Convertible, dst conversion.Hub) error {")
34 print(" scheme := conversionScheme()")
35 print(" var cur k8sRuntime.Object = src")
36 print(" for _, gv := range conversionIntermediates {")
37 print(" var err error")
38 print(" cur, err = scheme.ConvertToVersion(cur, gv)")
39 print(" if err != nil {")
40 print(" return err")
41 print(" }")
42 print(" }")
43 print(" return scheme.Convert(cur, dst, nil)")
44 print("}")
45 print("")
46
47 object=0
48}
49
50/\/\/ \+kubebuilder:object:root=true/ {
51 object=1
52}
53
54/^type \S+ struct/ && object {
55 if (!match($2, /List$/)) {
56 print "func(dst *" $2 ") ConvertFrom(src conversion.Hub) error { return convertFrom(src, dst) }"
57 print "func(src *" $2 ") ConvertTo(dst conversion.Hub) error { return convertTo(src, dst) }"
58 }
59 object=0
60}
View as plain text