Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit547f1b6

Browse files
authored
fix: terraform-plugin-sdk zeros *int fields (#123)
1 parent2f64d4c commit547f1b6

File tree

4 files changed

+311
-109
lines changed

4 files changed

+311
-109
lines changed

‎docs/data-sources/parameter.md

Lines changed: 5 additions & 0 deletions
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

‎provider/decode_test.go

Lines changed: 10 additions & 6 deletions
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
funcTestDecode(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
}

‎provider/parameter.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ type Option struct {
2828
}
2929

3030
typeValidationstruct {
31-
Min*int
32-
Max*int
31+
Minint
32+
MinDisabledbool`mapstructure:"min_disabled"`
33+
Maxint
34+
MaxDisabledbool`mapstructure:"max_disabled"`
35+
3336
Monotonicstring
3437

3538
Regexstring
@@ -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
returnnil,xerrors.New("validation rule should be a map")
364377
}
365378

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

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

402410
func (v*Validation)Valid(typ,valuestring)error {
403411
iftyp!="number" {
404-
ifv.Min!=nil {
412+
if!v.MinDisabled {
405413
returnfmt.Errorf("a min cannot be specified for a %s type",typ)
406414
}
407-
ifv.Max!=nil {
415+
if!v.MaxDisabled {
408416
returnfmt.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
iferr!=nil {
438446
returnfmt.Errorf("value %q is not a number",value)
439447
}
440-
ifv.Min!=nil&&num<*v.Min {
441-
returnfmt.Errorf("value %d is less than the minimum %d",num,*v.Min)
448+
if!v.MinDisabled&&num<v.Min {
449+
returnfmt.Errorf("value %d is less than the minimum %d",num,v.Min)
442450
}
443-
ifv.Max!=nil&&num>*v.Max {
444-
returnfmt.Errorf("value %d is more than the maximum %d",num,*v.Max)
451+
if!v.MaxDisabled&&num>v.Max {
452+
returnfmt.Errorf("value %d is more than the maximum %d",num,v.Max)
445453
}
446454
ifv.Monotonic!=""&&v.Monotonic!=ValidationMonotonicIncreasing&&v.Monotonic!=ValidationMonotonicDecreasing {
447455
returnfmt.Errorf("number monotonicity can be either %q or %q",ValidationMonotonicIncreasing,ValidationMonotonicDecreasing)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp