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

Add syntactic sugar for fn::open #14

Merged
merged 10 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions ast/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
"github.com/pulumi/esc/syntax/encoding"
)

const example = `
func TestExample(t *testing.T) {
t.Parallel()

const example = `
imports:
- green-channel
- us-west-2
Expand All @@ -30,9 +33,34 @@ config:
environment: prod
`

func TestExample(t *testing.T) {
syntax, diags := encoding.DecodeYAML("<stdin>", yaml.NewDecoder(strings.NewReader(example)), nil)
require.Len(t, diags, 0)

environment, diags := ParseEnvironment([]byte(example), syntax)
assert.Len(t, diags, 0)

assert.Nil(t, environment.Description)
}

func TestExample2(t *testing.T) {
t.Parallel()

const example = `
imports:
- green-channel
- us-west-2
config:
aws:
fn::open::aws-oidc:
inputs:
sessionName: site-prod-session
roleArn: some-role-arn
aq17 marked this conversation as resolved.
Show resolved Hide resolved
pulumi:
aws:defaultTags:
tags:
environment: prod
`

syntax, diags := encoding.DecodeYAML("<stdin>", yaml.NewDecoder(strings.NewReader(example)), nil)
require.Len(t, diags, 0)

Expand Down
17 changes: 17 additions & 0 deletions ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,11 @@ func tryParseFunction(node *syntax.ObjectNode) (Expr, syntax.Diagnostics, bool)
case "fn::toString":
parse = parseToString
default:
if strings.HasPrefix(kvp.Key.Value(), "fn::open::") {
parse = parseShortOpen
break
}

if strings.HasPrefix(strings.ToLower(kvp.Key.Value()), "fn::") {
diags = append(diags, syntax.Warning(kvp.Key.Syntax().Range(),
"'fn::' is a reserved prefix",
Expand Down Expand Up @@ -604,6 +609,18 @@ func parseOpen(node *syntax.ObjectNode, name *StringExpr, args Expr) (Expr, synt
return OpenSyntax(node, name, obj, provider, inputs), diags
}

func parseShortOpen(node *syntax.ObjectNode, name *StringExpr, args Expr) (Expr, syntax.Diagnostics) {
kvp := node.Index(0)
provider := strings.TrimPrefix(kvp.Key.Value(), "fn::open::")
inputs, ok := args.(*ObjectExpr)
if !ok {
return nil, syntax.Diagnostics{ExprError(args, fmt.Sprintf("the argument to fn::open::%s must be an object", provider), "")}
}
p := name.Syntax().(*syntax.StringNode)

return OpenSyntax(node, name, inputs, StringSyntaxValue(p, provider), inputs), nil
}

func parseJoin(node *syntax.ObjectNode, name *StringExpr, args Expr) (Expr, syntax.Diagnostics) {
list, ok := args.(*ListExpr)
if !ok || len(list.Elements) != 2 {
Expand Down
Loading