/
string.go
148 lines (122 loc) · 4 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package basetypes
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
)
var (
_ StringValuable = StringValue{}
)
// StringValuable extends attr.Value for string value types.
// Implement this interface to create a custom String value type.
type StringValuable interface {
attr.Value
// ToStringValue should convert the value type to a String.
ToStringValue(ctx context.Context) (StringValue, diag.Diagnostics)
}
// NewStringNull creates a String with a null value. Determine whether the value is
// null via the String type IsNull method.
//
// Setting the deprecated String type Null, Unknown, or Value fields after
// creating a String with this function has no effect.
func NewStringNull() StringValue {
return StringValue{
state: attr.ValueStateNull,
}
}
// NewStringUnknown creates a String with an unknown value. Determine whether the
// value is unknown via the String type IsUnknown method.
//
// Setting the deprecated String type Null, Unknown, or Value fields after
// creating a String with this function has no effect.
func NewStringUnknown() StringValue {
return StringValue{
state: attr.ValueStateUnknown,
}
}
// NewStringValue creates a String with a known value. Access the value via the String
// type ValueString method.
//
// Setting the deprecated String type Null, Unknown, or Value fields after
// creating a String with this function has no effect.
func NewStringValue(value string) StringValue {
return StringValue{
state: attr.ValueStateKnown,
value: value,
}
}
// StringValue represents a UTF-8 string value.
type StringValue struct {
// state represents whether the value is null, unknown, or known. The
// zero-value is null.
state attr.ValueState
// value contains the known value, if not null or unknown.
value string
}
// Type returns a StringType.
func (s StringValue) Type(_ context.Context) attr.Type {
return StringType{}
}
// ToTerraformValue returns the data contained in the *String as a tftypes.Value.
func (s StringValue) ToTerraformValue(_ context.Context) (tftypes.Value, error) {
switch s.state {
case attr.ValueStateKnown:
if err := tftypes.ValidateValue(tftypes.String, s.value); err != nil {
return tftypes.NewValue(tftypes.String, tftypes.UnknownValue), err
}
return tftypes.NewValue(tftypes.String, s.value), nil
case attr.ValueStateNull:
return tftypes.NewValue(tftypes.String, nil), nil
case attr.ValueStateUnknown:
return tftypes.NewValue(tftypes.String, tftypes.UnknownValue), nil
default:
panic(fmt.Sprintf("unhandled String state in ToTerraformValue: %s", s.state))
}
}
// Equal returns true if `other` is a String and has the same value as `s`.
func (s StringValue) Equal(other attr.Value) bool {
o, ok := other.(StringValue)
if !ok {
return false
}
if s.state != o.state {
return false
}
if s.state != attr.ValueStateKnown {
return true
}
return s.value == o.value
}
// IsNull returns true if the String represents a null value.
func (s StringValue) IsNull() bool {
return s.state == attr.ValueStateNull
}
// IsUnknown returns true if the String represents a currently unknown value.
func (s StringValue) IsUnknown() bool {
return s.state == attr.ValueStateUnknown
}
// String returns a human-readable representation of the String value. Use
// the ValueString method for Terraform data handling instead.
//
// The string returned here is not protected by any compatibility guarantees,
// and is intended for logging and error reporting.
func (s StringValue) String() string {
if s.IsUnknown() {
return attr.UnknownValueString
}
if s.IsNull() {
return attr.NullValueString
}
return fmt.Sprintf("%q", s.value)
}
// ValueString returns the known string value. If String is null or unknown, returns
// "".
func (s StringValue) ValueString() string {
return s.value
}
// ToStringValue returns String.
func (s StringValue) ToStringValue(context.Context) (StringValue, diag.Diagnostics) {
return s, nil
}