Skip to content

Commit 44b3cdf

Browse files
committed
fixup parameter values to be lazy eval'd
1 parent c49ec0d commit 44b3cdf

File tree

13 files changed

+64
-88
lines changed

13 files changed

+64
-88
lines changed

cli/clidisplay/resources.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func Parameters(writer io.Writer, params []types.Parameter) {
5757
row := table.Row{"Parameter"}
5858
tableWriter.AppendHeader(row)
5959
for _, p := range params {
60-
strVal := p.Value.Value
60+
strVal := p.Value.AsString()
6161
//value := p.Value.Value
6262
//
6363
//if value.IsNull() {
@@ -71,7 +71,7 @@ func Parameters(writer io.Writer, params []types.Parameter) {
7171
//}
7272

7373
tableWriter.AppendRow(table.Row{
74-
fmt.Sprintf("%s (%s): %s\n%s", p.Name, p.BlockName, p.Description, formatOptions(strVal, p.Options)),
74+
fmt.Sprintf("%s: %s\n%s", p.Name, p.Description, formatOptions(strVal, p.Options)),
7575
})
7676
tableWriter.AppendSeparator()
7777
}

cli/root.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/coder/preview"
1212
"github.com/coder/preview/cli/clidisplay"
13-
"github.com/coder/preview/types"
1413
"github.com/coder/serpent"
1514
)
1615

@@ -56,15 +55,13 @@ func (r *RootCmd) Root() *serpent.Command {
5655
Handler: func(i *serpent.Invocation) error {
5756
dfs := os.DirFS(dir)
5857

59-
rvars := make(map[string]types.ParameterValue)
58+
rvars := make(map[string]string)
6059
for _, val := range vars {
6160
parts := strings.Split(val, "=")
6261
if len(parts) != 2 {
6362
continue
6463
}
65-
rvars[parts[0]] = types.ParameterValue{
66-
Value: parts[1],
67-
}
64+
rvars[parts[0]] = parts[1]
6865
}
6966

7067
input := preview.Input{

extract/parameter.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
"github.com/coder/preview/types"
1313
)
1414

15-
func ParameterFromBlock(block *terraform.Block) (types.Parameter, hcl.Diagnostics) {
15+
func ParameterFromBlock(block *terraform.Block) (*types.Parameter, hcl.Diagnostics) {
1616
diags := required(block, "name", "type")
1717
if diags.HasErrors() {
18-
return types.Parameter{}, diags
18+
return nil, diags
1919
}
2020

2121
pType, typDiag := requiredString("type", block)
@@ -28,17 +28,13 @@ func ParameterFromBlock(block *terraform.Block) (types.Parameter, hcl.Diagnostic
2828
diags = diags.Append(nameDiag)
2929
}
3030

31-
pVal, valDiags := richParameterValue(block)
32-
diags = diags.Extend(valDiags)
33-
3431
if diags.HasErrors() {
35-
return types.Parameter{}, diags
32+
return nil, diags
3633
}
3734

35+
pVal := richParameterValue(block)
3836
p := types.Parameter{
39-
Value: types.ParameterValue{
40-
Value: pVal,
41-
},
37+
Value: pVal,
4238
RichParameter: types.RichParameter{
4339
Name: pName,
4440
Description: optionalString(block, "description"),
@@ -54,7 +50,6 @@ func ParameterFromBlock(block *terraform.Block) (types.Parameter, hcl.Diagnostic
5450
DisplayName: optionalString(block, "display_name"),
5551
Order: optionalInteger(block, "order"),
5652
Ephemeral: optionalBoolean(block, "ephemeral"),
57-
BlockName: block.NameLabel(),
5853
},
5954
}
6055

@@ -80,7 +75,10 @@ func ParameterFromBlock(block *terraform.Block) (types.Parameter, hcl.Diagnostic
8075
p.Validations = append(p.Validations, &valid)
8176
}
8277

83-
return p, diags
78+
// Diagnostics are scoped to the parameter
79+
p.Diagnostics = diags
80+
81+
return &p, nil
8482
}
8583

8684
func ParameterValidationFromBlock(block *terraform.Block) (types.ParameterValidation, hcl.Diagnostics) {
@@ -243,25 +241,19 @@ func required(block *terraform.Block, keys ...string) hcl.Diagnostics {
243241
return diags
244242
}
245243

246-
func richParameterValue(block *terraform.Block) (string, hcl.Diagnostics) {
244+
func richParameterValue(block *terraform.Block) types.HCLString {
247245
// Find the value of the parameter from the context.
248246
paramPath := append([]string{"data"}, block.Labels()...)
247+
path := strings.Join(paramPath, ".")
248+
249249
valueRef := hclext.ScopeTraversalExpr(append(paramPath, "value")...)
250250
val, diags := valueRef.Value(block.Context().Inner())
251-
if diags.HasErrors() {
252-
return "", diags
253-
}
254-
255-
if !val.Type().Equals(cty.String) {
256-
return "", hcl.Diagnostics{
257-
{
258-
Severity: hcl.DiagError,
259-
Summary: "Invalid parameter value",
260-
Detail: fmt.Sprintf("Expected a string, got %q", val.Type().FriendlyName()),
261-
},
262-
}
251+
return types.HCLString{
252+
Value: val,
253+
ValueDiags: diags,
254+
ValueExpr: &valueRef,
255+
Source: &path,
263256
}
264-
return val.AsString(), nil
265257
}
266258

267259
func ParameterCtyType(typ string) (cty.Type, error) {

extract/state.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ func ParameterFromState(block *tfjson.StateResource) (types.Parameter, error) {
5252
}
5353

5454
param := types.Parameter{
55-
Value: types.ParameterValue{
56-
Value: st.string("value"),
57-
},
55+
Value: types.StringLiteral(st.string("value")),
5856
RichParameter: types.RichParameter{
5957
Name: st.string("name"),
6058
Description: st.optionalString("description"),
@@ -68,7 +66,6 @@ func ParameterFromState(block *tfjson.StateResource) (types.Parameter, error) {
6866
DisplayName: st.optionalString("display_name"),
6967
Order: st.optionalInteger("order"),
7068
Ephemeral: st.optionalBool("ephemeral"),
71-
BlockName: block.Name,
7269
},
7370
}
7471

internal/verify/cmp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func CompareParameters(t *testing.T, pr *preview.Output, values *tfjson.StateMod
3535
types.SortParameters(pr.Parameters)
3636
for i, param := range stateParams {
3737
// TODO: A better compare function would be easier to debug
38-
assert.Equal(t, param, pr.Parameters[i], "parameter %q %d", param.BlockName, i)
38+
assert.Equal(t, param, pr.Parameters[i], "parameter %q %d", param.Name, i)
3939
}
4040

4141
return passed

parameter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ func RichParameters(modules terraform.Modules) ([]types.Parameter, hcl.Diagnosti
2020
diags = diags.Extend(pDiags)
2121
}
2222

23-
if !pDiags.HasErrors() {
24-
params = append(params, param)
23+
if param != nil {
24+
params = append(params, *param)
2525
}
2626
}
2727
}

paramhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func ParameterContextsEvalHook(input Input) func(ctx *tfcontext.Context, blocks
3030
pv, ok := input.RichParameterValue(name)
3131
if ok {
3232
// TODO: Handle non-string types
33-
value = cty.StringVal(pv.Value)
33+
value = cty.StringVal(pv)
3434
} else {
3535
// get the default value
3636
// TODO: Log any diags

preview.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
type Input struct {
1616
PlanJSONPath string
17-
ParameterValues map[string]types.ParameterValue
17+
ParameterValues map[string]string
1818
}
1919

2020
type Output struct {
@@ -88,7 +88,7 @@ func Preview(ctx context.Context, input Input, dir fs.FS) (*Output, hcl.Diagnost
8888
}, diags.Extend(rpDiags).Extend(tagDiags)
8989
}
9090

91-
func (i Input) RichParameterValue(key string) (types.ParameterValue, bool) {
91+
func (i Input) RichParameterValue(key string) (string, bool) {
9292
p, ok := i.ParameterValues[key]
9393
return p, ok
9494
}

preview_test.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ func Test_Extract(t *testing.T) {
6565
name: "conditional",
6666
dir: "conditional",
6767
input: preview.Input{
68-
ParameterValues: map[string]types.ParameterValue{
69-
"project": {
70-
Value: "small",
71-
},
68+
ParameterValues: map[string]string{
69+
"project": "small",
7270
},
7371
},
7472
expTags: map[string]string{},
@@ -85,10 +83,8 @@ func Test_Extract(t *testing.T) {
8583
"zone": "eu",
8684
},
8785
input: preview.Input{
88-
ParameterValues: map[string]types.ParameterValue{
89-
"region": {
90-
Value: "eu",
91-
},
86+
ParameterValues: map[string]string{
87+
"region": "eu",
9288
},
9389
},
9490
expUnknowns: []string{},
@@ -103,10 +99,8 @@ func Test_Extract(t *testing.T) {
10399
"zone": "eu",
104100
},
105101
input: preview.Input{
106-
ParameterValues: map[string]types.ParameterValue{
107-
"region": {
108-
Value: "eu",
109-
},
102+
ParameterValues: map[string]string{
103+
"region": "eu",
110104
},
111105
},
112106
expUnknowns: []string{},
@@ -158,7 +152,7 @@ func Test_Extract(t *testing.T) {
158152
expTags: map[string]string{},
159153
input: preview.Input{
160154
PlanJSONPath: "before.json",
161-
ParameterValues: map[string]types.ParameterValue{},
155+
ParameterValues: map[string]string{},
162156
},
163157
expUnknowns: []string{},
164158
params: map[string]func(t *testing.T, parameter types.Parameter){},
@@ -168,7 +162,7 @@ func Test_Extract(t *testing.T) {
168162
dir: "nulldefault",
169163
expTags: map[string]string{},
170164
input: preview.Input{
171-
ParameterValues: map[string]types.ParameterValue{},
165+
ParameterValues: map[string]string{},
172166
},
173167
expUnknowns: []string{},
174168
params: map[string]func(t *testing.T, parameter types.Parameter){},
@@ -178,7 +172,7 @@ func Test_Extract(t *testing.T) {
178172
dir: "manymodules",
179173
expTags: map[string]string{},
180174
input: preview.Input{
181-
ParameterValues: map[string]types.ParameterValue{},
175+
ParameterValues: map[string]string{},
182176
PlanJSONPath: "out.json",
183177
},
184178
expUnknowns: []string{},
@@ -189,7 +183,7 @@ func Test_Extract(t *testing.T) {
189183
dir: "dupemodparams",
190184
expTags: map[string]string{},
191185
input: preview.Input{
192-
ParameterValues: map[string]types.ParameterValue{},
186+
ParameterValues: map[string]string{},
193187
},
194188
expUnknowns: []string{},
195189
params: map[string]func(t *testing.T, parameter types.Parameter){},

previewe2e_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"github.com/coder/preview"
1616
"github.com/coder/preview/internal/verify"
17-
"github.com/coder/preview/types"
1817
)
1918

2019
// Test_VerifyE2E will fully evaluate with `terraform apply`
@@ -137,7 +136,7 @@ func Test_VerifyE2E(t *testing.T) {
137136
output, diags := preview.Preview(context.Background(),
138137
preview.Input{
139138
PlanJSONPath: "plan.json",
140-
ParameterValues: map[string]types.ParameterValue{},
139+
ParameterValues: map[string]string{},
141140
},
142141
os.DirFS(wp))
143142
if diags.HasErrors() {

0 commit comments

Comments
 (0)