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

decoder: Decouple each validator into its own type #297

Merged
merged 3 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion decoder/candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"

"github.com/hashicorp/hcl-lang/decoder/internal/schemahelper"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
Expand Down Expand Up @@ -142,7 +143,7 @@ func (d *PathDecoder) candidatesAtPos(ctx context.Context, body *hclsyntax.Body,
}

if block.Body != nil && block.Body.Range().ContainsPos(pos) {
mergedSchema, err := mergeBlockBodySchemas(block.AsHCLBlock(), blockSchema)
mergedSchema, err := schemahelper.MergeBlockBodySchemas(block.AsHCLBlock(), blockSchema)
if err != nil {
return lang.ZeroCandidates(), err
}
Expand Down
165 changes: 0 additions & 165 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ package decoder
import (
"fmt"

"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)

type Decoder struct {
Expand All @@ -32,169 +30,6 @@ func posEqual(pos, other hcl.Pos) bool {
pos.Byte == other.Byte
}

func mergeBlockBodySchemas(block *hcl.Block, blockSchema *schema.BlockSchema) (*schema.BodySchema, error) {
mergedSchema := &schema.BodySchema{}
if blockSchema.Body != nil {
mergedSchema = blockSchema.Body.Copy()
}
if mergedSchema.Attributes == nil {
mergedSchema.Attributes = make(map[string]*schema.AttributeSchema, 0)
}
if mergedSchema.Blocks == nil {
mergedSchema.Blocks = make(map[string]*schema.BlockSchema, 0)
}
if mergedSchema.TargetableAs == nil {
mergedSchema.TargetableAs = make([]*schema.Targetable, 0)
}
if mergedSchema.ImpliedOrigins == nil {
mergedSchema.ImpliedOrigins = make([]schema.ImpliedOrigin, 0)
}

depSchema, _, ok := NewBlockSchema(blockSchema).DependentBodySchema(block)
if ok {
for name, attr := range depSchema.Attributes {
if _, exists := mergedSchema.Attributes[name]; !exists {
mergedSchema.Attributes[name] = attr
} else {
// Skip duplicate attribute
continue
}
}
for bType, block := range depSchema.Blocks {
if _, exists := mergedSchema.Blocks[bType]; !exists {
// propagate DynamicBlocks extension to any nested blocks
if mergedSchema.Extensions != nil && mergedSchema.Extensions.DynamicBlocks {
if block.Body.Extensions == nil {
block.Body.Extensions = &schema.BodyExtensions{}
}
block.Body.Extensions.DynamicBlocks = true
}

mergedSchema.Blocks[bType] = block
} else {
// Skip duplicate block type
continue
}
}

if mergedSchema.Extensions != nil && mergedSchema.Extensions.DynamicBlocks && len(depSchema.Blocks) > 0 {
mergedSchema.Blocks["dynamic"] = buildDynamicBlockSchema(depSchema)
}

mergedSchema.TargetableAs = append(mergedSchema.TargetableAs, depSchema.TargetableAs...)
mergedSchema.ImpliedOrigins = append(mergedSchema.ImpliedOrigins, depSchema.ImpliedOrigins...)

// TODO: avoid resetting?
mergedSchema.Targets = depSchema.Targets.Copy()

// TODO: avoid resetting?
mergedSchema.DocsLink = depSchema.DocsLink.Copy()

// use extensions of DependentBody if not nil
// (to avoid resetting to nil)
if depSchema.Extensions != nil {
mergedSchema.Extensions = depSchema.Extensions.Copy()
}
} else if !ok && mergedSchema.Extensions != nil && mergedSchema.Extensions.DynamicBlocks && len(mergedSchema.Blocks) > 0 {
// dynamic blocks are only relevant for dependent schemas,
// but we may end up here because the schema is a result
// of merged static + dependent schema from previous iteration

// propagate DynamicBlocks extension to any nested blocks
if mergedSchema.Extensions != nil && mergedSchema.Extensions.DynamicBlocks {
for bType, block := range mergedSchema.Blocks {
if block.Body.Extensions == nil {
block.Body.Extensions = &schema.BodyExtensions{}
}
block.Body.Extensions.DynamicBlocks = true
mergedSchema.Blocks[bType] = block
}
}

mergedSchema.Blocks["dynamic"] = buildDynamicBlockSchema(mergedSchema)
}

return mergedSchema, nil
}

// blockContent represents HCL or JSON block content
type blockContent struct {
*hcl.Block

// Range represents range of the block in HCL syntax
// or closest available representative range in JSON
Range hcl.Range
}

// bodyContent represents an HCL or JSON body content
type bodyContent struct {
Attributes hcl.Attributes
Blocks []*blockContent
RangePtr *hcl.Range
}

// decodeBody produces content of either HCL or JSON body
//
// JSON body requires schema for decoding, empty bodyContent
// is returned if nil schema is provided
func decodeBody(body hcl.Body, bodySchema *schema.BodySchema) bodyContent {
content := bodyContent{
Attributes: make(hcl.Attributes, 0),
Blocks: make([]*blockContent, 0),
}

// More common HCL syntax is processed directly (without schema)
// which also better represents the reality in symbol lookups
// i.e. expressions written as opposed to schema requirements
if hclBody, ok := body.(*hclsyntax.Body); ok {
for name, attr := range hclBody.Attributes {
content.Attributes[name] = attr.AsHCLAttribute()
}

for _, block := range hclBody.Blocks {
content.Blocks = append(content.Blocks, &blockContent{
Block: block.AsHCLBlock(),
Range: block.Range(),
})
}

content.RangePtr = hclBody.Range().Ptr()

return content
}

// JSON syntax cannot be decoded without schema as attributes
// and blocks are otherwise ambiguous
if bodySchema != nil {
hclSchema := bodySchema.ToHCLSchema()
bContent, remainingBody, _ := body.PartialContent(hclSchema)

content.Attributes = bContent.Attributes
if bodySchema.AnyAttribute != nil {
// Remaining unknown fields may also be blocks in JSON,
// but we blindly treat them as attributes here
// as we cannot do any better without upstream HCL changes.
remainingAttrs, _ := remainingBody.JustAttributes()
for name, attr := range remainingAttrs {
content.Attributes[name] = attr
}
}

for _, block := range bContent.Blocks {
// hcl.Block interface (as the only way of accessing block in JSON)
// does not come with Range for the block, so we calculate it here
rng := hcl.RangeBetween(block.DefRange, block.Body.MissingItemRange())

content.Blocks = append(content.Blocks, &blockContent{
Block: block,
Range: rng,
})
}
}

return content
}

func stringPos(pos hcl.Pos) string {
return fmt.Sprintf("%d,%d", pos.Line, pos.Column)
}
57 changes: 0 additions & 57 deletions decoder/extension_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,63 +32,6 @@ func forEachAttributeSchema() *schema.AttributeSchema {
}
}

func buildDynamicBlockSchema(inputSchema *schema.BodySchema) *schema.BlockSchema {
dependentBody := make(map[schema.SchemaKey]*schema.BodySchema)
for blockName, block := range inputSchema.Blocks {
dependentBody[schema.NewSchemaKey(schema.DependencyKeys{
Labels: []schema.LabelDependent{
{Index: 0, Value: blockName},
},
})] = &schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"content": {
Description: lang.PlainText("The body of each generated block"),
MaxItems: 1,
Body: block.Body.Copy(),
},
},
}
}

return &schema.BlockSchema{
Description: lang.Markdown("A dynamic block to produce blocks dynamically by iterating over a given complex value"),
Labels: []*schema.LabelSchema{
{
Name: "name",
Completable: true,
IsDepKey: true,
},
},
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"for_each": {
Constraint: schema.OneOf{
schema.AnyExpression{OfType: cty.Map(cty.DynamicPseudoType)},
schema.AnyExpression{OfType: cty.Set(cty.String)},
},
IsRequired: true,
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."),
},
"iterator": {
Constraint: schema.LiteralType{Type: cty.String},
IsOptional: true,
Description: lang.Markdown("The name of a temporary variable that represents the current " +
"element of the complex value. Defaults to the label of the dynamic block."),
},
"labels": {
Constraint: schema.AnyExpression{
OfType: cty.List(cty.String),
},
IsOptional: true,
Description: lang.Markdown("A list of strings that specifies the block labels, " +
"in order, to use for each generated block."),
},
},
},
DependentBody: dependentBody,
}
}

func countIndexReferenceTarget(attr *hcl.Attribute, bodyRange hcl.Range) reference.Target {
return reference.Target{
LocalAddr: lang.Address{
Expand Down
5 changes: 3 additions & 2 deletions decoder/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sort"
"strings"

"github.com/hashicorp/hcl-lang/decoder/internal/schemahelper"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl-lang/schema"
Expand Down Expand Up @@ -143,7 +144,7 @@ func (d *PathDecoder) hoverAtPos(ctx context.Context, body *hclsyntax.Body, body
}

if block.Body != nil && block.Body.Range().ContainsPos(pos) {
mergedSchema, err := mergeBlockBodySchemas(block.AsHCLBlock(), blockSchema)
mergedSchema, err := schemahelper.MergeBlockBodySchemas(block.AsHCLBlock(), blockSchema)
if err != nil {
return nil, err
}
Expand All @@ -166,7 +167,7 @@ func (d *PathDecoder) hoverContentForLabel(i int, block *hclsyntax.Block, bSchem
labelSchema := bSchema.Labels[i]

if labelSchema.IsDepKey {
bs, _, ok := NewBlockSchema(bSchema).DependentBodySchema(block.AsHCLBlock())
bs, _, ok := schemahelper.NewBlockSchema(bSchema).DependentBodySchema(block.AsHCLBlock())
if ok {
content := fmt.Sprintf("`%s`", value)
if bs.Detail != "" {
Expand Down
24 changes: 24 additions & 0 deletions decoder/internal/ast/ast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package ast

import (
"github.com/hashicorp/hcl/v2"
)

// blockContent represents HCL or JSON block content
type BlockContent struct {
*hcl.Block

// Range represents range of the block in HCL syntax
// or closest available representative range in JSON
Range hcl.Range
}

// bodyContent represents an HCL or JSON body content
type BodyContent struct {
Attributes hcl.Attributes
Blocks []*BlockContent
RangePtr *hcl.Range
}
71 changes: 71 additions & 0 deletions decoder/internal/ast/decode_body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package ast

import (
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)

// DecodeBody produces content of either HCL or JSON body
// JSON body requires schema for decoding, empty bodyContent
// is returned if nil schema is provided
func DecodeBody(body hcl.Body, bodySchema *schema.BodySchema) BodyContent {
content := BodyContent{
Attributes: make(hcl.Attributes, 0),
Blocks: make([]*BlockContent, 0),
}

// More common HCL syntax is processed directly (without schema)
// which also better represents the reality in symbol lookups
// i.e. expressions written as opposed to schema requirements
if hclBody, ok := body.(*hclsyntax.Body); ok {
for name, attr := range hclBody.Attributes {
content.Attributes[name] = attr.AsHCLAttribute()
}

for _, block := range hclBody.Blocks {
content.Blocks = append(content.Blocks, &BlockContent{
Block: block.AsHCLBlock(),
Range: block.Range(),
})
}

content.RangePtr = hclBody.Range().Ptr()

return content
}

// JSON syntax cannot be decoded without schema as attributes
// and blocks are otherwise ambiguous
if bodySchema != nil {
hclSchema := bodySchema.ToHCLSchema()
bContent, remainingBody, _ := body.PartialContent(hclSchema)

content.Attributes = bContent.Attributes
if bodySchema.AnyAttribute != nil {
// Remaining unknown fields may also be blocks in JSON,
// but we blindly treat them as attributes here
// as we cannot do any better without upstream HCL changes.
remainingAttrs, _ := remainingBody.JustAttributes()
for name, attr := range remainingAttrs {
content.Attributes[name] = attr
}
}

for _, block := range bContent.Blocks {
// hcl.Block interface (as the only way of accessing block in JSON)
// does not come with Range for the block, so we calculate it here
rng := hcl.RangeBetween(block.DefRange, block.Body.MissingItemRange())

content.Blocks = append(content.Blocks, &BlockContent{
Block: block,
Range: rng,
})
}
}

return content
}
Loading