|
8 | 8 |
|
9 | 9 | hcl "github.com/hashicorp/hcl/v2"
|
10 | 10 | "github.com/zclconf/go-cty/cty"
|
11 |
| - "github.com/zclconf/go-cty/cty/convert" |
12 | 11 | "github.com/zclconf/go-cty/cty/gocty"
|
13 | 12 | )
|
14 | 13 |
|
@@ -46,43 +45,19 @@ func (c *Client) WalkResourceAttributes(resource, attributeName string, walker f
|
46 | 45 | return nil
|
47 | 46 | }
|
48 | 47 |
|
| 48 | +// EvalExprRequest is the interface used to communicate via RPC. |
| 49 | +type EvalExprRequest struct { |
| 50 | + Expr hcl.Expression |
| 51 | + Ret interface{} |
| 52 | +} |
| 53 | + |
49 | 54 | // EvaluateExpr queries the host process for the result of evaluating the value of the passed expression
|
50 | 55 | // and reflects it as the value of the second argument based on that.
|
51 | 56 | func (c *Client) EvaluateExpr(expr hcl.Expression, ret interface{}) error {
|
52 | 57 | var val cty.Value
|
53 | 58 | var err error
|
54 | 59 |
|
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 { |
86 | 61 | return err
|
87 | 62 | }
|
88 | 63 |
|
|
0 commit comments