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

fix: terraform-plugin-sdk zeros *int fields#123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mtojek merged 14 commits intocoder:mainfrommtojek:bug-validation-min
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletionsdocs/data-sources/parameter.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,4 +63,9 @@ Optional:
- `monotonic` (String) Number monotonicity, either increasing or decreasing.
- `regex` (String) A regex for the input parameter to match against.

Read-Only:

- `max_disabled` (Boolean) Helper field to check if max is present
- `min_disabled` (Boolean) Helper field to check if min is present


16 changes: 10 additions & 6 deletionsprovider/decode_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,10 +3,11 @@ package provider_test
import (
"testing"

"github.com/coder/terraform-provider-coder/provider"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/coder/terraform-provider-coder/provider"
)

func TestDecode(t *testing.T) {
Expand All@@ -23,11 +24,12 @@ func TestDecode(t *testing.T) {
"display_name": displayName,
"legacy_variable": legacyVariable,
"legacy_variable_name": legacyVariableName,
"min": nil,
"validation": []map[string]interface{}{
{
"min": nil,
"max": 5,
"min": nil,
"min_disabled": false,
"max": 5,
"max_disabled": true,
},
},
}
Expand All@@ -38,6 +40,8 @@ func TestDecode(t *testing.T) {
assert.Equal(t, displayName, param.DisplayName)
assert.Equal(t, legacyVariable, param.LegacyVariable)
assert.Equal(t, legacyVariableName, param.LegacyVariableName)
assert.Equal(t, (*int)(nil), param.Validation[0].Min)
assert.Equal(t, 5, *param.Validation[0].Max)
assert.Equal(t, 5, param.Validation[0].Max)
assert.True(t, param.Validation[0].MaxDisabled)
assert.Equal(t, 0, param.Validation[0].Min)
assert.False(t, param.Validation[0].MinDisabled)
}
38 changes: 23 additions & 15 deletionsprovider/parameter.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,8 +28,11 @@ type Option struct {
}

type Validation struct {
Min *int
Max *int
Min int
MinDisabled bool `mapstructure:"min_disabled"`
Max int
MaxDisabled bool `mapstructure:"max_disabled"`

Monotonic string

Regex string
Expand DownExpand Up@@ -288,11 +291,21 @@ func parameterDataSource() *schema.Resource {
Optional: true,
Description: "The minimum of a number parameter.",
},
"min_disabled": {
Type: schema.TypeBool,
Computed: true,
Description: "Helper field to check if min is present",
},
"max": {
Type: schema.TypeInt,
Optional: true,
Description: "The maximum of a number parameter.",
},
"max_disabled": {
Type: schema.TypeBool,
Computed: true,
Description: "Helper field to check if max is present",
},
"monotonic": {
Type: schema.TypeString,
Optional: true,
Expand DownExpand Up@@ -363,13 +376,8 @@ func fixValidationResourceData(rawConfig cty.Value, validation interface{}) (int
return nil, xerrors.New("validation rule should be a map")
}

// Fix the resource data
if rawValidationRule["min"].IsNull() {
validationRule["min"] = nil
}
if rawValidationRule["max"].IsNull() {
validationRule["max"] = nil
}
validationRule["min_disabled"] = rawValidationRule["min"].IsNull()
validationRule["max_disabled"] = rawValidationRule["max"].IsNull()
return vArr, nil
}

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

func (v *Validation) Valid(typ, value string) error {
if typ != "number" {
ifv.Min != nil {
if!v.MinDisabled {
return fmt.Errorf("a min cannot be specified for a %s type", typ)
}
ifv.Max != nil {
if!v.MaxDisabled {
return fmt.Errorf("a max cannot be specified for a %s type", typ)
}
}
Expand DownExpand Up@@ -437,11 +445,11 @@ func (v *Validation) Valid(typ, value string) error {
if err != nil {
return fmt.Errorf("value %q is not a number", value)
}
ifv.Min != nil&& num <*v.Min {
return fmt.Errorf("value %d is less than the minimum %d", num,*v.Min)
if!v.MinDisabled&& num < v.Min {
return fmt.Errorf("value %d is less than the minimum %d", num, v.Min)
}
ifv.Max != nil&& num >*v.Max {
return fmt.Errorf("value %d is more than the maximum %d", num,*v.Max)
if!v.MaxDisabled&& num > v.Max {
return fmt.Errorf("value %d is more than the maximum %d", num, v.Max)
}
if v.Monotonic != "" && v.Monotonic != ValidationMonotonicIncreasing && v.Monotonic != ValidationMonotonicDecreasing {
return fmt.Errorf("number monotonicity can be either %q or %q", ValidationMonotonicIncreasing, ValidationMonotonicDecreasing)
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp