Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a first version of try in PCL #18454

Merged
merged 1 commit into from
Feb 11, 2025
Merged

Implement a first version of try in PCL #18454

merged 1 commit into from
Feb 11, 2025

Conversation

brandonpollack23
Copy link
Contributor

@brandonpollack23 brandonpollack23 commented Feb 5, 2025

Terraform's try function accepts a variable number of arguments (of variable types) and returns the first that does not throw an error when evaluated. This change takes a first pass at adding try to PCL, in an effort to support better conversion from Terraform to Pulumi programs. It makes the following contributions:

  • PCL gets a new try function, which takes a single argument which is a list/tuple of expressions to be tried.

  • Code generation for NodeJS and Python has been extended to support a naive first implementation of try. Both implementations rely on these languages being dynamic under the hood. A Go implementation is not currently provided and its omission is tracked by Support try in Go program generation #18506.

  • A conformance test, l1-builtin-try, has been added to exercise the NodeJS and Python implementations. It is skipped in Go with a reference to the aforementioned tracking issue.

When this has merged, we can look at extending pulumi-converter-terraform to generate applications of try, which should enable us to make progress converting a number of popular Terraform modules and programs.

Part of #18350

Comment on lines 421 to 427
"try": model.NewFunction(model.StaticFunctionSignature{
Parameters: []model.Parameter{{
Name: "values",
Type: model.NewListType(model.DynamicType),
}},
ReturnType: model.NewOutputType(model.DynamicType),
}),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we do better here? If all the args are the same type we know the output type. For mixed types it might be better to use a union output rather than dynamic?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you use GenericFunctionSignature you can even make the arg count variadic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but we don't know (at this time) all the call sites of try and we are only generating one instance (now). We can do better in the future but I think for v1 we should keep it simple and try to bring types back in special cases after that

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed and it's now variadic, with a slightly better inferred type signature 👍

[Terraform's `try`
function](https://developer.hashicorp.com/terraform/language/functions/try)
accepts a variable number of arguments (of variable types) and returns
the first that does not throw an error when evaluated. This change takes
a first pass at adding `try` to PCL, in an effort to support better
conversion from Terraform to Pulumi programs. It makes the following
contributions:

* PCL gets a new `try` function, which is variadic like its Terraform
  equivalent. An expression of the form `try(e1, e2, ..., en)` will be
  typed as `(t1, t2, ..., tn) -> dynamic`, where `ti` is the type of
  `ei`.

* Code generation for NodeJS and Python has been extended to support a
  naive first implementation of `try`. Both implementations rely on
  these languages being dynamic under the hood. A Go implementation is
  not currently provided and its omission is tracked by #18506.

* A conformance test, `l1-builtin-try`, has been added to exercise the
  NodeJS and Python implementations. It is skipped in Go with a
  reference to the aforementioned tracking issue.

When this has merged, we can look at extending
`pulumi-converter-terraform` to generate applications of `try`, which
should enable us to make progress converting a number of popular
Terraform modules and programs.

Part of #18350

Co-authored-by: Brandon Pollack <brandonpollack23@gmail.com>
@lunaris lunaris added this pull request to the merge queue Feb 11, 2025
Merged via the queue into master with commit 0a6d2ad Feb 11, 2025
91 of 92 checks passed
@lunaris lunaris deleted the bpollack/try_in_ts branch February 11, 2025 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants