-
Notifications
You must be signed in to change notification settings - Fork 53
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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: | ||
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 { | ||
|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should fall back to the existing behavior of wrapping the expression. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it's not that important. |
||
} | ||
return value.AsString(), nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.