|
| 1 | +package timetypes |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/attr/xattr" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + _ basetypes.StringValuable = (*RFC3339PreciseToSecond)(nil) |
| 16 | + _ xattr.ValidateableAttribute = (*RFC3339PreciseToSecond)(nil) |
| 17 | +) |
| 18 | + |
| 19 | +// RFC3339PreciseToSecond represents a valid RFC3339-formatted string. It supports second-level precision and ignores milliseconds. |
| 20 | +// Semantic equality for this type normalizes timestamps to UTC, truncates the milliseconds, and compares the results. |
| 21 | +type RFC3339PreciseToSecond struct { |
| 22 | + basetypes.StringValue |
| 23 | +} |
| 24 | + |
| 25 | +// Type returns an RFC3339PreciseToSecondType. |
| 26 | +func (v RFC3339PreciseToSecond) Type(_ context.Context) attr.Type { |
| 27 | + return RFC3339PreciseToSecondType{} |
| 28 | +} |
| 29 | + |
| 30 | +// Equal returns true if the given value is equivalent. |
| 31 | +func (v RFC3339PreciseToSecond) Equal(o attr.Value) bool { |
| 32 | + other, ok := o.(RFC3339PreciseToSecond) |
| 33 | + |
| 34 | + if !ok { |
| 35 | + return false |
| 36 | + } |
| 37 | + |
| 38 | + return v.StringValue.Equal(other.StringValue) |
| 39 | +} |
| 40 | + |
| 41 | +// StringSemanticEquals returns true if the given RFC3339 timestamp is semantically equal the current RFC3339 timestamp. |
| 42 | +// This comparison ignores milliseconds, and normalizes both timestamps to UTC before comparison. |
| 43 | +// |
| 44 | +// Examples: |
| 45 | +// - `2023-07-25T22:43:16+02:00` is semantically equal to `2023-07-25T20:43:16Z` |
| 46 | +// - `2023-07-25T20:43:16.05Z` is semantically equal to `2023-07-25T20:43:16Z` |
| 47 | +func (v RFC3339PreciseToSecond) StringSemanticEquals(_ context.Context, newValuable basetypes.StringValuable) (bool, diag.Diagnostics) { |
| 48 | + var diags diag.Diagnostics |
| 49 | + |
| 50 | + newValue, ok := newValuable.(RFC3339PreciseToSecond) |
| 51 | + if !ok { |
| 52 | + diags.AddError( |
| 53 | + "Semantic Equality Check Error", |
| 54 | + "An unexpected value type was received while performing semantic equality checks. "+ |
| 55 | + "Please report this to the provider developers.\n\n"+ |
| 56 | + "Expected Value Type: "+fmt.Sprintf("%T", v)+"\n"+ |
| 57 | + "Got Value Type: "+fmt.Sprintf("%T", newValuable), |
| 58 | + ) |
| 59 | + |
| 60 | + return false, diags |
| 61 | + } |
| 62 | + |
| 63 | + newRFC3339time, _ := time.Parse(time.RFC3339, newValue.ValueString()) |
| 64 | + currentRFC3339time, _ := time.Parse(time.RFC3339, v.ValueString()) |
| 65 | + |
| 66 | + return normalize(currentRFC3339time) == normalize(newRFC3339time), diags |
| 67 | +} |
| 68 | + |
| 69 | +// Helper function to normalize a time.Time to UTC and truncate to second precision, and return as RFC3339 string. |
| 70 | +func normalize(t time.Time) string { |
| 71 | + return t.UTC().Truncate(time.Second).Format(time.RFC3339) |
| 72 | +} |
| 73 | + |
| 74 | +// This type requires the value to be a String value in valid RFC 3339 format |
| 75 | +func (v RFC3339PreciseToSecond) ValidateAttribute(ctx context.Context, req xattr.ValidateAttributeRequest, resp *xattr.ValidateAttributeResponse) { |
| 76 | + if v.IsUnknown() || v.IsNull() { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + if _, err := time.Parse(time.RFC3339, v.ValueString()); err != nil { |
| 81 | + resp.Diagnostics.Append( |
| 82 | + diag.WithPath(req.Path, diag.NewErrorDiagnostic("Invalid attribute value", "The attribute value must be a string in RFC3339 format.")), |
| 83 | + ) |
| 84 | + |
| 85 | + return |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// NewRFC3339PreciseToSecondNull creates an RFC3339PreciseToSecond with a null value. Determine whether the value is null via IsNull method. |
| 90 | +func NewRFC3339PreciseToSecondNull() RFC3339PreciseToSecond { |
| 91 | + return RFC3339PreciseToSecond{ |
| 92 | + StringValue: basetypes.NewStringNull(), |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// NewRFC3339PreciseToSecondUnknown creates an RFC3339PreciseToSecond with an unknown value. Determine whether the value is unknown via IsUnknown method. |
| 97 | +func NewRFC3339PreciseToSecondUnknown() RFC3339PreciseToSecond { |
| 98 | + return RFC3339PreciseToSecond{ |
| 99 | + StringValue: basetypes.NewStringUnknown(), |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// NewRFC3339PreciseToSecondValue creates an RFC3339PreciseToSecond with a known value or raises an error |
| 104 | +// diagnostic if the string is not RFC3339 format. |
| 105 | +func NewRFC3339PreciseToSecondValue(value string) (RFC3339PreciseToSecond, diag.Diagnostics) { |
| 106 | + _, err := time.Parse(time.RFC3339, value) |
| 107 | + |
| 108 | + if err != nil { |
| 109 | + // Returning an unknown value will guarantee that, as a last resort, |
| 110 | + // Terraform will return an error if attempting to store into state. |
| 111 | + return NewRFC3339PreciseToSecondUnknown(), diag.Diagnostics{} |
| 112 | + } |
| 113 | + |
| 114 | + return RFC3339PreciseToSecond{ |
| 115 | + StringValue: basetypes.NewStringValue(value), |
| 116 | + }, nil |
| 117 | +} |
| 118 | + |
| 119 | +// NewRFC3339PreciseToSecondValueMust creates an RFC3339PreciseToSecond with a known value or raises a panic |
| 120 | +// if the string is not RFC3339 format. |
| 121 | +// Used in unit tests. |
| 122 | +func NewRFC3339PreciseToSecondValueMust(value string) RFC3339PreciseToSecond { |
| 123 | + _, err := time.Parse(time.RFC3339, value) |
| 124 | + |
| 125 | + if err != nil { |
| 126 | + panic(fmt.Sprintf("Invalid RFC3339 String Value (%s): %s", value, err)) |
| 127 | + } |
| 128 | + |
| 129 | + return RFC3339PreciseToSecond{ |
| 130 | + StringValue: basetypes.NewStringValue(value), |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// NewRFC3339PreciseToSecondPointerValue creates an RFC3339PreciseToSecond with a null value if nil, a known |
| 135 | +// value, or raises an error diagnostic if the string is not RFC3339 format. |
| 136 | +func NewRFC3339PreciseToSecondPointerValue(value *string) (RFC3339PreciseToSecond, diag.Diagnostics) { |
| 137 | + if value == nil { |
| 138 | + return NewRFC3339PreciseToSecondNull(), nil |
| 139 | + } |
| 140 | + |
| 141 | + return NewRFC3339PreciseToSecondValue(*value) |
| 142 | +} |
0 commit comments