Skip to content

Commit 06d5c0a

Browse files
authored
Use msgpack to encoding to pass cty.Value in variable default (#96)
* Use msgpack to encoding to pass cty.Value in variable default * Better diagnostic output in failed unmarshal
1 parent 1d08094 commit 06d5c0a

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

plugin/plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
// handShakeConfig is used for UX. ProcotolVersion will be updated by incompatible changes.
1212
var handshakeConfig = plugin.HandshakeConfig{
13-
ProtocolVersion: 7,
13+
ProtocolVersion: 8,
1414
MagicCookieKey: "TFLINT_RULESET_PLUGIN",
1515
MagicCookieValue: "5adSn1bX8nrDfgBqiAqqEkC6OE1h3iD8SqbMc5UUONx8x3xCF0KlPDsBRNDjoYDP",
1616
}

tflint/client/decode_named_values.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package client
22

33
import (
4+
"fmt"
5+
46
hcl "github.com/hashicorp/hcl/v2"
57
"github.com/terraform-linters/tflint-plugin-sdk/terraform/configs"
68
"github.com/zclconf/go-cty/cty"
9+
"github.com/zclconf/go-cty/cty/msgpack"
710
)
811

912
// Variable is an intermediate representation of configs.Variable.
1013
type Variable struct {
1114
Name string
1215
Description string
13-
Default cty.Value
16+
Default []byte
1417
Type cty.Type
1518
ParsingMode configs.VariableParsingMode
1619
Validations []*VariableValidation
@@ -32,10 +35,22 @@ func decodeVariable(variable *Variable) (*configs.Variable, hcl.Diagnostics) {
3235
ret[i] = validation
3336
}
3437

38+
defaultVal, err := msgpack.Unmarshal(variable.Default, variable.Type)
39+
if err != nil {
40+
return nil, hcl.Diagnostics{
41+
&hcl.Diagnostic{
42+
Severity: hcl.DiagError,
43+
Summary: "cannot unmarshal variable default value",
44+
Detail: fmt.Sprint(err),
45+
Subject: &variable.DeclRange,
46+
},
47+
}
48+
}
49+
3550
return &configs.Variable{
3651
Name: variable.Name,
3752
Description: variable.Description,
38-
Default: variable.Default,
53+
Default: defaultVal,
3954
Type: variable.Type,
4055
ParsingMode: variable.ParsingMode,
4156
Validations: ret,

0 commit comments

Comments
 (0)