-
Notifications
You must be signed in to change notification settings - Fork 131
/
string.go
110 lines (80 loc) · 2.27 KB
/
string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package types
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
type TfStringValue struct {
basetypes.StringValue
}
var _ basetypes.StringValuable = TfStringValue{}
func (t *TfStringValue) UnmarshalJSON(data []byte) error {
var v string
if err := json.Unmarshal(data, &v); err != nil {
return err
}
t.StringValue = basetypes.NewStringValue(v)
return nil
}
func (t TfStringValue) MarshalJSON() ([]byte, error) {
return json.Marshal(t.StringValue.ValueString())
}
func (v TfStringValue) Equal(o attr.Value) bool {
other, ok := o.(TfStringValue)
if !ok {
return false
}
return v.StringValue.Equal(other.StringValue)
}
func (v TfStringValue) Type(ctx context.Context) attr.Type {
return TfStringType{}
}
func NewTfStringValue(value string) TfStringValue {
return TfStringValue{
StringValue: basetypes.NewStringValue(value),
}
}
type TfStringType struct {
basetypes.StringType
}
var _ basetypes.StringTypable = TfStringType{}
func (t TfStringType) Equal(o attr.Type) bool {
other, ok := o.(TfStringType)
if !ok {
return false
}
return t.StringType.Equal(other.StringType)
}
func (t TfStringType) String() string {
return "TfStringType"
}
func (t TfStringType) ValueFromString(ctx context.Context, in basetypes.StringValue) (basetypes.StringValuable, diag.Diagnostics) {
value := TfStringValue{
StringValue: in,
}
return value, nil
}
func (t TfStringType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
attrValue, err := t.StringType.ValueFromTerraform(ctx, in)
if err != nil {
return nil, err
}
stringValue, ok := attrValue.(basetypes.StringValue)
if !ok {
return nil, fmt.Errorf("unexpected value type of %T", attrValue)
}
stringValuable, diags := t.ValueFromString(ctx, stringValue)
if diags.HasError() {
return nil, fmt.Errorf("unexpected error converting StringValue to StringValuable: %v", diags)
}
return stringValuable, nil
}
func (t TfStringType) ValueType(ctx context.Context) attr.Value {
return TfStringValue{}
}