Skip to content

Commit f02c46f

Browse files
authored
V3 work (#39)
* beginning work on updating to work with v1 * closer * fixing few issues with comparison validators and adding set string tests * quick readme update and other small improvements
1 parent cd2424f commit f02c46f

File tree

13 files changed

+904
-421
lines changed

13 files changed

+904
-421
lines changed

README.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ changes as possible but, as always, community help is appreciated!
2121
|----------------------------|-----------------|
2222
| v0.7.0-v0.9.0 | v1 |
2323
| v0.10.x-v0.15.x | v2 |
24+
| v1.x | v3 |
2425

2526
# Installation
2627
```shell
27-
go get -u github.com/dcarbone/terraform-plugin-framework-utils/v2@latest
28+
go get -u github.com/dcarbone/terraform-plugin-framework-utils/v3@latest
2829
```
2930

3031
# Type Conversion
@@ -37,14 +38,14 @@ and from Terraform and Go easy and obvious.
3738
You can see the complete list of available conversions here:
3839
[terraform-plugin-framework-utils/conv](https://github.com/dcarbone/terraform-plugin-framework-utils/blob/main/conv)
3940

40-
# Attribute Validation
41+
# Generic Validation
4142

42-
The Terraform Plugin Framework has a great [validation](https://www.terraform.io/plugin/framework/validation) interface
43-
that makes it easy to create custom validation rules for the various schemas you define in your provider.
43+
The Terraform Plugin Framework has a great set of per-value type validator interfaces that you may implement as needed:
44+
[validators](https://www.terraform.io/plugin/framework/validation). This does not always fit the need, however,
45+
as some validations need not be aware of type, or may benefit from being applicable to multiple types.
4446

45-
Being in beta, the HashiCorp team has yet to provide a built-in set of providers. I have created a few that I have
46-
found useful when creating my own providers, and provided a small wrapper to make creating new providers as
47-
simple as [defining a function](validation/validators.go#25).
47+
To facilitate this, I have created a few that I have found useful when creating my own providers, and defined a
48+
small wrapper to make creating new validators as simple as [defining a function](validation/validators.go#19).
4849

4950
## Provided Validators
5051

@@ -54,7 +55,7 @@ Fails validation if the attribute is null or unknown
5455

5556
```go
5657
{
57-
Validators: []tfsdk.AttributeValidator{
58+
Validators: []validator.{Type}{
5859
validation.Required()
5960
},
6061
}
@@ -67,7 +68,7 @@ will attempt to convert the attribute to a string first.
6768

6869
```go
6970
{
70-
Validators: []tfsdk.AttributeValidator{
71+
Validators: []validator.{Type}{
7172
validation.RegexpMatch("{{ your regex here }}")
7273
},
7374
}
@@ -80,7 +81,7 @@ will attempt to convert the attribute to a string first.
8081

8182
```go
8283
{
83-
Validators: []tfsdk.AttributeValidator{
84+
Validators: []validator.{Type}{
8485
validation.RegexpNotMatch("{{ your regex here }}")
8586
},
8687
}
@@ -92,7 +93,7 @@ Fails validation if the attribute's value's length is not within the specified b
9293

9394
```go
9495
{
95-
Validators: []tfsdk.AttributeValidator{
96+
Validators: []validator.{Type}{
9697
// lower limit
9798
validation.Length(5, -1),
9899

@@ -114,7 +115,7 @@ your own [ComparisonFunc](validation/comparison.go#44) using [SetComparisonFunc]
114115

115116
```go
116117
{
117-
Validators: []tfsdk.AttributeValidator{
118+
Validators: []validator.{Type}{
118119
// equal
119120
validation.Compare(validation.Equal, 5),
120121
// string comparisons are case sensitive by default
@@ -174,7 +175,7 @@ Fails validation if the attribute's value is not parseable by `url.Parse`
174175

175176
```go
176177
{
177-
Validators: []tfsdk.AttributeValidator{
178+
Validators: []validator.{Type}{
178179
validation.IsUrl()
179180
}
180181
}
@@ -186,7 +187,7 @@ Fails validation if the attribute's value is not parseable by `time.ParseDuratio
186187

187188
```go
188189
{
189-
Validators: []tfsdk.AttributeValidator{
190+
Validators: []validator.{Type}{
190191
validation.IsDurationString()
191192
}
192193
}
@@ -198,7 +199,7 @@ Fails validation if the environment variable name defined by the attribute's val
198199

199200
```go
200201
{
201-
Validators: []tfsdk.AttributeValidator{
202+
Validators: []validator.{Type}{
202203
validation.EnvVarValued()
203204
}
204205
}
@@ -210,7 +211,7 @@ Fails validation if the file at the path defined in the attribute's value is not
210211

211212
```go
212213
{
213-
Validators: []tfsdk.AttributeValidator{
214+
Validators: []validator.{Type}{
214215
validation.FileIsReadable()
215216
}
216217
}
@@ -222,7 +223,7 @@ Fails validation if the attribute is valued and the configured sibling attribute
222223

223224
```go
224225
{
225-
Validators: []tfsdk.AttributeValidator{
226+
Validators: []validator.{Type}{
226227
validation.MutuallyExclusiveSibling("{{ sibling field name }}")
227228
}
228229
}
@@ -241,7 +242,7 @@ provider "whatever" {
241242
```go
242243
// Example validators list defined on the `address` attribute's schema
243244
{
244-
Validators: []tfsdk.AttributeValidator{
245+
Validators: []validator.{Type}{
245246
validation.MutuallyExclusiveSibling("address_env")
246247
}
247248
}
@@ -257,7 +258,7 @@ Requires that two sibling attributes either both be valued or not valued.
257258

258259
```go
259260
{
260-
Validators: []tfsdk.AttributeValidator{
261+
Validators: []validator.{Type}{
261262
validation.MutuallyInclusiveSibling("{{ sibling field name }}")
262263
}
263264
}
@@ -276,7 +277,7 @@ provider "whatever" {
276277
```go
277278
// Example validators list defined on the `ssh_key_password` attribute's schema
278279
{
279-
Validators: []tfsdk.AttributeValidator{
280+
Validators: []validator.{Type}{
280281
validation.MutuallyInclusiveSibling("ssh_key")
281282
}
282283
}

acctest/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"sync"
77
"time"
88

9-
"github.com/dcarbone/terraform-plugin-framework-utils/v2/internal/util"
9+
"github.com/dcarbone/terraform-plugin-framework-utils/v3/internal/util"
1010
)
1111

1212
type ConfigLiteral string

acctest/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"testing"
88
"time"
99

10-
"github.com/dcarbone/terraform-plugin-framework-utils/v2/acctest"
10+
"github.com/dcarbone/terraform-plugin-framework-utils/v3/acctest"
1111
)
1212

1313
func TestConfigValue_Defaults(t *testing.T) {

conv/attr.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package conv
22

33
import (
44
"fmt"
5+
"strconv"
56
"strings"
67

78
"github.com/hashicorp/terraform-plugin-framework/path"
@@ -38,9 +39,14 @@ func FormatAttributePathSteps(pathSteps ...tftypes.AttributePathStep) string {
3839
bits = append(bits, string(pathStep.(tftypes.AttributeName)))
3940
case tftypes.ElementKeyString:
4041
bits = append(bits, string(pathStep.(tftypes.ElementKeyString)))
42+
case tftypes.ElementKeyInt:
43+
bits = append(bits, strconv.FormatInt(int64(pathStep.(tftypes.ElementKeyInt)), 10))
44+
case tftypes.ElementKeyValue:
45+
bits = append(bits, (tftypes.Value)(pathStep.(tftypes.ElementKeyValue)).String())
4146

4247
default:
43-
panic(fmt.Sprintf("no case to convert type %T (%[1]v) to string", pathStep))
48+
// if this is reached, a new path step implementation has been created
49+
panic(fmt.Sprintf("no case to convert type %T (%[1]v) to string, please create issue with this error message", pathStep))
4450
}
4551
}
4652
return strings.Join(bits, ".")

conv/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"errors"
55
"fmt"
66

7-
"github.com/dcarbone/terraform-plugin-framework-utils/v2/internal/util"
7+
"github.com/dcarbone/terraform-plugin-framework-utils/v3/internal/util"
88
"github.com/hashicorp/terraform-plugin-framework/attr"
99
)
1010

0 commit comments

Comments
 (0)