pulumi/pkg/codegen/go/gen_program_inline_invoke.go

79 lines
2.0 KiB
Go
Raw Normal View History

[go/program-gen] Fix using inline invoke expressions inside resources, objects and arrays (#14484) # Description This PR fixes an issue in Go program where if users are writing invoke expressions inline inside other expressions, then that invoke is extracted into a temporary variable and then later referenced the same way it was used. This is because non-output-versioned invokes cannot be used directly since they return a tuple (InvokeResult, err) so we need to check for the error before proceeding. Fixes #14064 ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-03 14:22:43 +00:00
package gen
import (
"fmt"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model"
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
)
type inlineInvokeTemp struct {
Name string
Value *model.FunctionCallExpression
}
func (temp *inlineInvokeTemp) Type() model.Type {
return temp.Value.Type()
}
func (temp *inlineInvokeTemp) Traverse(traverser hcl.Traverser) (model.Traversable, hcl.Diagnostics) {
return temp.Type().Traverse(traverser)
}
func (temp *inlineInvokeTemp) SyntaxNode() hclsyntax.Node {
return syntax.None
}
type inlineInvokeSpiller struct {
temps []*inlineInvokeTemp
count int
}
func (spiller *inlineInvokeSpiller) spillExpression(
expr model.Expression,
g *generator,
) (model.Expression, hcl.Diagnostics) {
switch expr := expr.(type) {
case *model.FunctionCallExpression:
isOutputInvoke, _, _ := pcl.RecognizeOutputVersionedInvoke(expr)
if expr.Name == "invoke" && !isOutputInvoke {
// non-output-versioned invokes are the only ones that need to be converted
// because their return type Tuple(InvokeResult, error)
_, _, fn, _ := g.functionName(expr.Args[0])
tempName := fmt.Sprintf("invoke%s%d", fn, spiller.count)
if spiller.count == 0 {
Enable perfsprint linter (#14813) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Prompted by a comment in another review: https://github.com/pulumi/pulumi/pull/14654#discussion_r1419995945 This lints that we don't use `fmt.Errorf` when `errors.New` will suffice, it also covers a load of other cases where `Sprintf` is sub-optimal. Most of these edits were made by running `perfsprint --fix`. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-12 12:19:42 +00:00
tempName = "invoke" + fn
[go/program-gen] Fix using inline invoke expressions inside resources, objects and arrays (#14484) # Description This PR fixes an issue in Go program where if users are writing invoke expressions inline inside other expressions, then that invoke is extracted into a temporary variable and then later referenced the same way it was used. This is because non-output-versioned invokes cannot be used directly since they return a tuple (InvokeResult, err) so we need to check for the error before proceeding. Fixes #14064 ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-03 14:22:43 +00:00
}
temp := &inlineInvokeTemp{
Name: tempName,
Value: expr,
}
spiller.temps = append(spiller.temps, temp)
spiller.count++
reference := model.VariableReference(&model.Variable{
Name: temp.Name,
})
return reference, nil
}
return expr, nil
default:
return expr, nil
}
}
func (g *generator) rewriteInlineInvokes(x model.Expression) (model.Expression, []*inlineInvokeTemp) {
spiller := g.inlineInvokeSpiller
spiller.temps = nil
spill := func(expr model.Expression) (model.Expression, hcl.Diagnostics) {
return spiller.spillExpression(expr, g)
}
expr, _ := model.VisitExpression(x, spill, spill)
return expr, spiller.temps
}