Skip to content

schema/decoder: Support multiple OfType/OfScopeId in Reference #261

Open
@radeksimko

Description

@radeksimko

Context

Whilst working on #241, #232 (f2c8ee0 specifically) and investigating #170 it became clear that we need some more effective way to express that an attribute takes multiple types and/or scopes.

This function also expresses the problem:

func appendOrigins(origins, newOrigins reference.Origins) reference.Origins {
// Deduplicating origins like this is probably not ideal
// from performance perspective (N^2) but improving it would
// require redesign of the schema.Reference constraint,
// such that it doesn't necessitate the need of OneOf for multiple ScopeIds
// and maintains all possible ScopeIds & Types as a *single* slice.
for _, newOrigin := range newOrigins {
newMatchableOrigin, ok := newOrigin.(reference.MatchableOrigin)
if !ok {
origins = append(origins, newOrigin)
continue
}
foundMatch := false
for i, origin := range origins {
existingOrigin, ok := origin.(reference.MatchableOrigin)
if ok &&
existingOrigin.Address().Equals(newMatchableOrigin.Address()) &&
rangesEqual(existingOrigin.OriginRange(), newMatchableOrigin.OriginRange()) {
origins[i] = existingOrigin.AppendConstraints(newMatchableOrigin.OriginConstraints())
foundMatch = true
break
}
}
if !foundMatch {
origins = append(origins, newOrigin)
}
}
return origins
}

There are currently some known use cases in Terraform schema:

  • for_each - map or set
  • depends_on - multiple scopes (data, module, resource)

func forEachAttributeSchema() *schema.AttributeSchema {
return &schema.AttributeSchema{
IsOptional: true,
Constraint: schema.OneOf{
schema.AnyExpression{OfType: cty.Map(cty.DynamicPseudoType)},
schema.AnyExpression{OfType: cty.Set(cty.String)},
},
Description: lang.Markdown("A meta-argument that accepts a map or a set of strings, and creates an instance for each item in that map or set.\n\n" +
"**Note**: A given block cannot use both `count` and `for_each`."),
}
}

See also https://github.com/search?q=repo%3Ahashicorp%2Fterraform-schema+OfScopeId&type=code

Proposal

We should explore some more elegant ways of expressing such a constraint.

One possible solution would be to just introduce plural version OfType and OfScopeId, where validation ensures that either singular or plural is used (not both).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions