Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
radeksimko opened this issue Apr 14, 2023 · 0 comments
Open

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

radeksimko opened this issue Apr 14, 2023 · 0 comments
Labels
enhancement New feature or request technical-debt

Comments

@radeksimko
Copy link
Member

radeksimko commented Apr 14, 2023

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).

@radeksimko radeksimko added enhancement New feature or request technical-debt labels Apr 14, 2023
@radeksimko radeksimko changed the title schema/decoder: Replace Reference's OfType/OfScopeId with plural slices schema/decoder: Support multiple OfType/OfScopeId in Reference Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request technical-debt
Projects
None yet
Development

No branches or pull requests

1 participant