Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 72743a5

Browse files
Merge pull request #1 from matthewhartstonge/feature/generify-type-and-value
feature/generify-type-and-value
2 parents a4888ef + b6049a8 commit 72743a5

File tree

6 files changed

+669
-117
lines changed

6 files changed

+669
-117
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ and DCE 1.1: Authentication and Security Services.
1010

1111
### Schema
1212

13-
Replace usages of `types.StringType` with `uuidtype.Type{}`.
13+
Replace usages of `types.StringType` with `uuidtype.UUIDType{}`.
1414

1515
Given the previous schema attribute:
1616

@@ -27,14 +27,14 @@ The updated schema will look like:
2727
```go
2828
tfsdk.Attribute{
2929
Required: true
30-
Type: uuidtype.Type{}
30+
Type: uuidtype.UUIDType{}
3131
}
3232
```
3333

3434
### Schema Data Model
3535

3636
Replace usage of `string`, `*string`, or `types.String` in schema data models
37-
with `uuidtype.Value`.
37+
with `uuidtype.UUID`.
3838

3939
Given the previous schema data model:
4040

@@ -50,7 +50,7 @@ The updated schema data model will look like:
5050
```go
5151
type ThingResourceModel struct {
5252
// ...
53-
Example uuidtype.Value `tfsdk:"example"`
53+
Example uuidtype.UUID `tfsdk:"example"`
5454
}
5555
```
5656

@@ -62,24 +62,24 @@ a known `uuid` value.
6262

6363
### Writing Values
6464

65-
Create a `uuidtype.Value` by calling one of these functions:
65+
Create a `uuidtype.UUID` by calling one of these functions:
6666

67-
- `NullValue() Value`: creates a `null` value.
68-
- `UnknownValue() Value`: creates an unknown value.
69-
- `StringValue(string, path.Path) (Value, diag.Diagnostics)`: creates a known
67+
- `NullUUID() UUID`: creates a `null` value.
68+
- `UnknownUUID() UUID`: creates an unknown value.
69+
- `UUIDFromString(string, path.Path) (UUID, diag.Diagnostics)`: creates a known
7070
value using the given `string` or returns validation errors if `string` is
7171
not in the expected UUID format.
72-
- `MustValue(string) Value` creates a known value using the given string, but
73-
will panic if it's unparseable as a UUID.
72+
- `UUIDFromGoogleUUID(uuid.UUID) UUID` creates a known value given a
73+
Google [uuid.UUID](https://pkg.go.dev/github.com/google/uuid#UUID) struct.
7474

7575
### Adding the Dependency
7676

77-
All functionality is located in the `github.com/matthewhartstonge/terraform-plugin-framework-type-uuid/uuidtype`
77+
All functionality is located in the `github.com/matthewhartstonge/terraform-plugin-framework-type-uuid/uuidtypes`
7878
package. Add this as an `import` as required to your relevant Go files.
7979

8080
Run the following Go commands to fetch the latest version and ensure all module files are up-to-date.
8181

8282
```shell
83-
go get github.com/matthewhartstonge/terraform-plugin-framework-type-uuidtype@latest
83+
go get github.com/matthewhartstonge/terraform-plugin-framework-type-uuid@latest
8484
go mod tidy
8585
```

uuidtype/doc.go renamed to uuidtypes/doc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
// Package uuidtype implements a terraform-plugin-framework attr.Type and
17+
// Package uuidtypes implements a terraform-plugin-framework attr.Type and
1818
// attr.Value for Universally Unique IDentifiers (UUIDs) as defined in
1919
// [RFC 4122].
2020
//
2121
// [RFC 4122]: https://tools.ietf.org/html/rfc4122
22-
package uuidtype
22+
package uuidtypes
Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package uuidtype
17+
package uuidtypes
1818

1919
import (
2020
// Standard Library Imports
@@ -30,25 +30,25 @@ import (
3030

3131
// Ensure Implementation matches the expected interfaces.
3232
var (
33-
_ attr.Value = Value{}
33+
_ attr.Value = UUID{}
3434
)
3535

36-
// NullValue returns a null UUID value.
37-
func NullValue() Value {
38-
return Value{null: true}
36+
// UUIDNull returns a null UUID value.
37+
func UUIDNull() UUID {
38+
return UUID{null: true}
3939
}
4040

41-
// UnknownValue returns an unknown UUID value.
42-
func UnknownValue() Value {
43-
return Value{unknown: true}
41+
// UUIDUnknown returns an unknown UUID value.
42+
func UUIDUnknown() UUID {
43+
return UUID{unknown: true}
4444
}
4545

46-
// StringValue returns a value or any errors when attempting to parse the string
47-
// as a UUID.
48-
func StringValue(value string, schemaPath path.Path) (Value, diag.Diagnostics) {
46+
// UUIDFromString returns a value or any errors when attempting to parse the
47+
// string as a UUID.
48+
func UUIDFromString(value string, schemaPath path.Path) (UUID, diag.Diagnostics) {
4949
validUUID, err := uuid.Parse(value)
5050
if err != nil {
51-
return UnknownValue(), diag.Diagnostics{
51+
return UUIDUnknown(), diag.Diagnostics{
5252
diag.NewAttributeErrorDiagnostic(
5353
schemaPath,
5454
"Invalid UUID String Value",
@@ -60,89 +60,86 @@ func StringValue(value string, schemaPath path.Path) (Value, diag.Diagnostics) {
6060
}
6161
}
6262

63-
return Value{
64-
value: validUUID.String(),
63+
return UUID{
64+
value: validUUID,
6565
}, nil
6666
}
6767

68-
// MustValue expects a valid UUID, otherwise will panic on error.
69-
func MustValue(value string) Value {
70-
validUUID, err := uuid.Parse(value)
71-
if err != nil {
72-
panic(err)
73-
}
74-
75-
return Value{
76-
value: validUUID.String(),
68+
// UUIDFromGoogleUUID expects a valid google/uuid.UUID and returns a Terraform
69+
// UUID Value.
70+
func UUIDFromGoogleUUID(value uuid.UUID) UUID {
71+
return UUID{
72+
value: value,
7773
}
7874
}
7975

80-
// Value provides a concrete implementation of a UUID tftypes.Value for the
76+
// UUID provides a concrete implementation of a UUID tftypes.Value for the
8177
// Terraform Plugin framework.
82-
type Value struct {
78+
type UUID struct {
8379
null bool
8480
unknown bool
85-
value string
81+
value uuid.UUID
8682
}
8783

88-
// Type returns the UUID type that created the Value.
89-
func (v Value) Type(_ context.Context) attr.Type {
90-
return Type{}
84+
// Type returns the UUID type that created the UUID.
85+
func (u UUID) Type(_ context.Context) attr.Type {
86+
return UUIDType{}
9187
}
9288

9389
// ToTerraformValue returns the UUID as a tftypes.Value.
94-
func (v Value) ToTerraformValue(_ context.Context) (tftypes.Value, error) {
95-
if v.IsNull() {
90+
func (u UUID) ToTerraformValue(_ context.Context) (tftypes.Value, error) {
91+
if u.IsNull() {
9692
return tftypes.NewValue(tftypes.String, nil), nil
9793
}
9894

99-
if v.IsUnknown() {
95+
if u.IsUnknown() {
10096
return tftypes.NewValue(tftypes.String, tftypes.UnknownValue), nil
10197
}
10298

103-
return tftypes.NewValue(tftypes.String, v.value), nil
99+
return tftypes.NewValue(tftypes.String, u.value.String()), nil
104100
}
105101

106102
// IsNull returns true if the uuid represents a null value.
107-
func (v Value) IsNull() bool {
108-
return v.null
103+
func (u UUID) IsNull() bool {
104+
return u.null
109105
}
110106

111107
// IsUnknown returns true if the uuid represents an unknown value.
112-
func (v Value) IsUnknown() bool {
113-
return v.unknown
108+
func (u UUID) IsUnknown() bool {
109+
return u.unknown
114110
}
115111

116112
// Equal returns true if the uuid is semantically equal to the Value passed as
117113
// an argument.
118-
func (v Value) Equal(other attr.Value) bool {
119-
otherValue, ok := other.(Value)
114+
func (u UUID) Equal(other attr.Value) bool {
115+
otherValue, ok := other.(UUID)
120116
if !ok {
121117
return false
122118
}
123119

124-
if otherValue.null != v.null {
120+
if otherValue.null != u.null {
125121
return false
126122
}
127123

128-
if otherValue.unknown != v.unknown {
124+
if otherValue.unknown != u.unknown {
129125
return false
130126
}
131127

132-
return otherValue.value == v.value
128+
// perform a byte-for-byte comparison.
129+
return otherValue.value == u.value
133130
}
134131

135132
// String returns a summary representation of either the underlying Value,
136133
// or UnknownValueString (`<unknown>`) when IsUnknown() returns true,
137134
// or NullValueString (`<null>`) when IsNull() return true.
138-
func (v Value) String() string {
139-
if v.null {
135+
func (u UUID) String() string {
136+
if u.null {
140137
return attr.NullValueString
141138
}
142139

143-
if v.unknown {
140+
if u.unknown {
144141
return attr.UnknownValueString
145142
}
146143

147-
return v.value
144+
return u.value.String()
148145
}

0 commit comments

Comments
 (0)