-
Notifications
You must be signed in to change notification settings - Fork 8
/
funcs.go
205 lines (178 loc) · 5.63 KB
/
funcs.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
package render
import (
"strings"
"text/template"
"github.com/gertd/go-pluralize"
"bytes"
"encoding/json"
"github.com/BurntSushi/toml"
"github.com/Masterminds/sprig/v3"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/iancoleman/strcase"
"github.com/solo-io/skv2/codegen/model"
"github.com/solo-io/skv2/codegen/util"
"sigs.k8s.io/yaml"
)
func makeTemplateFuncs(customFuncs template.FuncMap) template.FuncMap {
f := sprig.TxtFuncMap()
// Add some functionality for skv2 templates
skv2Funcs := template.FuncMap{
// string utils
"toToml": toTOML,
"toYaml": toYAML,
"fromYaml": fromYAML,
"toJson": toJSON,
"fromJson": fromJSON,
"join": strings.Join,
"lower": strings.ToLower,
"lower_camel": strcase.ToLowerCamel,
"upper_camel": strcase.ToCamel,
"pluralize": pluralize.NewClient().Plural,
"snake": strcase.ToSnake,
"split": splitTrimEmpty,
"string_contains": strings.Contains,
// resource-related funcs
"group_import_path": func(grp Group) string {
return util.GoPackage(grp)
},
"group_import_name": func(grp Group) string {
name := strings.ReplaceAll(grp.GroupVersion.String(), "/", "_")
name = strings.ReplaceAll(name, ".", "_")
name = strings.ReplaceAll(name, "-", "_")
return name
},
"generated_code_import_path": func(grp Group) string {
return util.GeneratedGoPackage(grp)
},
// Used by types.go to get all unique external imports for a groups resources
"imports_for_group": func(grp Group) []string {
allImports := uniqueGoImportPathsForGroup(grp)
var excludingGroupImport []string
for _, imp := range allImports {
if imp == util.GoPackage(grp) {
continue
}
excludingGroupImport = append(excludingGroupImport, imp)
}
return excludingGroupImport
},
}
for k, v := range skv2Funcs {
f[k] = v
}
for k, v := range customFuncs {
f[k] = v
}
return f
}
/*
Find the proto messages for a given set of descriptors which need proto_deepcopoy funcs and whose types are not in
the API root package
return true if the descriptor corresponds to the Spec or the Status field
*/
func shouldDeepCopyExternalMessage(resources []model.Resource, desc *descriptor.DescriptorProto) bool {
for _, resource := range resources {
if resource.Spec.Type.Name == desc.GetName() ||
(resource.Status != nil && resource.Status.Type.Name == desc.GetName()) {
return true
}
}
return false
}
/*
Find the proto messages for a given set of descriptors which need proto_deepcopoy funcs.
The two cases are as follows:
1. One of the subfields has an external type
2. There is a oneof present
*/
func shouldDeepCopyInternalMessage(packageName string, desc *descriptor.DescriptorProto) bool {
var shouldGenerate bool
// case 1 above
for _, v := range desc.GetField() {
if v.TypeName != nil && !strings.Contains(v.GetTypeName(), packageName) {
shouldGenerate = true
break
}
}
// case 2 above
return len(desc.GetOneofDecl()) > 0 || shouldGenerate
}
// toYAML takes an interface, marshals it to yaml, and returns a string. It will
// always return a string, even on marshal error (empty string).
//
// This is designed to be called from a template.
func toYAML(v interface{}) string {
// NOTE(ilackarms): due to a bug in the underlying yaml library
// inserting unnecessary newlines when marshalling string arrays,
// we handle that special-case here
if strSlice, ok := v.([]string); ok {
return strSliceToYaml(strSlice)
}
data, err := yaml.Marshal(v)
if err != nil {
// Swallow errors inside of a template.
return ""
}
return strings.TrimSuffix(string(data), "\n")
}
func strSliceToYaml(strSlice []string) string {
var yamlElements []string
for _, s := range strSlice {
yamlElements = append(yamlElements, "- "+s)
}
return strings.Join(yamlElements, "\n")
}
// fromYAML converts a YAML document into a map[string]interface{}.
//
// This is not a general-purpose YAML parser, and will not parse all valid
// YAML documents. Additionally, because its intended use is within templates
// it tolerates errors. It will insert the returned error message string into
// m["Error"] in the returned map.
func fromYAML(str string) map[string]interface{} {
m := map[string]interface{}{}
if err := yaml.Unmarshal([]byte(str), &m); err != nil {
m["Error"] = err.Error()
}
return m
}
// toTOML takes an interface, marshals it to toml, and returns a string. It will
// always return a string, even on marshal error (empty string).
//
// This is designed to be called from a template.
func toTOML(v interface{}) string {
b := bytes.NewBuffer(nil)
e := toml.NewEncoder(b)
err := e.Encode(v)
if err != nil {
return err.Error()
}
return b.String()
}
// toJSON takes an interface, marshals it to json, and returns a string. It will
// always return a string, even on marshal error (empty string).
//
// This is designed to be called from a template.
func toJSON(v interface{}) string {
data, err := json.Marshal(v)
if err != nil {
// Swallow errors inside of a template.
return ""
}
return string(data)
}
// fromJSON converts a JSON document into a map[string]interface{}.
//
// This is not a general-purpose JSON parser, and will not parse all valid
// JSON documents. Additionally, because its intended use is within templates
// it tolerates errors. It will insert the returned error message string into
// m["Error"] in the returned map.
func fromJSON(str string) map[string]interface{} {
m := make(map[string]interface{})
if err := json.Unmarshal([]byte(str), &m); err != nil {
m["Error"] = err.Error()
}
return m
}
func splitTrimEmpty(s, sep string) []string {
return strings.Split(strings.TrimSpace(s), sep)
}