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

hclext: Add schema mode to BodySchema #201

Merged
merged 2 commits into from
Oct 2, 2022
Merged

Conversation

wata727
Copy link
Member

@wata727 wata727 commented Oct 1, 2022

This PR introduces a "schema mode" to hclext.BodySchema.

Previously, the hclext.Content was not possible to retrieve the body if the nested block contained only attributes with unknown schemas.

body := toHCLBody(`
locals {
  foo = "foo"
  bar = "bar"
}`)

hclext.Content(body, &hclext.BodySchema{
  Blocks: Blocks{
    {
      Type: "locals",
      Body: &hclext.BodySchema{}, // what attributes are declared?
    },
  },
})

On the other hand, in HCL you can retrieve them using JustAttributes.

nestedBody := body.Content(&hcl.BodySchema{
  Blocks: Blocks{
    {
      Type: "locals",
    },
  },
})

nestedBody.JustAttributes() // => "foo", "bar" attributes

This is clearly a problem with supporting nested schema. Here we introduce the concept of modes into the schema, allowing us to call JustAttributes for nested blocks.

body := hclext.Content(body, &hclext.BodySchema{
  Blocks: Blocks{
    {
      Type: "locals",
      Body: &hclext.BodySchema{Mode: hclext.SchemaJustAttributesMode},
    },
  },
})

body.Blocks[0].Body.Attributes  // => "foo", "bar" attributes

Note that in just attributes mode, any other Attributes or Blocks schema are ignored. Internally it calls HCL's JustAttributes, so it returns a diagnostic if the body has blocks. In this mode, there is no difference in behavior between Content and PartialContent because it is schemaless.

This feature is available since TFLint v0.42

@wata727 wata727 merged commit eed3c18 into master Oct 2, 2022
@wata727 wata727 deleted the hclext_just_attributes branch October 2, 2022 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant