workflow: support optional() with defaults in object type constraints#168
Merged
Conversation
added 2 commits
May 25, 2026 21:39
Replaces typeexpr.Type() with typeexpr.TypeConstraintWithDefaults() when
parsing variable, shared_variable, and output type expressions. This enables
the Terraform-style optional attribute modifier with default values:
type = object({
a = optional(string, "default_a")
b = optional(number, 42)
})
Type-level defaults are stored on the compiled node (VariableNode,
SharedVariableNode, OutputNode) and applied before cty type conversion at
both compile time (for foldable values) and runtime (for output expressions
evaluated against the final run state).
Compile-time behavioral changes:
- variable default = {} now resolves missing optional fields from the type.
- shared_variable initial values are coerced via convert.Convert (replacing
strict type equality) so optional-attribute objects match correctly.
- output foldable values get defaults applied before compile-time type check.
Runtime behavioral changes:
- evalRunOutputs and evalRunOutputsAsValues apply defaults before converting
output expressions to their declared types.
Tests cover:
- variable with object optional defaults
- shared_variable with object optional defaults
- output with object optional defaults
- single-arg optional (no default value) still parses and resolves to null
brokenbot
approved these changes
May 26, 2026
Collaborator
brokenbot
left a comment
There was a problem hiding this comment.
The implementation is clean and idiomatic. Support for optional() defaults in object type constraints is correctly implemented across variables, shared variables, and outputs, both at compile-time and during runtime evaluation. Tests are thorough and cover the relevant edge cases. Approved.
brokenbot
pushed a commit
that referenced
this pull request
May 26, 2026
…#168) (#169) * workflow: support optional() with defaults in object type constraints Replaces typeexpr.Type() with typeexpr.TypeConstraintWithDefaults() when parsing variable, shared_variable, and output type expressions. This enables the Terraform-style optional attribute modifier with default values: type = object({ a = optional(string, "default_a") b = optional(number, 42) }) Type-level defaults are stored on the compiled node (VariableNode, SharedVariableNode, OutputNode) and applied before cty type conversion at both compile time (for foldable values) and runtime (for output expressions evaluated against the final run state). Compile-time behavioral changes: - variable default = {} now resolves missing optional fields from the type. - shared_variable initial values are coerced via convert.Convert (replacing strict type equality) so optional-attribute objects match correctly. - output foldable values get defaults applied before compile-time type check. Runtime behavioral changes: - evalRunOutputs and evalRunOutputsAsValues apply defaults before converting output expressions to their declared types. Tests cover: - variable with object optional defaults - shared_variable with object optional defaults - output with object optional defaults - single-arg optional (no default value) still parses and resolves to null * run make spec-gen --------- Co-authored-by: Dave Sanderson <dave@brokenbots.net>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces
typeexpr.Type()withtypeexpr.TypeConstraintWithDefaults()when parsing variable, shared_variable, and output type expressions. This enables the Terraform-styleoptional()attribute modifier with default values inside object type constraints.What changed:
typeexpr.Type()totypeexpr.TypeConstraintWithDefaults()across variable, shared_variable, and output declarations.TypeDefaults *typeexpr.DefaultstoVariableNode,SharedVariableNode, andOutputNode.Type().Equals()toconvert.Convert(), which correctly allows optional-attribute object types.Example:
With this change, the compiled default value for
configbecomes{name = "default_name", count = 42}instead of failing with a type mismatch.New tests:
TestVariableCompile_TypeDefaultsTestVariableCompile_TypeDefaults_MissingDefaultTestVariableCompile_TypeDefaults_SingleArgOptionalTestCompileSharedVariables_TypeDefaultsTestCompileOutputs_TypeDefaults_ObjectAll existing tests pass.
make lint-goandmake lint-importsare clean.