Skip to content

Commit

Permalink
Implement reference validation
Browse files Browse the repository at this point in the history
Add the ability to use the collected origin and target references in early validation.

Validation funcs will be provided by terraform-ls for now.
  • Loading branch information
jpogran committed Aug 10, 2023
1 parent 5997d13 commit f3a1433
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
13 changes: 8 additions & 5 deletions decoder/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (

"github.com/hashicorp/hcl-lang/decoder/internal/validator"
"github.com/hashicorp/hcl-lang/decoder/internal/walker"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)

// Validate returns a set of Diagnostics for all known files
func (d *PathDecoder) Validate(ctx context.Context) (map[string]hcl.Diagnostics, error) {
diags := make(map[string]hcl.Diagnostics)
func (d *PathDecoder) Validate(ctx context.Context) (lang.DiagnosticsMap, error) {
diags := make(lang.DiagnosticsMap)
if d.pathCtx.Schema == nil {
return diags, &NoSchemaError{}
}
Expand Down Expand Up @@ -44,10 +45,12 @@ func (d *PathDecoder) Validate(ctx context.Context) (map[string]hcl.Diagnostics,
})
}

ctx = withPathContext(ctx, d.pathCtx)

// Run validation functions
// for _, vFunc := range d.decoderCtx.Validations {
// diags = diags.Extend(vFunc(ctx))
// }
for _, vFunc := range d.decoderCtx.Validations {
diags = diags.Extend(vFunc(ctx))
}

return diags, nil
}
Expand Down
16 changes: 15 additions & 1 deletion lang/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,18 @@ import (
"github.com/hashicorp/hcl/v2"
)

type ValidationFunc func(ctx context.Context) hcl.Diagnostics
type ValidationFunc func(ctx context.Context) DiagnosticsMap

type DiagnosticsMap map[string]hcl.Diagnostics

func (dm DiagnosticsMap) Extend(diagMap DiagnosticsMap) DiagnosticsMap {
for fileName, diags := range diagMap {
_, ok := dm[fileName]
if !ok {
dm[fileName] = make(hcl.Diagnostics, 0)
}
dm[fileName].Extend(diags)
}

return dm
}

0 comments on commit f3a1433

Please sign in to comment.