1414 * limitations under the License.
1515 */
1616
17- package uuidtype
17+ package uuidtypes
1818
1919import (
2020 // Standard Library Imports
@@ -30,25 +30,25 @@ import (
3030
3131// Ensure Implementation matches the expected interfaces.
3232var (
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