Skip to content

workflow: support optional() with defaults in object type constraints#168

Merged
brokenbot merged 2 commits into
mainfrom
optional-object-defaults
May 26, 2026
Merged

workflow: support optional() with defaults in object type constraints#168
brokenbot merged 2 commits into
mainfrom
optional-object-defaults

Conversation

@handcaught
Copy link
Copy Markdown
Member

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 inside object type constraints.

What changed:

  • Changes type parsing from typeexpr.Type() to typeexpr.TypeConstraintWithDefaults() across variable, shared_variable, and output declarations.
  • Adds TypeDefaults *typeexpr.Defaults to VariableNode, SharedVariableNode, and OutputNode.
  • Applies type-level optional defaults before cty type conversion at compile time (for foldable values) and at runtime (for output expressions evaluated against the final run state).
  • Switches shared_variable initial value type checking from strict Type().Equals() to convert.Convert(), which correctly allows optional-attribute object types.

Example:

variable "config" {
  type = object({
    name  = optional(string, "default_name")
    count = optional(number, 42)
  })
  default = {}
}

With this change, the compiled default value for config becomes {name = "default_name", count = 42} instead of failing with a type mismatch.

New tests:

  • TestVariableCompile_TypeDefaults
  • TestVariableCompile_TypeDefaults_MissingDefault
  • TestVariableCompile_TypeDefaults_SingleArgOptional
  • TestCompileSharedVariables_TypeDefaults
  • TestCompileOutputs_TypeDefaults_Object

All existing tests pass. make lint-go and make lint-imports are clean.

Dave Sanderson 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
Copy link
Copy Markdown
Collaborator

@brokenbot brokenbot left a comment

Choose a reason for hiding this comment

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

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 brokenbot merged commit 7132112 into main May 26, 2026
7 checks passed
@brokenbot brokenbot deleted the optional-object-defaults branch May 26, 2026 03:56
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>
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.

2 participants