Skip to content

Commit 547f1b6

Browse files
authored
fix: terraform-plugin-sdk zeros *int fields (#123)
1 parent 2f64d4c commit 547f1b6

File tree

4 files changed

+311
-109
lines changed

4 files changed

+311
-109
lines changed

Diff for: docs/data-sources/parameter.md

+5
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,9 @@ Optional:
6363
- `monotonic` (String) Number monotonicity, either increasing or decreasing.
6464
- `regex` (String) A regex for the input parameter to match against.
6565

66+
Read-Only:
67+
68+
- `max_disabled` (Boolean) Helper field to check if max is present
69+
- `min_disabled` (Boolean) Helper field to check if min is present
70+
6671

Diff for: provider/decode_test.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package provider_test
33
import (
44
"testing"
55

6-
"github.com/coder/terraform-provider-coder/provider"
76
"github.com/mitchellh/mapstructure"
87
"github.com/stretchr/testify/assert"
98
"github.com/stretchr/testify/require"
9+
10+
"github.com/coder/terraform-provider-coder/provider"
1011
)
1112

1213
func TestDecode(t *testing.T) {
@@ -23,11 +24,12 @@ func TestDecode(t *testing.T) {
2324
"display_name": displayName,
2425
"legacy_variable": legacyVariable,
2526
"legacy_variable_name": legacyVariableName,
26-
"min": nil,
2727
"validation": []map[string]interface{}{
2828
{
29-
"min": nil,
30-
"max": 5,
29+
"min": nil,
30+
"min_disabled": false,
31+
"max": 5,
32+
"max_disabled": true,
3133
},
3234
},
3335
}
@@ -38,6 +40,8 @@ func TestDecode(t *testing.T) {
3840
assert.Equal(t, displayName, param.DisplayName)
3941
assert.Equal(t, legacyVariable, param.LegacyVariable)
4042
assert.Equal(t, legacyVariableName, param.LegacyVariableName)
41-
assert.Equal(t, (*int)(nil), param.Validation[0].Min)
42-
assert.Equal(t, 5, *param.Validation[0].Max)
43+
assert.Equal(t, 5, param.Validation[0].Max)
44+
assert.True(t, param.Validation[0].MaxDisabled)
45+
assert.Equal(t, 0, param.Validation[0].Min)
46+
assert.False(t, param.Validation[0].MinDisabled)
4347
}

Diff for: provider/parameter.go

+23-15
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ type Option struct {
2828
}
2929

3030
type Validation struct {
31-
Min *int
32-
Max *int
31+
Min int
32+
MinDisabled bool `mapstructure:"min_disabled"`
33+
Max int
34+
MaxDisabled bool `mapstructure:"max_disabled"`
35+
3336
Monotonic string
3437

3538
Regex string
@@ -288,11 +291,21 @@ func parameterDataSource() *schema.Resource {
288291
Optional: true,
289292
Description: "The minimum of a number parameter.",
290293
},
294+
"min_disabled": {
295+
Type: schema.TypeBool,
296+
Computed: true,
297+
Description: "Helper field to check if min is present",
298+
},
291299
"max": {
292300
Type: schema.TypeInt,
293301
Optional: true,
294302
Description: "The maximum of a number parameter.",
295303
},
304+
"max_disabled": {
305+
Type: schema.TypeBool,
306+
Computed: true,
307+
Description: "Helper field to check if max is present",
308+
},
296309
"monotonic": {
297310
Type: schema.TypeString,
298311
Optional: true,
@@ -363,13 +376,8 @@ func fixValidationResourceData(rawConfig cty.Value, validation interface{}) (int
363376
return nil, xerrors.New("validation rule should be a map")
364377
}
365378

366-
// Fix the resource data
367-
if rawValidationRule["min"].IsNull() {
368-
validationRule["min"] = nil
369-
}
370-
if rawValidationRule["max"].IsNull() {
371-
validationRule["max"] = nil
372-
}
379+
validationRule["min_disabled"] = rawValidationRule["min"].IsNull()
380+
validationRule["max_disabled"] = rawValidationRule["max"].IsNull()
373381
return vArr, nil
374382
}
375383

@@ -401,10 +409,10 @@ func valueIsType(typ, value string) diag.Diagnostics {
401409

402410
func (v *Validation) Valid(typ, value string) error {
403411
if typ != "number" {
404-
if v.Min != nil {
412+
if !v.MinDisabled {
405413
return fmt.Errorf("a min cannot be specified for a %s type", typ)
406414
}
407-
if v.Max != nil {
415+
if !v.MaxDisabled {
408416
return fmt.Errorf("a max cannot be specified for a %s type", typ)
409417
}
410418
}
@@ -437,11 +445,11 @@ func (v *Validation) Valid(typ, value string) error {
437445
if err != nil {
438446
return fmt.Errorf("value %q is not a number", value)
439447
}
440-
if v.Min != nil && num < *v.Min {
441-
return fmt.Errorf("value %d is less than the minimum %d", num, *v.Min)
448+
if !v.MinDisabled && num < v.Min {
449+
return fmt.Errorf("value %d is less than the minimum %d", num, v.Min)
442450
}
443-
if v.Max != nil && num > *v.Max {
444-
return fmt.Errorf("value %d is more than the maximum %d", num, *v.Max)
451+
if !v.MaxDisabled && num > v.Max {
452+
return fmt.Errorf("value %d is more than the maximum %d", num, v.Max)
445453
}
446454
if v.Monotonic != "" && v.Monotonic != ValidationMonotonicIncreasing && v.Monotonic != ValidationMonotonicDecreasing {
447455
return fmt.Errorf("number monotonicity can be either %q or %q", ValidationMonotonicIncreasing, ValidationMonotonicDecreasing)

0 commit comments

Comments
 (0)