-
Notifications
You must be signed in to change notification settings - Fork 19
/
templates.go
206 lines (179 loc) · 6.18 KB
/
templates.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
// Copyright 2022, Pulumi Corporation. All rights reserved.
package java
import (
tmplt "text/template"
)
const (
// MissingKeyErrorOption is the renderer option to stop execution immediately with an error on missing key
MissingKeyErrorOption = "missingkey=error"
)
func Template(name string, text string) *tmplt.Template {
return tmplt.Must(tmplt.New(name).Funcs(predefinedFunctions).Option(MissingKeyErrorOption).Parse(text))
}
var predefinedFunctions = tmplt.FuncMap{
"eq": func(x, y interface{}) bool {
return x == y
},
"sub": func(y, x int) int {
return x - y
},
}
// nolint:lll
const javaUtilitiesTemplateText = `
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import com.pulumi.core.internal.Environment;
import com.pulumi.deployment.InvokeOptions;
public class {{ .ClassName }} {
public static Optional<String> getEnv(String... names) {
for (var n : names) {
var value = Environment.getEnvironmentVariable(n);
if (value.isValue()) {
return Optional.of(value.value());
}
}
return Optional.empty();
}
public static Optional<Boolean> getEnvBoolean(String... names) {
for (var n : names) {
var value = Environment.getBooleanEnvironmentVariable(n);
if (value.isValue()) {
return Optional.of(value.value());
}
}
return Optional.empty();
}
public static Optional<Integer> getEnvInteger(String... names) {
for (var n : names) {
var value = Environment.getIntegerEnvironmentVariable(n);
if (value.isValue()) {
return Optional.of(value.value());
}
}
return Optional.empty();
}
public static Optional<Double> getEnvDouble(String... names) {
for (var n : names) {
var value = Environment.getDoubleEnvironmentVariable(n);
if (value.isValue()) {
return Optional.of(value.value());
}
}
return Optional.empty();
}
// TODO: this probably should be done via a mutator on the InvokeOptions
public static InvokeOptions withVersion(@Nullable InvokeOptions options) {
if (options != null && options.getVersion().isPresent()) {
return options;
}
return new InvokeOptions(
options == null ? null : options.getParent().orElse(null),
options == null ? null : options.getProvider().orElse(null),
getVersion()
);
}
private static final String version;
public static String getVersion() {
return version;
}
static {
var resourceName = "{{ .VersionPath }}/version.txt";
var versionFile = Utilities.class.getClassLoader().getResourceAsStream(resourceName);
if (versionFile == null) {
throw new IllegalStateException(
String.format("expected resource '%s' on Classpath, not found", resourceName)
);
}
version = new BufferedReader(new InputStreamReader(versionFile))
.lines()
.collect(Collectors.joining("\n"))
.trim();
}
}
`
var javaUtilitiesTemplate = Template("JavaUtilities", javaUtilitiesTemplateText)
type javaUtilitiesTemplateContext struct {
Name string
VersionPath string
ClassName string
Tool string
}
const getterTemplateText = `{{ .Indent }}public {{ .GetterType }} {{ .GetterName }}() {
{{ .Indent }} return {{ .ReturnStatement }};
{{ .Indent }}}`
var getterTemplate = Template("Getter", getterTemplateText)
type getterTemplateContext struct {
Indent string
GetterType string
GetterName string
ReturnStatement string
}
type builderSetterTemplateContext struct {
SetterName string
PropertyType string
PropertyName string
Assignment string
ListType string
Annotations []string
}
type builderFieldTemplateContext struct {
FieldType string
FieldName string
Initialization string
}
const builderTemplateText = `{{ .Indent }}public static {{ .Name }} builder() {
{{ .Indent }} return new {{ .Name }}();
{{ .Indent }}}
{{ .Indent }}public static {{ .Name }} builder({{ .ResultType }} defaults) {
{{ .Indent }} return new {{ .Name }}(defaults);
{{ .Indent }}}
{{- range $annotation := .Annotations }}
{{ $.Indent }}{{ $annotation }}
{{- end }}
{{ .Indent }}public static {{ if .IsFinal }}final {{ end }}class {{ .Name }} {
{{- range $field := .Fields }}
{{ $.Indent }} private {{ $field.FieldType }} {{ $field.FieldName }}{{ $field.Initialization }};
{{- end }}
{{ $.Indent }} public {{ .Name }}() {}
{{ $.Indent }} public {{ .Name }}({{ .ResultType }} defaults) {
{{ $.Indent }} {{ .Objects }}.requireNonNull(defaults);
{{- range $field := .Fields }}
{{ $.Indent }} this.{{ $field.FieldName }} = defaults.{{ $field.FieldName }};
{{- end }}
{{ $.Indent }} }
{{ range $setter := .Setters }}
{{- range $annotation := $setter.Annotations }}
{{ $.Indent }} {{ $annotation }}
{{- end }}
{{ $.Indent }} public {{ $.Name }} {{ $setter.SetterName }}({{ $setter.PropertyType }} {{ $setter.PropertyName }}) {
{{ $.Indent }} {{ $setter.Assignment }};
{{ $.Indent }} return this;
{{ $.Indent }} }
{{- if $setter.ListType }}
{{ $.Indent }} public {{ $.Name }} {{ $setter.SetterName }}({{ $setter.ListType }}... {{ $setter.PropertyName }}) {
{{ $.Indent }} return {{ $setter.SetterName }}(List.of({{ $setter.PropertyName }}));
{{ $.Indent }} }
{{- end -}}
{{ end }}
{{ $.Indent }} public {{ .ResultType }} build() {
{{ $.Indent }} final var o = new {{ .ResultType }}();
{{- range $i, $field := .Fields }}
{{ $.Indent }} o.{{ $field.FieldName }} = {{ $field.FieldName }};
{{- end }}
{{ $.Indent }} return o;
{{ .Indent }} }
{{ .Indent }}}`
var builderTemplate = Template("Builder", builderTemplateText)
type builderTemplateContext struct {
Indent string
Name string
IsFinal bool
Fields []builderFieldTemplateContext
Setters []builderSetterTemplateContext
ResultType string
Objects string
Annotations []string
}