-
Notifications
You must be signed in to change notification settings - Fork 19
/
gen_defaults.go
307 lines (273 loc) · 8.3 KB
/
gen_defaults.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
// Copyright 2022, Pulumi Corporation. All rights reserved.
//
// Default value and config value code-generation facilities for SDK
// generation are kept in this separate file as they are a bit
// intricate.
package java
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pulumi/pulumi/pkg/v3/codegen"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi-java/pkg/codegen/java/names"
)
// TODO would this file be a convenient place to handle
// DefaultValue.ConstValue also?
// Helper type for config getter and default value generation. Use
// configExpr and defaultValueExpr, the rest are helper methods.
type defaultsGen struct {
mod *modContext
ctx *classFileContext
}
// Generates code for an expression to compute the value of config
// property. The expression assumes an environment where config is
// bound to a Config instance. The type of the generated expression
// must match targetType.
func (dg *defaultsGen) configExpr(
propContext string,
configProp *schema.Property,
targetType TypeShape,
) (string, error) {
return dg.builderExpr(propContext, configProp, targetType, "config", "")
}
// Generates an expression that evaluates to the default value of the
// desired type. The expression takes care of env var lookups and
// hydrating the expected targetType. Optional arg, if given, points
// to a user-provided value that takes priority over defaults.
func (dg *defaultsGen) defaultValueExpr(
propContext string,
prop *schema.Property,
targetType TypeShape,
arg string,
) (string, error) {
return dg.builderExpr(propContext, prop, targetType, "", arg)
}
// Utility to generates a builder expr against the
// Codegen.PropertyBuilder API.
//
// config if non-empty is an expr of type Config.
//
// arg if non-empty is an expr of type targetType pointing to a
// user-provided nullable argument.
//
// propContext is free text only used in WARN statements to
// contextualize the property.
func (dg *defaultsGen) builderExpr(
propContext string,
prop *schema.Property,
targetType TypeShape,
config string,
arg string,
) (string, error) {
if code, ok := dg.builderExprSpecialCase(prop, targetType, config, arg); ok {
return code, nil
}
isOutput, t0 := targetType.UnOutput()
var builderTransformCode string
if isOutput {
if prop.Secret {
builderTransformCode = ".secret()"
} else {
builderTransformCode = ".output()"
}
} else {
// TODO[pulumi/pulumi#9744]: Codegen for secret
// properties forgets the secret flag
if prop.Secret {
fmt.Printf("WARN: secret property %s (%s) does not have an Output type\n",
prop.Name, propContext)
}
builderTransformCode = ""
}
isOptional, t := t0.UnOptional()
var code string
// For Either<A,B> try to codegen A or B opportunistically.
if isEither, a, b := t.UnEither(); isEither {
aV, err := dg.builderExprWithSimpleType(prop, a, config, arg,
fmt.Sprintf(".left(%s)%s",
b.ToCodeClassLiteral(dg.ctx.imports),
builderTransformCode))
if err != nil {
bV, err2 := dg.builderExprWithSimpleType(prop, b, config, arg,
fmt.Sprintf(".right(%s)%s",
a.ToCodeClassLiteral(dg.ctx.imports),
builderTransformCode))
if err2 != nil {
return "", fmt.Errorf("Cannot process Either"+
"\nLeft: %v\nRight: %w", err, err2)
}
code = bV
} else {
code = aV
}
} else {
c, err := dg.builderExprWithSimpleType(
prop, t, config, arg, builderTransformCode)
if err != nil {
return "", err
}
code = c
}
if isOptional {
return fmt.Sprintf("%s.get()", code), nil
}
if !prop.IsRequired() {
return fmt.Sprintf("%s.getNullable()", code), nil
}
return fmt.Sprintf("%s.require()", code), nil
}
// Degenerate cases can have simpler code.
func (dg *defaultsGen) builderExprSpecialCase(
prop *schema.Property,
_ TypeShape,
_ string,
arg string,
) (string, bool) {
// No defaults, no null check, already have an argument of wanted type, simply pass it on.
if !prop.IsRequired() && arg != "" && prop.DefaultValue == nil && prop.ConstValue == nil {
return arg, true
}
// Required prop without defaults, all we need to do is a null check with a good exception.
if prop.IsRequired() && arg != "" && prop.DefaultValue == nil && prop.ConstValue == nil {
msg := fmt.Sprintf("expected parameter '%s' to be non-null", prop.Name)
return fmt.Sprintf("%s.requireNonNull(%s, %s)",
dg.ctx.ref(names.Objects),
arg,
dg.quoteJavaStringLiteral(msg)), true
}
return "", false
}
// Helper for simple types - not Optional, Either, or Output.
func (dg *defaultsGen) builderExprWithSimpleType(
prop *schema.Property,
targetType TypeShape,
config string,
arg string,
builderTransformCode string,
) (string, error) {
var buf bytes.Buffer
fmt.Fprint(&buf, dg.ctx.ref(names.Codegen))
propLiteral := dg.quoteJavaStringLiteral(prop.Name)
isObject := false
if targetType.Type.Equal(names.Boolean) {
fmt.Fprintf(&buf, ".booleanProp(%s)", propLiteral)
} else if targetType.Type.Equal(names.Integer) {
fmt.Fprintf(&buf, ".integerProp(%s)", propLiteral)
} else if targetType.Type.Equal(names.String) {
fmt.Fprintf(&buf, ".stringProp(%s)", propLiteral)
} else if targetType.Type.Equal(names.Double) {
fmt.Fprintf(&buf, ".doubleProp(%s)", propLiteral)
} else {
// Need to know TypeShape to deserialize at runtime
isObject = true
if len(targetType.Parameters) == 0 {
fmt.Fprintf(&buf, ".objectProp(%s, %s.class)",
propLiteral,
dg.ctx.ref(targetType.Type))
} else {
fmt.Fprintf(&buf, ".objectProp(%s, %s)",
propLiteral,
targetType.StringJavaTypeShape(dg.ctx.imports))
}
}
fmt.Fprintf(&buf, "%s", builderTransformCode)
if config != "" {
fmt.Fprintf(&buf, ".config(%s)", config)
}
if arg != "" {
fmt.Fprintf(&buf, ".arg(%s)", arg)
}
if prop.DefaultValue == nil {
return buf.String(), nil
}
dv := prop.DefaultValue
if len(dv.Environment) > 0 {
fmt.Fprintf(&buf, ".env(")
for i, envVarName := range dv.Environment {
if i != 0 {
fmt.Fprintf(&buf, ", ")
}
fmt.Fprintf(&buf, "%s", dg.quoteJavaStringLiteral(envVarName))
}
fmt.Fprintf(&buf, ")")
}
if dv.Value != nil {
// For enums, refer to matching Enum.CASE as a direct default value
enumTypes := dg.detectEnumTypes(prop)
if enumType, ok := enumTypes[targetType.Type.String()]; ok {
code, err := dg.enumReference(dg.ctx, enumType, dv)
if err != nil {
return "", err
}
fmt.Fprintf(&buf, ".def(%s)", code)
} else if isObject {
encoded, err := json.Marshal(dv.Value)
if err != nil {
return "", err
}
j := dg.quoteJavaStringLiteral(string(encoded))
fmt.Fprintf(&buf, ".defJson(%s)", j)
} else {
primCode, primType, err := primitiveValue(dv.Value)
if err != nil {
return "", err
}
if !primType.WithoutAnnotations().Equal(targetType.WithoutAnnotations()) {
return "", fmt.Errorf("Type mismatch: %v vs %v",
primType, targetType)
}
fmt.Fprintf(&buf, ".def(%s)", primCode)
}
}
return buf.String(), nil
}
// Helper to find any Enum types referenced in the type of Property
// and index them by FQN.
func (dg *defaultsGen) detectEnumTypes(prop *schema.Property) map[string]*schema.EnumType {
// Index all EnumTypes present in schemaType by FQN.
enumTypes := map[string]*schema.EnumType{}
codegen.VisitTypeClosure([]*schema.Property{prop}, func(t schema.Type) {
switch eT := t.(type) {
case *schema.EnumType:
fqn := dg.mod.typeStringForEnumType(eT).Type
enumTypes[fqn.String()] = eT
}
})
return enumTypes
}
// In cases when the default value is a known enum reference, this
// will be set to the code literal, such as
// `io.pulumi.azurenative.insights.enums.WebTestKind.Ping`.
func (dg *defaultsGen) enumReference(
ctx *classFileContext,
enumType *schema.EnumType,
dv *schema.DefaultValue,
) (string, error) {
enumFQN := dg.mod.typeStringForEnumType(enumType).Type
enumName := enumFQN.BaseIdent().String()
for _, e := range enumType.Elements {
if e.Value != dv.Value {
continue
}
elName := e.Name
if elName == "" {
elName = fmt.Sprintf("%v", e.Value)
}
safeName, err := names.MakeSafeEnumName(elName, enumName)
if err != nil {
return "", err
}
return fmt.Sprintf("%s.%s", ctx.ref(enumFQN), safeName), nil
}
return "", fmt.Errorf("Bad default value for enum %s: %v",
enumFQN.String(),
dv.Value)
}
func (dg *defaultsGen) quoteJavaStringLiteral(s string) string {
v, _, err := primitiveValue(s)
if err != nil {
panic(err)
}
return v
}