-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
gen_program.go
786 lines (691 loc) 路 23.9 KB
/
gen_program.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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
// Copyright 2016-2021, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dotnet
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"path"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/pulumi/pulumi/pkg/v3/codegen"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model/format"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/encoding"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)
type GenerateProgramOptions struct {
// Determines whether ResourceArg types have an implicit name
// when constructing a resource. For example:
// when implicitResourceArgsTypeName is set to true,
// new Bucket("name", new BucketArgs { ... })
// becomes
// new Bucket("name", new() { ... });
// The latter syntax is only available on .NET 6 or later
implicitResourceArgsTypeName bool
}
type generator struct {
// The formatter to use when generating code.
*format.Formatter
program *pcl.Program
// C# namespace map per package.
namespaces map[string]map[string]string
// C# codegen compatibility mode per package.
compatibilities map[string]string
// A function to convert tokens to module names per package (utilizes the `moduleFormat` setting internally).
tokenToModules map[string]func(x string) string
// Type names per invoke function token.
functionArgs map[string]string
// keep track of variable identifiers which are the result of an invoke
// for example "var resourceGroup = GetResourceGroup.Invoke(...)"
// we will keep track of the reference "resourceGroup"
//
// later on when apply a traversal such as resourceGroup.name,
// we should rewrite it as resourceGroup.Apply(resourceGroupResult => resourceGroupResult.name)
functionInvokes map[string]*schema.Function
// Whether awaits are needed, and therefore an async Initialize method should be declared.
asyncInit bool
configCreated bool
diagnostics hcl.Diagnostics
insideFunctionInvoke bool
insideAwait bool
// Program generation options
generateOptions GenerateProgramOptions
}
const pulumiPackage = "pulumi"
func GenerateProgramWithOptions(
program *pcl.Program,
options GenerateProgramOptions) (map[string][]byte, hcl.Diagnostics, error) {
pcl.MapProvidersAsResources(program)
// Linearize the nodes into an order appropriate for procedural code generation.
nodes := pcl.Linearize(program)
// Import C#-specific schema info.
namespaces := make(map[string]map[string]string)
compatibilities := make(map[string]string)
tokenToModules := make(map[string]func(x string) string)
functionArgs := make(map[string]string)
packages, err := program.PackageSnapshots()
if err != nil {
return nil, nil, err
}
for _, p := range packages {
if err := p.ImportLanguages(map[string]schema.Language{"csharp": Importer}); err != nil {
return make(map[string][]byte), nil, err
}
csharpInfo, hasInfo := p.Language["csharp"].(CSharpPackageInfo)
if !hasInfo {
csharpInfo = CSharpPackageInfo{}
}
packageNamespaces := csharpInfo.Namespaces
namespaces[p.Name] = packageNamespaces
compatibilities[p.Name] = csharpInfo.Compatibility
tokenToModules[p.Name] = p.TokenToModule
for _, f := range p.Functions {
if f.Inputs != nil {
functionArgs[f.Inputs.Token] = f.Token
}
}
}
g := &generator{
program: program,
namespaces: namespaces,
compatibilities: compatibilities,
tokenToModules: tokenToModules,
functionArgs: functionArgs,
functionInvokes: map[string]*schema.Function{},
generateOptions: options,
}
g.Formatter = format.NewFormatter(g)
for _, n := range nodes {
if r, ok := n.(*pcl.Resource); ok && requiresAsyncInit(r) {
g.asyncInit = true
break
}
}
var index bytes.Buffer
g.genPreamble(&index, program)
g.Indented(func() {
for _, n := range nodes {
g.genNode(&index, n)
}
})
g.genPostamble(&index, nodes)
files := map[string][]byte{
"Program.cs": index.Bytes(),
}
return files, g.diagnostics, nil
}
func GenerateProgram(program *pcl.Program) (map[string][]byte, hcl.Diagnostics, error) {
defaultOptions := GenerateProgramOptions{
// by default, we generate C# code that targets .NET 6
implicitResourceArgsTypeName: true,
}
return GenerateProgramWithOptions(program, defaultOptions)
}
func GenerateProject(directory string, project workspace.Project, program *pcl.Program) error {
files, diagnostics, err := GenerateProgram(program)
if err != nil {
return err
}
if diagnostics.HasErrors() {
return diagnostics
}
// Set the runtime to "dotnet" then marshal to Pulumi.yaml
project.Runtime = workspace.NewProjectRuntimeInfo("dotnet", nil)
projectBytes, err := encoding.YAML.Marshal(project)
if err != nil {
return err
}
files["Pulumi.yaml"] = projectBytes
// Build a .csproj based on the packages used by program
var csproj bytes.Buffer
csproj.WriteString(`<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Pulumi" Version="3.*" />
`)
// For each package add a PackageReference line
packages, err := program.PackageSnapshots()
if err != nil {
return err
}
for _, p := range packages {
packageTemplate := " <PackageReference Include=\"%s\" Version=\"%s\" />\n"
if err := p.ImportLanguages(map[string]schema.Language{"csharp": Importer}); err != nil {
return err
}
if p.Name == pulumiPackage {
continue
}
packageName := fmt.Sprintf("Pulumi.%s", namespaceName(map[string]string{}, p.Name))
if langInfo, found := p.Language["csharp"]; found {
csharpInfo, ok := langInfo.(CSharpPackageInfo)
if ok {
namespace := namespaceName(csharpInfo.Namespaces, p.Name)
packageName = fmt.Sprintf("%s.%s", csharpInfo.GetRootNamespace(), namespace)
}
}
if p.Version != nil {
csproj.WriteString(fmt.Sprintf(packageTemplate, packageName, p.Version.String()))
} else {
csproj.WriteString(fmt.Sprintf(packageTemplate, packageName, "*"))
}
}
csproj.WriteString(` </ItemGroup>
</Project>`)
files[project.Name.String()+".csproj"] = csproj.Bytes()
// Add the language specific .gitignore
files[".gitignore"] = []byte(dotnetGitIgnore)
for filename, data := range files {
outPath := path.Join(directory, filename)
err := ioutil.WriteFile(outPath, data, 0600)
if err != nil {
return fmt.Errorf("could not write output program: %w", err)
}
}
return nil
}
// genTrivia generates the list of trivia associated with a given token.
func (g *generator) genTrivia(w io.Writer, token syntax.Token) {
for _, t := range token.LeadingTrivia {
if c, ok := t.(syntax.Comment); ok {
g.genComment(w, c)
}
}
for _, t := range token.TrailingTrivia {
if c, ok := t.(syntax.Comment); ok {
g.genComment(w, c)
}
}
}
func (g *generator) warnf(location *hcl.Range, reason string, args ...interface{}) {
g.diagnostics = append(g.diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: fmt.Sprintf(reason, args...),
Subject: location,
})
}
func (g *generator) findFunctionSchema(token string, location *hcl.Range) (*schema.Function, bool) {
for _, pkg := range g.program.PackageReferences() {
fn, ok, err := pcl.LookupFunction(pkg, token)
if !ok {
continue
}
if err != nil {
g.diagnostics = append(g.diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: fmt.Sprintf("Could not find function schema for '%s'", token),
Detail: err.Error(),
Subject: location,
})
return nil, false
}
return fn, true
}
return nil, false
}
func (g *generator) isFunctionInvoke(localVariable *pcl.LocalVariable) (*schema.Function, bool) {
value := localVariable.Definition.Value
switch value.(type) {
case *model.FunctionCallExpression:
call := value.(*model.FunctionCallExpression)
switch call.Name {
case pcl.Invoke:
token := call.Args[0].(*model.TemplateExpression).Parts[0].(*model.LiteralValueExpression).Value.AsString()
return g.findFunctionSchema(token, call.Args[0].SyntaxNode().Range().Ptr())
}
}
return nil, false
}
// genComment generates a comment into the output.
func (g *generator) genComment(w io.Writer, comment syntax.Comment) {
for _, l := range comment.Lines {
g.Fgenf(w, "%s//%s\n", g.Indent, l)
}
}
// genPreamble generates using statements, class definition and constructor.
func (g *generator) genPreamble(w io.Writer, program *pcl.Program) {
// Accumulate other using statements for the various providers and packages. Don't emit them yet, as we need
// to sort them later on.
systemUsings := codegen.NewStringSet("System.Collections.Generic")
pulumiUsings := codegen.NewStringSet()
preambleHelperMethods := codegen.NewStringSet()
for _, n := range program.Nodes {
if r, isResource := n.(*pcl.Resource); isResource {
pcl.FixupPulumiPackageTokens(r)
pkg, _, _, _ := r.DecomposeToken()
if pkg != pulumiPackage {
namespace := namespaceName(g.namespaces[pkg], pkg)
var info CSharpPackageInfo
if r.Schema != nil && r.Schema.PackageReference != nil {
def, err := r.Schema.PackageReference.Definition()
contract.AssertNoError(err)
if csharpinfo, ok := def.Language["csharp"].(CSharpPackageInfo); ok {
info = csharpinfo
}
}
pulumiUsings.Add(fmt.Sprintf("%s = %[2]s.%[1]s", namespace, info.GetRootNamespace()))
}
}
diags := n.VisitExpressions(nil, func(n model.Expression) (model.Expression, hcl.Diagnostics) {
if call, ok := n.(*model.FunctionCallExpression); ok {
for _, i := range g.genFunctionUsings(call) {
if strings.HasPrefix(i, "System") {
systemUsings.Add(i)
} else {
pulumiUsings.Add(i)
}
}
// Checking to see if this function call deserves its own dedicated helper method in the preamble
if helperMethodBody, ok := getHelperMethodIfNeeded(call.Name); ok {
preambleHelperMethods.Add(helperMethodBody)
}
}
if _, ok := n.(*model.SplatExpression); ok {
systemUsings.Add("System.Linq")
}
return n, nil
})
contract.Assert(len(diags) == 0)
}
if g.asyncInit {
systemUsings.Add("System.Threading.Tasks")
}
for _, pkg := range systemUsings.SortedValues() {
g.Fprintf(w, "using %v;\n", pkg)
}
g.Fprintln(w, `using Pulumi;`)
for _, pkg := range pulumiUsings.SortedValues() {
g.Fprintf(w, "using %v;\n", pkg)
}
g.Fprint(w, "\n")
// If we collected any helper methods that should be added, write them just before the main func
for _, preambleHelperMethodBody := range preambleHelperMethods.SortedValues() {
g.Fprintf(w, "\t%s\n\n", preambleHelperMethodBody)
}
asyncKeywordWhenNeeded := ""
if g.asyncInit {
asyncKeywordWhenNeeded = "async"
}
g.Fprintf(w, "return await Deployment.RunAsync(%s() => \n", asyncKeywordWhenNeeded)
g.Fprint(w, "{\n")
}
// hasOutputVariables checks whether there are any output declarations
func hasOutputVariables(nodes []pcl.Node) bool {
for _, n := range nodes {
switch n.(type) {
case *pcl.OutputVariable:
return true
}
}
return false
}
// genPostamble closes the method and the class and declares stack output statements.
func (g *generator) genPostamble(w io.Writer, nodes []pcl.Node) {
if hasOutputVariables(nodes) {
g.Indented(func() {
g.Fgenf(w, "%sreturn new Dictionary<string, object?>\n", g.Indent)
g.Fgenf(w, "%s{\n", g.Indent)
g.Indented(func() {
// Emit stack output properties
for _, n := range nodes {
switch n := n.(type) {
case *pcl.OutputVariable:
outputID := fmt.Sprintf(`"%s"`, g.escapeString(n.LogicalName(), false, false))
g.Fgenf(w, "%s[%s] = %.3v,\n", g.Indent, outputID, g.lowerExpression(n.Value, n.Type()))
}
}
})
g.Fgenf(w, "%s};\n", g.Indent)
})
}
// Close lambda call expression
g.Fprintf(w, "});\n\n")
}
func (g *generator) genNode(w io.Writer, n pcl.Node) {
switch n := n.(type) {
case *pcl.Resource:
g.genResource(w, n)
case *pcl.ConfigVariable:
g.genConfigVariable(w, n)
case *pcl.LocalVariable:
g.genLocalVariable(w, n)
}
}
// requiresAsyncInit returns true if the program requires awaits in the code, and therefore an asynchronous
// method must be declared.
func requiresAsyncInit(r *pcl.Resource) bool {
if r.Options == nil || r.Options.Range == nil {
return false
}
return model.ContainsPromises(r.Options.Range.Type())
}
// resourceTypeName computes the C# class name for the given resource.
func (g *generator) resourceTypeName(r *pcl.Resource) string {
pcl.FixupPulumiPackageTokens(r)
// Compute the resource type from the Pulumi type token.
pkg, module, member, diags := r.DecomposeToken()
contract.Assert(len(diags) == 0)
namespaces := g.namespaces[pkg]
rootNamespace := namespaceName(namespaces, pkg)
namespace := namespaceName(namespaces, module)
namespaceTokens := strings.Split(namespace, "/")
for i, name := range namespaceTokens {
namespaceTokens[i] = Title(name)
}
namespace = strings.Join(namespaceTokens, ".")
if namespace != "" {
namespace = "." + namespace
}
qualifiedMemberName := fmt.Sprintf("%s%s.%s", rootNamespace, namespace, Title(member))
return qualifiedMemberName
}
func (g *generator) extractInputPropertyNameMap(r *pcl.Resource) map[string]string {
// Extract language-specific property names from schema
var csharpInputPropertyNameMap = make(map[string]string)
if r.Schema != nil {
for _, inputProperty := range r.Schema.InputProperties {
if val1, ok := inputProperty.Language["csharp"]; ok {
if val2, ok := val1.(CSharpPropertyInfo); ok {
csharpInputPropertyNameMap[inputProperty.Name] = val2.Name
}
}
}
}
return csharpInputPropertyNameMap
}
// resourceArgsTypeName computes the C# arguments class name for the given resource.
func (g *generator) resourceArgsTypeName(r *pcl.Resource) string {
// Compute the resource type from the Pulumi type token.
pkg, module, member, diags := r.DecomposeToken()
contract.Assert(len(diags) == 0)
namespaces := g.namespaces[pkg]
rootNamespace := namespaceName(namespaces, pkg)
namespace := namespaceName(namespaces, module)
if g.compatibilities[pkg] == "kubernetes20" && module != "" {
namespace = fmt.Sprintf("Types.Inputs.%s", namespace)
}
if namespace != "" {
namespace = "." + namespace
}
return fmt.Sprintf("%s%s.%sArgs", rootNamespace, namespace, Title(member))
}
// functionName computes the C# namespace and class name for the given function token.
func (g *generator) functionName(tokenArg model.Expression) (string, string) {
token := tokenArg.(*model.TemplateExpression).Parts[0].(*model.LiteralValueExpression).Value.AsString()
tokenRange := tokenArg.SyntaxNode().Range()
// Compute the resource type from the Pulumi type token.
pkg, module, member, diags := pcl.DecomposeToken(token, tokenRange)
contract.Assert(len(diags) == 0)
namespaces := g.namespaces[pkg]
rootNamespace := namespaceName(namespaces, pkg)
namespace := namespaceName(namespaces, module)
if namespace != "" {
namespace = "." + namespace
}
return rootNamespace, fmt.Sprintf("%s%s.%s", rootNamespace, namespace, Title(member))
}
func (g *generator) toSchemaType(destType model.Type) (schema.Type, bool) {
schemaType, ok := pcl.GetSchemaForType(destType.(model.Type))
if !ok {
return nil, false
}
return codegen.UnwrapType(schemaType), true
}
// argumentTypeName computes the C# argument class name for the given expression and model type.
func (g *generator) argumentTypeName(expr model.Expression, destType model.Type) string {
suffix := "Args"
if g.insideFunctionInvoke {
suffix = "InputArgs"
}
return g.argumentTypeNameWithSuffix(expr, destType, suffix)
}
func (g *generator) argumentTypeNameWithSuffix(expr model.Expression, destType model.Type, suffix string) string {
schemaType, ok := g.toSchemaType(destType)
if !ok {
return ""
}
objType, ok := schemaType.(*schema.ObjectType)
if !ok {
return ""
}
token := objType.Token
tokenRange := expr.SyntaxNode().Range()
qualifier := "Inputs"
if f, ok := g.functionArgs[token]; ok {
token = f
qualifier = ""
}
pkg, _, member, diags := pcl.DecomposeToken(token, tokenRange)
contract.Assert(len(diags) == 0)
module := g.tokenToModules[pkg](token)
namespaces := g.namespaces[pkg]
rootNamespace := namespaceName(namespaces, pkg)
namespace := namespaceName(namespaces, module)
if strings.ToLower(namespace) == "index" {
namespace = ""
}
if namespace != "" {
namespace = "." + namespace
}
if g.compatibilities[pkg] == "kubernetes20" {
namespace = ".Types.Inputs" + namespace
} else if qualifier != "" {
namespace = namespace + "." + qualifier
}
member = member + suffix
return fmt.Sprintf("%s%s.%s", rootNamespace, namespace, Title(member))
}
// makeResourceName returns the expression that should be emitted for a resource's "name" parameter given its base name
// and the count variable name, if any.
func (g *generator) makeResourceName(baseName, count string) string {
if count == "" {
return fmt.Sprintf(`"%s"`, baseName)
}
return fmt.Sprintf("$\"%s-{%s}\"", baseName, count)
}
func (g *generator) genResourceOptions(opts *pcl.ResourceOptions) string {
if opts == nil {
return ""
}
var result bytes.Buffer
appendOption := func(name string, value model.Expression) {
if result.Len() == 0 {
_, err := fmt.Fprintf(&result, ", new CustomResourceOptions\n%s{", g.Indent)
g.Indent += " "
contract.IgnoreError(err)
}
if name == "IgnoreChanges" {
// ignore changes need to be special cased
// because new [] { "field" } cannot be implicitly casted to List<string>
// which is the type of IgnoreChanges
if changes, isTuple := value.(*model.TupleConsExpression); isTuple {
g.Fgenf(&result, "\n%sIgnoreChanges =", g.Indent)
g.Fgenf(&result, "\n%s{", g.Indent)
g.Indented(func() {
for _, v := range changes.Expressions {
g.Fgenf(&result, "\n%s\"%.v\",", g.Indent, v)
}
})
g.Fgenf(&result, "\n%s}", g.Indent)
} else {
g.Fgenf(&result, "\n%s%s = %v,", g.Indent, name, g.lowerExpression(value, value.Type()))
}
} else {
g.Fgenf(&result, "\n%s%s = %v,", g.Indent, name, g.lowerExpression(value, value.Type()))
}
}
if opts.Parent != nil {
appendOption("Parent", opts.Parent)
}
if opts.Provider != nil {
appendOption("Provider", opts.Provider)
}
if opts.DependsOn != nil {
appendOption("DependsOn", opts.DependsOn)
}
if opts.Protect != nil {
appendOption("Protect", opts.Protect)
}
if opts.IgnoreChanges != nil {
appendOption("IgnoreChanges", opts.IgnoreChanges)
}
if result.Len() != 0 {
g.Indent = g.Indent[:len(g.Indent)-4]
_, err := fmt.Fprintf(&result, "\n%s}", g.Indent)
contract.IgnoreError(err)
}
return result.String()
}
// genResource handles the generation of instantiations of non-builtin resources.
func (g *generator) genResource(w io.Writer, r *pcl.Resource) {
qualifiedMemberName := g.resourceTypeName(r)
csharpInputPropertyNameMap := g.extractInputPropertyNameMap(r)
// Add conversions to input properties
for _, input := range r.Inputs {
destType, diagnostics := r.InputType.Traverse(hcl.TraverseAttr{Name: input.Name})
g.diagnostics = append(g.diagnostics, diagnostics...)
input.Value = g.lowerExpression(input.Value, destType.(model.Type))
if csharpName, ok := csharpInputPropertyNameMap[input.Name]; ok {
input.Name = csharpName
}
}
pcl.AnnotateResourceInputs(r)
name := r.LogicalName()
variableName := makeValidIdentifier(r.Name())
argsName := g.resourceArgsTypeName(r)
g.genTrivia(w, r.Definition.Tokens.GetType(""))
for _, l := range r.Definition.Tokens.GetLabels(nil) {
g.genTrivia(w, l)
}
g.genTrivia(w, r.Definition.Tokens.GetOpenBrace())
instantiate := func(resName string) {
if len(r.Inputs) == 0 && r.Options == nil {
// only resource name is provided
g.Fgenf(w, "new %s(%s)", qualifiedMemberName, resName)
} else {
if g.generateOptions.implicitResourceArgsTypeName {
g.Fgenf(w, "new %s(%s, new()\n", qualifiedMemberName, resName)
} else {
g.Fgenf(w, "new %s(%s, new %s\n", qualifiedMemberName, resName, argsName)
}
g.Fgenf(w, "%s{\n", g.Indent)
g.Indented(func() {
for _, attr := range r.Inputs {
g.Fgenf(w, "%s%s =", g.Indent, propertyName(attr.Name))
g.Fgenf(w, " %.v,\n", attr.Value)
}
})
g.Fgenf(w, "%s}%s)", g.Indent, g.genResourceOptions(r.Options))
}
}
if r.Options != nil && r.Options.Range != nil {
rangeType := model.ResolveOutputs(r.Options.Range.Type())
rangeExpr := g.lowerExpression(r.Options.Range, rangeType)
g.Fgenf(w, "%svar %s = new List<%s>();\n", g.Indent, variableName, qualifiedMemberName)
resKey := "Key"
if model.InputType(model.NumberType).ConversionFrom(rangeExpr.Type()) != model.NoConversion {
g.Fgenf(w, "%sfor (var rangeIndex = 0; rangeIndex < %.12o; rangeIndex++)\n", g.Indent, rangeExpr)
g.Fgenf(w, "%s{\n", g.Indent)
g.Fgenf(w, "%s var range = new { Value = rangeIndex };\n", g.Indent)
resKey = "Value"
} else {
rangeExpr := &model.FunctionCallExpression{
Name: "entries",
Args: []model.Expression{rangeExpr},
}
g.Fgenf(w, "%sforeach (var range in %.v)\n", g.Indent, rangeExpr)
g.Fgenf(w, "%s{\n", g.Indent)
}
resName := g.makeResourceName(name, "range."+resKey)
g.Indented(func() {
g.Fgenf(w, "%s%s.Add(", g.Indent, variableName)
instantiate(resName)
g.Fgenf(w, ");\n")
})
g.Fgenf(w, "%s}\n", g.Indent)
} else {
g.Fgenf(w, "%svar %s = ", g.Indent, variableName)
instantiate(g.makeResourceName(name, ""))
g.Fgenf(w, ";\n\n")
}
g.genTrivia(w, r.Definition.Tokens.GetCloseBrace())
}
func (g *generator) genConfigVariable(w io.Writer, v *pcl.ConfigVariable) {
if !g.configCreated {
g.Fprintf(w, "%svar config = new Config();\n", g.Indent)
g.configCreated = true
}
getType := "Object<dynamic>"
switch v.Type() {
case model.StringType:
getType = ""
case model.NumberType, model.IntType:
getType = "Number"
case model.BoolType:
getType = "Boolean"
}
getOrRequire := "Get"
if v.DefaultValue == nil {
getOrRequire = "Require"
}
name := makeValidIdentifier(v.Name())
if v.DefaultValue != nil {
typ := v.DefaultValue.Type()
if _, ok := typ.(*model.PromiseType); ok {
g.Fgenf(w, "%svar %s = Output.Create(config.%s%s(\"%s\"))",
g.Indent, name, getOrRequire, getType, v.LogicalName())
} else {
g.Fgenf(w, "%svar %s = config.%s%s(\"%s\")",
g.Indent, name, getOrRequire, getType, v.LogicalName())
}
expr := g.lowerExpression(v.DefaultValue, v.DefaultValue.Type())
g.Fgenf(w, " ?? %.v", expr)
} else {
g.Fgenf(w, "%svar %s = config.%s%s(\"%s\")",
g.Indent, name, getOrRequire, getType, v.LogicalName())
}
g.Fgenf(w, ";\n")
}
func (g *generator) genLocalVariable(w io.Writer, localVariable *pcl.LocalVariable) {
variableName := makeValidIdentifier(localVariable.Name())
value := localVariable.Definition.Value
functionSchema, isInvokeCall := g.isFunctionInvoke(localVariable)
if isInvokeCall {
result := g.lowerExpressionWithoutApplies(value, value.Type())
g.functionInvokes[variableName] = functionSchema
g.Fgenf(w, "%svar %s = %v;\n\n", g.Indent, variableName, result)
} else {
result := g.lowerExpression(value, value.Type())
g.Fgenf(w, "%svar %s = %v;\n\n", g.Indent, variableName, result)
}
}
func (g *generator) genNYI(w io.Writer, reason string, vs ...interface{}) {
message := fmt.Sprintf("not yet implemented: %s", fmt.Sprintf(reason, vs...))
g.diagnostics = append(g.diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: message,
Detail: message,
})
g.Fgenf(w, "\"TODO: %s\"", fmt.Sprintf(reason, vs...))
}