Skip to content

Commit

Permalink
Move the zcl package and its two parsing subpackages to "hcl" names
Browse files Browse the repository at this point in the history
This is a super-invasive update since the "zcl" package in particular
is referenced all over.

There are probably still a few zcl references hanging around in comments,
etc but this takes care of most of it.
  • Loading branch information
apparentlymart committed Sep 11, 2017
1 parent 0dc3a60 commit 708abb8
Show file tree
Hide file tree
Showing 115 changed files with 5,394 additions and 5,392 deletions.
6 changes: 3 additions & 3 deletions cmd/hclfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"os"
"strings"

"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hclparse"
"github.com/hashicorp/hcl2/hclwrite"
"github.com/hashicorp/hcl2/zcl"
"golang.org/x/crypto/ssh/terminal"
)

Expand All @@ -25,7 +25,7 @@ var (
)

var parser = hclparse.NewParser()
var diagWr zcl.DiagnosticWriter // initialized in init
var diagWr hcl.DiagnosticWriter // initialized in init
var checkErrs = false
var changed []string

Expand All @@ -35,7 +35,7 @@ func init() {
if err != nil {
w = 80
}
diagWr = zcl.NewDiagnosticTextWriter(os.Stderr, parser.Files(), uint(w), color)
diagWr = hcl.NewDiagnosticTextWriter(os.Stderr, parser.Files(), uint(w), color)
}

func main() {
Expand Down
8 changes: 4 additions & 4 deletions ext/include/file_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/hashicorp/hcl2/hclparse"
"github.com/hashicorp/hcl2/zcl"
"github.com/hashicorp/hcl2/hcl"
)

// FileResolver creates and returns a Resolver that interprets include paths
Expand Down Expand Up @@ -35,13 +35,13 @@ type fileResolver struct {
Parser *hclparse.Parser
}

func (r fileResolver) ResolveBodyPath(path string, refRange zcl.Range) (zcl.Body, zcl.Diagnostics) {
func (r fileResolver) ResolveBodyPath(path string, refRange hcl.Range) (hcl.Body, hcl.Diagnostics) {
callerFile := filepath.Join(r.BaseDir, refRange.Filename)
callerDir := filepath.Dir(callerFile)
targetFile := filepath.Join(callerDir, path)

var f *zcl.File
var diags zcl.Diagnostics
var f *hcl.File
var diags hcl.Diagnostics
if strings.HasSuffix(targetFile, ".json") {
f, diags = r.Parser.ParseJSONFile(targetFile)
} else {
Expand Down
10 changes: 5 additions & 5 deletions ext/include/map_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ package include
import (
"fmt"

"github.com/hashicorp/hcl2/zcl"
"github.com/hashicorp/hcl2/hcl"
)

// MapResolver returns a Resolver that consults the given map for preloaded
// bodies (the values) associated with static include paths (the keys).
//
// An error diagnostic is returned if a path is requested that does not appear
// as a key in the given map.
func MapResolver(m map[string]zcl.Body) Resolver {
return ResolverFunc(func(path string, refRange zcl.Range) (zcl.Body, zcl.Diagnostics) {
func MapResolver(m map[string]hcl.Body) Resolver {
return ResolverFunc(func(path string, refRange hcl.Range) (hcl.Body, hcl.Diagnostics) {
if body, ok := m[path]; ok {
return body, nil
}

return nil, zcl.Diagnostics{
return nil, hcl.Diagnostics{
{
Severity: zcl.DiagError,
Severity: hcl.DiagError,
Summary: "Invalid include path",
Detail: fmt.Sprintf("The include path %q is not recognized.", path),
Subject: &refRange,
Expand Down
10 changes: 5 additions & 5 deletions ext/include/resolver.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package include

import (
"github.com/hashicorp/hcl2/zcl"
"github.com/hashicorp/hcl2/hcl"
)

// A Resolver maps an include path (an arbitrary string, but usually something
// filepath-like) to a zcl.Body.
// filepath-like) to a hcl.Body.
//
// The parameter "refRange" is the source range of the expression in the calling
// body that provided the given path, for use in generating "invalid path"-type
Expand All @@ -16,13 +16,13 @@ import (
// Any returned diagnostics will be emitted when content is requested from the
// final composed body (after all includes have been dealt with).
type Resolver interface {
ResolveBodyPath(path string, refRange zcl.Range) (zcl.Body, zcl.Diagnostics)
ResolveBodyPath(path string, refRange hcl.Range) (hcl.Body, hcl.Diagnostics)
}

// ResolverFunc is a function type that implements Resolver.
type ResolverFunc func(path string, refRange zcl.Range) (zcl.Body, zcl.Diagnostics)
type ResolverFunc func(path string, refRange hcl.Range) (hcl.Body, hcl.Diagnostics)

// ResolveBodyPath is an implementation of Resolver.ResolveBodyPath.
func (f ResolverFunc) ResolveBodyPath(path string, refRange zcl.Range) (zcl.Body, zcl.Diagnostics) {
func (f ResolverFunc) ResolveBodyPath(path string, refRange hcl.Range) (hcl.Body, hcl.Diagnostics) {
return f(path, refRange)
}
22 changes: 11 additions & 11 deletions ext/include/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package include
import (
"github.com/hashicorp/hcl2/ext/transform"
"github.com/hashicorp/hcl2/gohcl"
"github.com/hashicorp/hcl2/zcl"
"github.com/hashicorp/hcl2/hcl"
)

// Transformer builds a transformer that finds any "include" blocks in a body
Expand All @@ -30,10 +30,10 @@ import (
// // "body" will now have includes resolved in its own content and that
// // of any descendent blocks.
//
func Transformer(blockType string, ctx *zcl.EvalContext, resolver Resolver) transform.Transformer {
func Transformer(blockType string, ctx *hcl.EvalContext, resolver Resolver) transform.Transformer {
return &transformer{
Schema: &zcl.BodySchema{
Blocks: []zcl.BlockHeaderSchema{
Schema: &hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
{
Type: blockType,
},
Expand All @@ -45,20 +45,20 @@ func Transformer(blockType string, ctx *zcl.EvalContext, resolver Resolver) tran
}

type transformer struct {
Schema *zcl.BodySchema
Ctx *zcl.EvalContext
Schema *hcl.BodySchema
Ctx *hcl.EvalContext
Resolver Resolver
}

func (t *transformer) TransformBody(in zcl.Body) zcl.Body {
func (t *transformer) TransformBody(in hcl.Body) hcl.Body {
content, remain, diags := in.PartialContent(t.Schema)

if content == nil || len(content.Blocks) == 0 {
// Nothing to do!
return transform.BodyWithDiagnostics(remain, diags)
}

bodies := make([]zcl.Body, 1, len(content.Blocks)+1)
bodies := make([]hcl.Body, 1, len(content.Blocks)+1)
bodies[0] = remain // content in "remain" takes priority over includes
for _, block := range content.Blocks {
incContent, incDiags := block.Body.Content(includeBlockSchema)
Expand All @@ -79,11 +79,11 @@ func (t *transformer) TransformBody(in zcl.Body) zcl.Body {
bodies = append(bodies, transform.BodyWithDiagnostics(incBody, incDiags))
}

return zcl.MergeBodies(bodies)
return hcl.MergeBodies(bodies)
}

var includeBlockSchema = &zcl.BodySchema{
Attributes: []zcl.AttributeSchema{
var includeBlockSchema = &hcl.BodySchema{
Attributes: []hcl.AttributeSchema{
{
Name: "path",
Required: true,
Expand Down
38 changes: 19 additions & 19 deletions ext/include/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,59 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/hcl2/gohcl"
"github.com/hashicorp/hcl2/hcltest"
"github.com/hashicorp/hcl2/zcl"
"github.com/hashicorp/hcl2/hcl"
"github.com/zclconf/go-cty/cty"
)

func TestTransformer(t *testing.T) {
caller := hcltest.MockBody(&zcl.BodyContent{
Blocks: zcl.Blocks{
caller := hcltest.MockBody(&hcl.BodyContent{
Blocks: hcl.Blocks{
{
Type: "include",
Body: hcltest.MockBody(&zcl.BodyContent{
Attributes: hcltest.MockAttrs(map[string]zcl.Expression{
Body: hcltest.MockBody(&hcl.BodyContent{
Attributes: hcltest.MockAttrs(map[string]hcl.Expression{
"path": hcltest.MockExprVariable("var_path"),
}),
}),
},
{
Type: "include",
Body: hcltest.MockBody(&zcl.BodyContent{
Attributes: hcltest.MockAttrs(map[string]zcl.Expression{
Body: hcltest.MockBody(&hcl.BodyContent{
Attributes: hcltest.MockAttrs(map[string]hcl.Expression{
"path": hcltest.MockExprLiteral(cty.StringVal("include2")),
}),
}),
},
{
Type: "foo",
Body: hcltest.MockBody(&zcl.BodyContent{
Attributes: hcltest.MockAttrs(map[string]zcl.Expression{
Body: hcltest.MockBody(&hcl.BodyContent{
Attributes: hcltest.MockAttrs(map[string]hcl.Expression{
"from": hcltest.MockExprLiteral(cty.StringVal("caller")),
}),
}),
},
},
})

resolver := MapResolver(map[string]zcl.Body{
"include1": hcltest.MockBody(&zcl.BodyContent{
Blocks: zcl.Blocks{
resolver := MapResolver(map[string]hcl.Body{
"include1": hcltest.MockBody(&hcl.BodyContent{
Blocks: hcl.Blocks{
{
Type: "foo",
Body: hcltest.MockBody(&zcl.BodyContent{
Attributes: hcltest.MockAttrs(map[string]zcl.Expression{
Body: hcltest.MockBody(&hcl.BodyContent{
Attributes: hcltest.MockAttrs(map[string]hcl.Expression{
"from": hcltest.MockExprLiteral(cty.StringVal("include1")),
}),
}),
},
},
}),
"include2": hcltest.MockBody(&zcl.BodyContent{
Blocks: zcl.Blocks{
"include2": hcltest.MockBody(&hcl.BodyContent{
Blocks: hcl.Blocks{
{
Type: "foo",
Body: hcltest.MockBody(&zcl.BodyContent{
Attributes: hcltest.MockAttrs(map[string]zcl.Expression{
Body: hcltest.MockBody(&hcl.BodyContent{
Attributes: hcltest.MockAttrs(map[string]hcl.Expression{
"from": hcltest.MockExprLiteral(cty.StringVal("include2")),
}),
}),
Expand All @@ -68,7 +68,7 @@ func TestTransformer(t *testing.T) {
}),
})

ctx := &zcl.EvalContext{
ctx := &hcl.EvalContext{
Variables: map[string]cty.Value{
"var_path": cty.StringVal("include1"),
},
Expand Down
36 changes: 18 additions & 18 deletions ext/transform/error.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package transform

import (
"github.com/hashicorp/hcl2/zcl"
"github.com/hashicorp/hcl2/hcl"
)

// NewErrorBody returns a zcl.Body that returns the given diagnostics whenever
// NewErrorBody returns a hcl.Body that returns the given diagnostics whenever
// any of its content-access methods are called.
//
// The given diagnostics must have at least one diagnostic of severity
// zcl.DiagError, or this function will panic.
// hcl.DiagError, or this function will panic.
//
// This can be used to prepare a return value for a Transformer that
// can't complete due to an error. While the transform itself will succeed,
// the error will be returned as soon as a caller attempts to extract content
// from the resulting body.
func NewErrorBody(diags zcl.Diagnostics) zcl.Body {
func NewErrorBody(diags hcl.Diagnostics) hcl.Body {
if !diags.HasErrors() {
panic("NewErrorBody called without any error diagnostics")
}
Expand All @@ -23,7 +23,7 @@ func NewErrorBody(diags zcl.Diagnostics) zcl.Body {
}
}

// BodyWithDiagnostics returns a zcl.Body that wraps another zcl.Body
// BodyWithDiagnostics returns a hcl.Body that wraps another hcl.Body
// and emits the given diagnostics for any content-extraction method.
//
// Unlike the result of NewErrorBody, a body with diagnostics still runs
Expand All @@ -36,7 +36,7 @@ func NewErrorBody(diags zcl.Diagnostics) zcl.Body {
// This function is intended for conveniently reporting errors and/or warnings
// produced during a transform, ensuring that they will be seen when the
// caller eventually extracts content from the returned body.
func BodyWithDiagnostics(body zcl.Body, diags zcl.Diagnostics) zcl.Body {
func BodyWithDiagnostics(body hcl.Body, diags hcl.Diagnostics) hcl.Body {
if len(diags) == 0 {
// nothing to do!
return body
Expand All @@ -49,60 +49,60 @@ func BodyWithDiagnostics(body zcl.Body, diags zcl.Diagnostics) zcl.Body {
}

type diagBody struct {
Diags zcl.Diagnostics
Wrapped zcl.Body
Diags hcl.Diagnostics
Wrapped hcl.Body
}

func (b diagBody) Content(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Diagnostics) {
func (b diagBody) Content(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Diagnostics) {
if b.Diags.HasErrors() {
return b.emptyContent(), b.Diags
}

content, wrappedDiags := b.Wrapped.Content(schema)
diags := make(zcl.Diagnostics, 0, len(b.Diags)+len(wrappedDiags))
diags := make(hcl.Diagnostics, 0, len(b.Diags)+len(wrappedDiags))
diags = append(diags, b.Diags...)
diags = append(diags, wrappedDiags...)
return content, diags
}

func (b diagBody) PartialContent(schema *zcl.BodySchema) (*zcl.BodyContent, zcl.Body, zcl.Diagnostics) {
func (b diagBody) PartialContent(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Body, hcl.Diagnostics) {
if b.Diags.HasErrors() {
return b.emptyContent(), b.Wrapped, b.Diags
}

content, remain, wrappedDiags := b.Wrapped.PartialContent(schema)
diags := make(zcl.Diagnostics, 0, len(b.Diags)+len(wrappedDiags))
diags := make(hcl.Diagnostics, 0, len(b.Diags)+len(wrappedDiags))
diags = append(diags, b.Diags...)
diags = append(diags, wrappedDiags...)
return content, remain, diags
}

func (b diagBody) JustAttributes() (zcl.Attributes, zcl.Diagnostics) {
func (b diagBody) JustAttributes() (hcl.Attributes, hcl.Diagnostics) {
if b.Diags.HasErrors() {
return nil, b.Diags
}

attributes, wrappedDiags := b.Wrapped.JustAttributes()
diags := make(zcl.Diagnostics, 0, len(b.Diags)+len(wrappedDiags))
diags := make(hcl.Diagnostics, 0, len(b.Diags)+len(wrappedDiags))
diags = append(diags, b.Diags...)
diags = append(diags, wrappedDiags...)
return attributes, diags
}

func (b diagBody) MissingItemRange() zcl.Range {
func (b diagBody) MissingItemRange() hcl.Range {
if b.Wrapped != nil {
return b.Wrapped.MissingItemRange()
}

// Placeholder. This should never be seen in practice because decoding
// a diagBody without a wrapped body should always produce an error.
return zcl.Range{
return hcl.Range{
Filename: "<empty>",
}
}

func (b diagBody) emptyContent() *zcl.BodyContent {
return &zcl.BodyContent{
func (b diagBody) emptyContent() *hcl.BodyContent {
return &hcl.BodyContent{
MissingItemRange: b.MissingItemRange(),
}
}
Loading

0 comments on commit 708abb8

Please sign in to comment.