|
1 | 1 | /* |
2 | | - * Copyright (c) 2022 Matthew Hartstonge <[email protected]> |
| 2 | + * Copyright (c) 2023 Matthew Hartstonge <[email protected]> |
3 | 3 | * |
4 | 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
5 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
9 | 9 | package uuidtypes |
10 | 10 |
|
11 | 11 | import ( |
12 | | - // Standard Library Imports |
13 | | - "context" |
14 | | - |
15 | 12 | // External Imports |
16 | | - "github.com/google/uuid" |
17 | | - "github.com/hashicorp/terraform-plugin-framework/attr" |
18 | | - "github.com/hashicorp/terraform-plugin-framework/diag" |
19 | | - "github.com/hashicorp/terraform-plugin-framework/path" |
20 | | - "github.com/hashicorp/terraform-plugin-go/tftypes" |
21 | | -) |
22 | | - |
23 | | -// Ensure Implementation matches the expected interfaces. |
24 | | -var ( |
25 | | - _ attr.Value = UUID{} |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
26 | 14 | ) |
27 | 15 |
|
28 | | -// UUIDNull returns a null UUID value. |
29 | | -func UUIDNull() UUID { |
30 | | - return UUID{state: attr.ValueStateNull} |
31 | | -} |
32 | | - |
33 | | -// UUIDUnknown returns an unknown UUID value. |
34 | | -func UUIDUnknown() UUID { |
35 | | - return UUID{state: attr.ValueStateUnknown} |
36 | | -} |
37 | | - |
38 | | -// UUIDFromString returns a value or any errors when attempting to parse the |
39 | | -// string as a UUID. |
40 | | -func UUIDFromString(value string, schemaPath path.Path) (UUID, diag.Diagnostics) { |
41 | | - validUUID, err := uuid.Parse(value) |
42 | | - if err != nil { |
43 | | - return UUIDUnknown(), diag.Diagnostics{ |
44 | | - diag.NewAttributeErrorDiagnostic( |
45 | | - schemaPath, |
46 | | - "Invalid UUID String Value", |
47 | | - "An unexpected error occurred attempting to parse a string value that was expected to be a valid UUID format. "+ |
48 | | - "The expected UUID format is 00000000-0000-0000-0000-00000000. "+ |
49 | | - "For example, a Version 4 UUID is of the form 7b16fd41-cc23-4ef7-8aa9-c598350ccd18.\n\n"+ |
50 | | - "Error: "+err.Error(), |
51 | | - ), |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - return UUID{ |
56 | | - state: attr.ValueStateKnown, |
57 | | - value: validUUID, |
58 | | - }, nil |
59 | | -} |
60 | | - |
61 | | -// UUIDFromGoogleUUID expects a valid google/uuid.UUID and returns a Terraform |
62 | | -// UUID Value. |
63 | | -func UUIDFromGoogleUUID(value uuid.UUID) UUID { |
64 | | - return UUID{ |
65 | | - state: attr.ValueStateKnown, |
66 | | - value: value, |
67 | | - } |
68 | | -} |
69 | | - |
70 | | -// UUID provides a concrete implementation of a UUID tftypes.Value for the |
71 | | -// Terraform Plugin framework. |
72 | | -type UUID struct { |
73 | | - state attr.ValueState |
74 | | - value uuid.UUID |
75 | | -} |
76 | | - |
77 | | -// Type returns the UUID type that created the UUID. |
78 | | -func (u UUID) Type(_ context.Context) attr.Type { |
79 | | - return UUIDType{} |
80 | | -} |
81 | | - |
82 | | -// ToTerraformValue returns the UUID as a tftypes.Value. |
83 | | -func (u UUID) ToTerraformValue(_ context.Context) (tftypes.Value, error) { |
84 | | - if u.IsNull() { |
85 | | - return tftypes.NewValue(tftypes.String, nil), nil |
86 | | - } |
87 | | - |
88 | | - if u.IsUnknown() { |
89 | | - return tftypes.NewValue(tftypes.String, tftypes.UnknownValue), nil |
90 | | - } |
| 16 | +type UUID = UUIDValue |
91 | 17 |
|
92 | | - return tftypes.NewValue(tftypes.String, u.value.String()), nil |
| 18 | +// NewUUIDNull creates a UUID with a null value. Determine whether the value is |
| 19 | +// null via the UUID type IsNull method. |
| 20 | +func NewUUIDNull() UUIDValue { |
| 21 | + return UUIDValue{StringValue: basetypes.NewStringNull()} |
93 | 22 | } |
94 | 23 |
|
95 | | -// IsNull returns true if the uuid represents a null value. |
96 | | -func (u UUID) IsNull() bool { |
97 | | - return u.state == attr.ValueStateNull |
| 24 | +// NewUUIDUnknown creartes a UUID with an unknown UUID value. Determine whether |
| 25 | +// the value is unknown via the UUID type IsUnknown method. |
| 26 | +func NewUUIDUnknown() UUIDValue { |
| 27 | + return UUIDValue{StringValue: basetypes.NewStringUnknown()} |
98 | 28 | } |
99 | 29 |
|
100 | | -// IsUnknown returns true if the uuid represents an unknown value. |
101 | | -func (u UUID) IsUnknown() bool { |
102 | | - return u.state == attr.ValueStateUnknown |
103 | | -} |
104 | | - |
105 | | -// Equal returns true if the uuid is semantically equal to the Value passed as |
106 | | -// an argument. |
107 | | -func (u UUID) Equal(other attr.Value) bool { |
108 | | - otherValue, ok := other.(UUID) |
109 | | - if !ok { |
110 | | - return false |
111 | | - } |
112 | | - |
113 | | - if otherValue.state != u.state { |
114 | | - return false |
| 30 | +// NewUUIDValue creates a UUID with a known value. Access the value via the |
| 31 | +// String type ValueString method. |
| 32 | +func NewUUIDValue(value string) UUIDValue { |
| 33 | + return UUIDValue{ |
| 34 | + StringValue: basetypes.NewStringValue(value), |
115 | 35 | } |
116 | | - |
117 | | - // perform a byte-for-byte comparison. |
118 | | - return otherValue.value == u.value |
119 | 36 | } |
120 | 37 |
|
121 | | -// String returns a summary representation of either the underlying Value, |
122 | | -// or UnknownValueString (`<unknown>`) when IsUnknown() returns true, |
123 | | -// or NullValueString (`<null>`) when IsNull() return true. |
124 | | -func (u UUID) String() string { |
125 | | - switch u.state { |
126 | | - case attr.ValueStateNull: |
127 | | - return attr.NullValueString |
128 | | - |
129 | | - case attr.ValueStateUnknown: |
130 | | - return attr.UnknownValueString |
131 | | - |
132 | | - default: |
133 | | - return u.value.String() |
| 38 | +// NewUUIDPointerValue creates a UUID with a null value if nil or a known |
| 39 | +// value. Access the value via the String type ValueStringPointer method. |
| 40 | +func NewUUIDPointerValue(value *string) UUIDValue { |
| 41 | + return UUIDValue{ |
| 42 | + StringValue: basetypes.NewStringPointerValue(value), |
134 | 43 | } |
135 | 44 | } |
0 commit comments