Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
num/bool support on protos
Browse files Browse the repository at this point in the history
  • Loading branch information
krantzinator committed Jun 25, 2021
1 parent 008852f commit 58333d6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
5 changes: 5 additions & 0 deletions internal/config/variables/testdata/valid.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ variable "mug" {
variable "is_good" {
default = false
type = bool
}

variable "whatdoesittaketobenumber" {
default = 1
type = number
}
15 changes: 10 additions & 5 deletions internal/config/variables/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,19 @@ func (iv *InputVars) CollectInputValues(wd string, pbvars []*pb.Variable) hcl.Di
var expr hclsyntax.Expression
switch pbv.Value.(type) {

case *pb.Variable_Hcl:
value := pbv.Value.(*pb.Variable_Hcl).Hcl
fakeFilename := fmt.Sprintf("<value for var.%s from server>", pbv.Name)
expr, diags = hclsyntax.ParseExpression([]byte(value), fakeFilename, hcl.Pos{Line: 1, Column: 1})

// case *pb.Variable_Hcl:
// value := pbv.Value.(*pb.Variable_Hcl).Hcl
// fakeFilename := fmt.Sprintf("<value for var.%s from server>", pbv.Name)
// expr, diags = hclsyntax.ParseExpression([]byte(value), fakeFilename, hcl.Pos{Line: 1, Column: 1})
case *pb.Variable_Str:
value := pbv.Value.(*pb.Variable_Str).Str
expr = &hclsyntax.LiteralValueExpr{Val: cty.StringVal(value)}
case *pb.Variable_Bool:
value := pbv.Value.(*pb.Variable_Bool).Bool
expr = &hclsyntax.LiteralValueExpr{Val: cty.BoolVal(value)}
case *pb.Variable_Num:
value := pbv.Value.(*pb.Variable_Num).Num
expr = &hclsyntax.LiteralValueExpr{Val: cty.NumberIntVal(value)}
}

val, valDiags := expr.Value(nil)
Expand Down
41 changes: 41 additions & 0 deletions internal/config/variables/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,38 @@ func TestVariables_collectValues(t *testing.T) {
},
Type: cty.String,
},
"is_good": &InputVar{
Name: "is_good",
Values: []Value{
{cty.BoolVal(false), Source{"default", 0}, hcl.Expression(nil), hcl.Range{}},
},
Type: cty.Bool,
},
"whatdoesittaketobenumber": &InputVar{
Name: "whatdoesittaketobenumber",
Values: []Value{
{cty.NumberIntVal(1), Source{"default", 0}, hcl.Expression(nil), hcl.Range{}},
},
Type: cty.Number,
},
},
err: "",
},
// {
// name: "complex types",
// file: "collections.hcl",
// inputValues: []*pb.Variable{},
// expected: InputVars{
// "docker_ports": &InputVar{
// Name: "docker_ports",
// Values: []Value{
// {stringListVal("us-east-1"), Source{"default", 0}, hcl.Expression(nil), hcl.Range{}},
// },
// Type: cty.List(cty.String),
// },
// },
// err: "",
// },
{
name: "undefined variable for pb.Variable value",
file: "valid.hcl",
Expand Down Expand Up @@ -317,3 +346,15 @@ type testConfig struct {
Variables []*HclVariable `hcl:"variable,block"`
Body hcl.Body `hcl:",body"`
}

// func stringListVal(strings ...string) cty.Value {
// values := []cty.Value{}
// for _, str := range strings {
// values = append(values, cty.StringVal(str))
// }
// list, err := convert.Convert(cty.ListVal(values), cty.List(cty.String))
// if err != nil {
// panic(err)
// }
// return list
// }
3 changes: 2 additions & 1 deletion internal/server/proto/server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,10 @@ message Variable {

oneof value {
string str = 2;
string hcl = 3;
bool bool = 9;
int64 num = 10;
// Unimplemented
string hcl = 3;
}

// Source tracks where a value came from for more helpful error messaging.
Expand Down

0 comments on commit 58333d6

Please sign in to comment.