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

Added support for #14

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 38 additions & 1 deletion convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,31 @@ import (
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
ctyconvert "github.com/zclconf/go-cty/cty/convert"
"github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/function/stdlib"
ctyjson "github.com/zclconf/go-cty/cty/json"
"strconv"
"strings"
)

var specFuncs = map[string]function.Function{
"abs": stdlib.AbsoluteFunc,
"coalesce": stdlib.CoalesceFunc,
"concat": stdlib.ConcatFunc,
"hasindex": stdlib.HasIndexFunc,
"int": stdlib.IntFunc,
"jsondecode": stdlib.JSONDecodeFunc,
"jsonencode": stdlib.JSONEncodeFunc,
"length": stdlib.LengthFunc,
"lower": stdlib.LowerFunc,
"max": stdlib.MaxFunc,
"min": stdlib.MinFunc,
"reverse": stdlib.ReverseFunc,
"strlen": stdlib.StrlenFunc,
"substr": stdlib.SubstrFunc,
"upper": stdlib.UpperFunc,
}

type jsonObj map[string]interface{}

// Convert an hcl File to a json serializable object
Expand Down Expand Up @@ -62,7 +83,7 @@ func (c *converter) convertBlock(block *hclsyntax.Block, out jsonObj) error {
out, ok = inner.(jsonObj)
if !ok {
// TODO: better diagnostics
return fmt.Errorf("Unable to conver Block to JSON: %v.%v", block.Type, strings.Join(block.Labels, "."))
return fmt.Errorf("Unable to convert Block to JSON: %v.%v", block.Type, strings.Join(block.Labels, "."))
}
} else {
obj := make(jsonObj)
Expand Down Expand Up @@ -90,10 +111,15 @@ func (c *converter) convertExpression(expr hclsyntax.Expression) (interface{}, e
switch value := expr.(type) {
case *hclsyntax.LiteralValueExpr:
return ctyjson.SimpleJSONValue{Value: value.Val}, nil
case *hclsyntax.UnaryOpExpr:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This functionality is already on master.

num := c.rangeSource(expr.Range())
return strconv.Atoi(num)
case *hclsyntax.TemplateExpr:
return c.convertTemplate(value)
case *hclsyntax.TemplateWrapExpr:
return c.convertExpression(value.Wrapped)
case *hclsyntax.FunctionCallExpr:
return c.convertFunctionCall(expr)
case *hclsyntax.TupleConsExpr:
var list []interface{}
for _, ex := range value.Exprs {
Expand Down Expand Up @@ -219,3 +245,14 @@ func (c *converter) convertTemplateFor(expr *hclsyntax.ForExpr) (string, error)
func (c *converter) wrapExpr(expr hclsyntax.Expression) string {
return "${" + c.rangeSource(expr.Range()) + "}"
}

func (c *converter) convertFunctionCall(expr hclsyntax.Expression) (interface{}, error) {
specCtx := &hcl.EvalContext{
Functions: specFuncs,
}
value, valueDiags := expr.Value(specCtx)
if valueDiags.HasErrors() {
return nil, fmt.Errorf(valueDiags.Error())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should fall back to the existing behavior of wrapping the expression.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's not that important.

}
return value.AsString(), nil
}