|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package types |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/YakDriver/regexache" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/attr/xattr" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 18 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + _ xattr.TypeWithValidate = (*onceAWeekWindowType)(nil) |
| 23 | + _ basetypes.StringTypable = (*onceAWeekWindowType)(nil) |
| 24 | + _ basetypes.StringValuable = (*OnceAWeekWindow)(nil) |
| 25 | + _ basetypes.StringValuableWithSemanticEquals = (*OnceAWeekWindow)(nil) |
| 26 | +) |
| 27 | + |
| 28 | +type onceAWeekWindowType struct { |
| 29 | + basetypes.StringType |
| 30 | +} |
| 31 | + |
| 32 | +var ( |
| 33 | + OnceAWeekWindowType = onceAWeekWindowType{} |
| 34 | +) |
| 35 | + |
| 36 | +func (t onceAWeekWindowType) Equal(o attr.Type) bool { |
| 37 | + other, ok := o.(onceAWeekWindowType) |
| 38 | + if !ok { |
| 39 | + return false |
| 40 | + } |
| 41 | + |
| 42 | + return t.StringType.Equal(other.StringType) |
| 43 | +} |
| 44 | + |
| 45 | +func (onceAWeekWindowType) String() string { |
| 46 | + return "OnceAWeekWindowType" |
| 47 | +} |
| 48 | + |
| 49 | +func (t onceAWeekWindowType) ValueFromString(_ context.Context, in types.String) (basetypes.StringValuable, diag.Diagnostics) { |
| 50 | + var diags diag.Diagnostics |
| 51 | + |
| 52 | + if in.IsNull() { |
| 53 | + return OnceAWeekWindowNull(), diags |
| 54 | + } |
| 55 | + if in.IsUnknown() { |
| 56 | + return OnceAWeekWindowUnknown(), diags |
| 57 | + } |
| 58 | + |
| 59 | + return OnceAWeekWindowValue(in.ValueString()), diags |
| 60 | +} |
| 61 | + |
| 62 | +func (t onceAWeekWindowType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) { |
| 63 | + attrValue, err := t.StringType.ValueFromTerraform(ctx, in) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + stringValue, ok := attrValue.(basetypes.StringValue) |
| 69 | + if !ok { |
| 70 | + return nil, fmt.Errorf("unexpected value type of %T", attrValue) |
| 71 | + } |
| 72 | + |
| 73 | + stringValuable, diags := t.ValueFromString(ctx, stringValue) |
| 74 | + if diags.HasError() { |
| 75 | + return nil, fmt.Errorf("unexpected error converting StringValue to StringValuable: %v", diags) |
| 76 | + } |
| 77 | + |
| 78 | + return stringValuable, nil |
| 79 | +} |
| 80 | + |
| 81 | +func (onceAWeekWindowType) ValueType(context.Context) attr.Value { |
| 82 | + return OnceAWeekWindow{} |
| 83 | +} |
| 84 | + |
| 85 | +func (t onceAWeekWindowType) Validate(ctx context.Context, in tftypes.Value, path path.Path) diag.Diagnostics { |
| 86 | + var diags diag.Diagnostics |
| 87 | + |
| 88 | + if !in.IsKnown() || in.IsNull() { |
| 89 | + return diags |
| 90 | + } |
| 91 | + |
| 92 | + var value string |
| 93 | + err := in.As(&value) |
| 94 | + if err != nil { |
| 95 | + diags.AddAttributeError( |
| 96 | + path, |
| 97 | + "OnceAWeekWindowType Validation Error", |
| 98 | + ProviderErrorDetailPrefix+fmt.Sprintf("Cannot convert value to string: %s", err), |
| 99 | + ) |
| 100 | + return diags |
| 101 | + } |
| 102 | + |
| 103 | + // Valid time format is "ddd:hh24:mi". |
| 104 | + validTimeFormat := "(sun|mon|tue|wed|thu|fri|sat):([0-1][0-9]|2[0-3]):([0-5][0-9])" |
| 105 | + validTimeFormatConsolidated := "^(" + validTimeFormat + "-" + validTimeFormat + "|)$" |
| 106 | + |
| 107 | + if v := strings.ToLower(value); !regexache.MustCompile(validTimeFormatConsolidated).MatchString(v) { |
| 108 | + diags.AddAttributeError( |
| 109 | + path, |
| 110 | + "OnceAWeekWindowType Validation Error", |
| 111 | + fmt.Sprintf("Value %q must satisfy the format of \"ddd:hh24:mi-ddd:hh24:mi\".", value), |
| 112 | + ) |
| 113 | + return diags |
| 114 | + } |
| 115 | + |
| 116 | + return diags |
| 117 | +} |
| 118 | + |
| 119 | +type OnceAWeekWindow struct { |
| 120 | + basetypes.StringValue |
| 121 | +} |
| 122 | + |
| 123 | +func OnceAWeekWindowNull() OnceAWeekWindow { |
| 124 | + return OnceAWeekWindow{StringValue: basetypes.NewStringNull()} |
| 125 | +} |
| 126 | + |
| 127 | +func OnceAWeekWindowUnknown() OnceAWeekWindow { |
| 128 | + return OnceAWeekWindow{StringValue: basetypes.NewStringUnknown()} |
| 129 | +} |
| 130 | + |
| 131 | +func OnceAWeekWindowValue(value string) OnceAWeekWindow { |
| 132 | + return OnceAWeekWindow{StringValue: basetypes.NewStringValue(value)} |
| 133 | +} |
| 134 | + |
| 135 | +func (v OnceAWeekWindow) Equal(o attr.Value) bool { |
| 136 | + other, ok := o.(OnceAWeekWindow) |
| 137 | + if !ok { |
| 138 | + return false |
| 139 | + } |
| 140 | + |
| 141 | + return v.StringValue.Equal(other.StringValue) |
| 142 | +} |
| 143 | + |
| 144 | +func (OnceAWeekWindow) Type(context.Context) attr.Type { |
| 145 | + return OnceAWeekWindowType |
| 146 | +} |
| 147 | + |
| 148 | +func (v OnceAWeekWindow) StringSemanticEquals(ctx context.Context, newValuable basetypes.StringValuable) (bool, diag.Diagnostics) { |
| 149 | + var diags diag.Diagnostics |
| 150 | + |
| 151 | + newValue, ok := newValuable.(OnceAWeekWindow) |
| 152 | + if !ok { |
| 153 | + return false, diags |
| 154 | + } |
| 155 | + |
| 156 | + old, d := v.ToStringValue(ctx) |
| 157 | + diags.Append(d...) |
| 158 | + if diags.HasError() { |
| 159 | + return false, diags |
| 160 | + } |
| 161 | + |
| 162 | + new, d := newValue.ToStringValue(ctx) |
| 163 | + diags.Append(d...) |
| 164 | + if diags.HasError() { |
| 165 | + return false, diags |
| 166 | + } |
| 167 | + |
| 168 | + // Case insensitive comparison. |
| 169 | + return strings.EqualFold(old.ValueString(), new.ValueString()), diags |
| 170 | +} |
0 commit comments