|
| 1 | +package encodedstring |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + _ basetypes.StringValuableWithSemanticEquals = (*Base64Input)(nil) |
| 15 | +) |
| 16 | + |
| 17 | +// Base64Input represents a valid base64 encoded string. Custom semantic equality |
| 18 | +// logic is defined for Base64Input, where the encoded value is decoded, and then compared to the value received in response from Read / Create / Update. |
| 19 | +type Base64Input struct { |
| 20 | + basetypes.StringValue |
| 21 | +} |
| 22 | + |
| 23 | +// Type returns an Base64InputType. |
| 24 | +func (v Base64Input) Type(_ context.Context) attr.Type { |
| 25 | + return Base64InputType{} |
| 26 | +} |
| 27 | + |
| 28 | +// Equal returns true if the given value is equivalent. |
| 29 | +func (v Base64Input) Equal(o attr.Value) bool { |
| 30 | + other, ok := o.(Base64Input) |
| 31 | + |
| 32 | + if !ok { |
| 33 | + return false |
| 34 | + } |
| 35 | + |
| 36 | + return v.StringValue.Equal(other.StringValue) |
| 37 | +} |
| 38 | + |
| 39 | +// NewBase64InputNull creates an Base64Input with a null value. Determine whether the value is null via IsNull method. |
| 40 | +func NewBase64InputNull() Base64Input { |
| 41 | + return Base64Input{ |
| 42 | + StringValue: basetypes.NewStringNull(), |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// NewBase64InputUnknown creates an Base64Input with an unknown value. Determine whether the value is unknown via IsUnknown method. |
| 47 | +func NewBase64InputUnknown() Base64Input { |
| 48 | + return Base64Input{ |
| 49 | + StringValue: basetypes.NewStringUnknown(), |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// NewBase64InputValue creates an Base64Input with a known value. Access the value via ValueString method. |
| 54 | +func NewBase64InputValue(value string) Base64Input { |
| 55 | + return Base64Input{ |
| 56 | + StringValue: basetypes.NewStringValue(value), |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// NewBase64InputPointerValue creates an Base64Input with a null value if nil or a known value. Access the value via ValueStringPointer method. |
| 61 | +func NewBase64InputPointerValue(value *string) Base64Input { |
| 62 | + return Base64Input{ |
| 63 | + StringValue: basetypes.NewStringPointerValue(value), |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// StringSemanticEquals decodes givenValuable, compares it with the receiver string value and returns whether they are inconsequentially equal. |
| 68 | +// Semantic equality is checked during planning phase, and after receiving response in apply phase. In planning phase, givenValuable comes from the state, and v |
| 69 | +// is the current value - from Read method response. In the apply phase, givenValuable is from the plan, and v is from response of Create / Update. |
| 70 | +func (v Base64Input) StringSemanticEquals(ctx context.Context, givenValuable basetypes.StringValuable) (bool, diag.Diagnostics) { |
| 71 | + var diags diag.Diagnostics |
| 72 | + // The framework should always pass the correct value type, but always check |
| 73 | + _, ok := givenValuable.(Base64Input) |
| 74 | + |
| 75 | + if !ok { |
| 76 | + diags.AddError( |
| 77 | + "Semantic Equality Check Error", |
| 78 | + "An unexpected value type was received while performing semantic equality checks. "+ |
| 79 | + "Please report this to the provider developers.\n\n"+ |
| 80 | + "Expected Value Type: "+fmt.Sprintf("%T", v)+"\n"+ |
| 81 | + "Got Value Type: "+fmt.Sprintf("%T", givenValuable), |
| 82 | + ) |
| 83 | + |
| 84 | + return false, diags |
| 85 | + } |
| 86 | + |
| 87 | + givenStringValue, err := givenValuable.ToStringValue(ctx) |
| 88 | + if err != nil { |
| 89 | + // Not a StringValue type. |
| 90 | + diags.AddError( |
| 91 | + "Custom String Conversion Error", |
| 92 | + "An unexpected error was encountered trying to convert a custom string. This is always an error in the provider. Please report the following to the provider developer:\n\n", |
| 93 | + ) |
| 94 | + return false, diags |
| 95 | + } |
| 96 | + |
| 97 | + decodedGivenStringBytes, errors := base64.StdEncoding.DecodeString(givenStringValue.ValueString()) |
| 98 | + if errors != nil { |
| 99 | + // Not base64 encoded, return false so value from response is used in plan to compare with config. |
| 100 | + diags.AddError( |
| 101 | + "Custom String Decode Error", |
| 102 | + "An unexpected error was encountered trying to decode a custom string. This is always an error in the provider. Please report the following to the provider developer:\n\n", |
| 103 | + ) |
| 104 | + return false, diags |
| 105 | + } |
| 106 | + |
| 107 | + return string(decodedGivenStringBytes) == v.ValueString(), diags |
| 108 | +} |
0 commit comments