Skip to content

Commit 0f9ac8d

Browse files
committed
Convert cty.Value in server side
1 parent c75504c commit 0f9ac8d

File tree

2 files changed

+8
-33
lines changed

2 files changed

+8
-33
lines changed

tflint/client.go

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
hcl "github.com/hashicorp/hcl/v2"
1010
"github.com/zclconf/go-cty/cty"
11-
"github.com/zclconf/go-cty/cty/convert"
1211
"github.com/zclconf/go-cty/cty/gocty"
1312
)
1413

@@ -46,43 +45,19 @@ func (c *Client) WalkResourceAttributes(resource, attributeName string, walker f
4645
return nil
4746
}
4847

48+
// EvalExprRequest is the interface used to communicate via RPC.
49+
type EvalExprRequest struct {
50+
Expr hcl.Expression
51+
Ret interface{}
52+
}
53+
4954
// EvaluateExpr queries the host process for the result of evaluating the value of the passed expression
5055
// and reflects it as the value of the second argument based on that.
5156
func (c *Client) EvaluateExpr(expr hcl.Expression, ret interface{}) error {
5257
var val cty.Value
5358
var err error
5459

55-
if err := c.rpcClient.Call("Plugin.EvalExpr", &expr, &val); err != nil {
56-
return err
57-
}
58-
59-
switch ret.(type) {
60-
case *string:
61-
val, err = convert.Convert(val, cty.String)
62-
case *int:
63-
val, err = convert.Convert(val, cty.Number)
64-
case *[]string:
65-
val, err = convert.Convert(val, cty.List(cty.String))
66-
case *[]int:
67-
val, err = convert.Convert(val, cty.List(cty.Number))
68-
case *map[string]string:
69-
val, err = convert.Convert(val, cty.Map(cty.String))
70-
case *map[string]int:
71-
val, err = convert.Convert(val, cty.Map(cty.Number))
72-
}
73-
74-
if err != nil {
75-
err := &Error{
76-
Code: TypeConversionError,
77-
Level: ErrorLevel,
78-
Message: fmt.Sprintf(
79-
"Invalid type expression in %s:%d",
80-
expr.Range().Filename,
81-
expr.Range().Start.Line,
82-
),
83-
Cause: err,
84-
}
85-
log.Printf("[ERROR] %s", err)
60+
if err := c.rpcClient.Call("Plugin.EvalExpr", EvalExprRequest{Expr: expr, Ret: ret}, &val); err != nil {
8661
return err
8762
}
8863

tflint/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ type Rule interface {
2525
// Server is the interface that hosts that provide the plugin mechanism must meet in order to respond to queries from the plugin.
2626
type Server interface {
2727
Attributes(*AttributesRequest, *hcl.Attributes) error
28-
EvalExpr(hcl.Expression, *cty.Value) error
28+
EvalExpr(*EvalExprRequest, *cty.Value) error
2929
EmitIssue(*EmitIssueRequest, *interface{}) error
3030
}

0 commit comments

Comments
 (0)