Skip to content

Commit

Permalink
add diag on invalid enum
Browse files Browse the repository at this point in the history
  • Loading branch information
aq17 committed Dec 1, 2022
1 parent 7afac76 commit 5cb5aba
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 5 deletions.
@@ -0,0 +1,4 @@
changes:
- type: chore
scope: programgen
description: Improve error message for invalid enum values on `pulumi convert`.
2 changes: 1 addition & 1 deletion pkg/codegen/dotnet/gen_program_expressions.go
Expand Up @@ -337,7 +337,7 @@ func (g *generator) genIntrensic(w io.Writer, from model.Expression, to model.Ty
} else {
pcl.GenEnum(to, from, g.genSafeEnum(w, to), func(from model.Expression) {
g.Fgenf(w, "%s(%v)", convertFn, from)
})
}, &g.diagnostics)
}
default:
g.Fgenf(w, "%.v", from) // <- probably wrong w.r.t. precedence
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/go/gen_program_expressions.go
Expand Up @@ -207,7 +207,7 @@ func (g *generator) GenFunctionCallExpression(w io.Writer, expr *model.FunctionC
}
pcl.GenEnum(to, from, g.genSafeEnum(w, to), func(from model.Expression) {
g.Fgenf(w, "%s(%v)", enumTag, from)
})
}, &g.diagnostics)
return
}
switch arg := from.(type) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/nodejs/gen_program_expressions.go
Expand Up @@ -342,7 +342,7 @@ func (g *generator) GenFunctionCallExpression(w io.Writer, expr *model.FunctionC
g.Fgenf(w, "%s.%s", enum, memberTag)
}, func(from model.Expression) {
g.Fgenf(w, "%s[%.v]", enum, from)
})
}, &g.diagnostics)
}
} else {
g.Fgenf(w, "%v", from)
Expand Down
11 changes: 10 additions & 1 deletion pkg/codegen/pcl/binder_schema.go
Expand Up @@ -549,6 +549,7 @@ func GenEnum(
from model.Expression,
safeEnum func(member *schema.Enum),
unsafeEnum func(from model.Expression),
diags *hcl.Diagnostics,
) {
known := cty.NilVal
if from, ok := from.(*model.TemplateExpression); ok && len(from.Parts) == 1 {
Expand All @@ -566,7 +567,15 @@ func GenEnum(
contract.Assertf(ok,
"We have determined %s is a safe enum, which we define as "+
"being able to calculate a member for", t)
safeEnum(member)
if member != nil {
safeEnum(member)
} else {
unsafeEnum(from)
*diags = append(*diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("\"%v\" is an invalid enum value.", known.AsString()),
})
}
} else {
unsafeEnum(from)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/python/gen_program_expressions.go
Expand Up @@ -257,7 +257,7 @@ func (g *generator) GenFunctionCallExpression(w io.Writer, expr *model.FunctionC
g.Fgenf(w, "%s.%s.%s", pkg, enumName, tag)
}, func(from model.Expression) {
g.Fgenf(w, "%s.%s(%.v)", pkg, enumName, from)
})
}, &g.diagnostics)
}
default:
switch arg := from.(type) {
Expand Down

0 comments on commit 5cb5aba

Please sign in to comment.