Skip to content

Commit

Permalink
Add type attribute to output blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Jan 24, 2025
1 parent 3c57915 commit 3b7d884
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
28 changes: 28 additions & 0 deletions internal/configs/named_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,23 @@ type Output struct {
Sensitive bool
Ephemeral bool

// ConstraintType is a type constraint which the result is guaranteed
// to conform to when used in the calling module.
ConstraintType cty.Type
// TypeDefaults describes any optional attribute defaults that should be
// applied to the Expr result before type conversion.
TypeDefaults *typeexpr.Defaults

Preconditions []*CheckRule

DescriptionSet bool
SensitiveSet bool
EphemeralSet bool
// TypeSet is true if there was an explicit "type" argument in the
// configuration block. This is mainly to allow distinguish explicitly
// setting vs. just using the default type constraint when processing
// override files.
TypeSet bool

DeclRange hcl.Range
}
Expand Down Expand Up @@ -390,6 +402,19 @@ func decodeOutputBlock(block *hcl.Block, override bool) (*Output, hcl.Diagnostic
o.Expr = attr.Expr
}

if attr, exists := content.Attributes["type"]; exists {
ty, defaults, moreDiags := typeexpr.TypeConstraintWithDefaults(attr.Expr)
diags = append(diags, moreDiags...)
o.ConstraintType = ty
o.TypeDefaults = defaults
o.TypeSet = true
}
if o.ConstraintType == cty.NilType {
// If no constraint is given then the type will be inferred
// automatically from the value.
o.ConstraintType = cty.DynamicPseudoType
}

if attr, exists := content.Attributes["sensitive"]; exists {
valDiags := gohcl.DecodeExpression(attr.Expr, nil, &o.Sensitive)
diags = append(diags, valDiags...)
Expand Down Expand Up @@ -525,6 +550,9 @@ var outputBlockSchema = &hcl.BodySchema{
{
Name: "ephemeral",
},
{
Name: "type",
},
},
Blocks: []hcl.BlockHeaderSchema{
{Type: "precondition"},
Expand Down
11 changes: 11 additions & 0 deletions internal/configs/testdata/valid-files/output-type-constraint.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "string" {
type = string
value = "Hello"
}

output "object" {
type = object({
name = optional(string, "Bart"),
})
value = {}
}

0 comments on commit 3b7d884

Please sign in to comment.