Skip to content

Commit 5564ae5

Browse files
committed
cleaning up and deprecating a bunch of stuff
1 parent f02c46f commit 5564ae5

File tree

3 files changed

+27
-68
lines changed

3 files changed

+27
-68
lines changed

conv/values.go

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -128,30 +128,13 @@ func ValueToStringType(v attr.Value) types.String {
128128
// function body for a particular type if you're interested in what "empty" means.
129129
func TestAttributeValueState(av attr.Value) error {
130130
var (
131-
undefined bool
132-
null bool
133-
empty bool
131+
empty bool
132+
133+
undefined = av.IsUnknown()
134+
null = av.IsNull()
134135
)
135136

136137
switch av.(type) {
137-
// bool values cannot be "empty"
138-
case types.Bool, *types.Bool:
139-
tv := ValueToBoolType(av)
140-
undefined = tv.IsUnknown()
141-
null = tv.IsNull()
142-
143-
// float values cannot be "empty"
144-
case types.Float64, *types.Float64:
145-
tv := ValueToFloat64Type(av)
146-
undefined = tv.IsUnknown()
147-
null = tv.IsNull()
148-
149-
// int values cannot be "empty"
150-
case types.Int64, *types.Int64:
151-
tv := ValueToInt64Type(av)
152-
undefined = tv.IsUnknown()
153-
null = tv.IsNull()
154-
155138
case types.List, *types.List:
156139
tv := ValueToListType(av)
157140
undefined = tv.IsUnknown()
@@ -164,17 +147,6 @@ func TestAttributeValueState(av attr.Value) error {
164147
null = tv.IsNull()
165148
empty = AttributeValueLength(av) == 0
166149

167-
case types.Number, *types.Number:
168-
tv := ValueToNumberType(av)
169-
undefined = tv.IsUnknown()
170-
null = tv.IsNull()
171-
172-
// todo: implement object "emptiness" check
173-
case types.Object, *types.Object:
174-
tv := ValueToObjectType(av)
175-
undefined = tv.IsUnknown()
176-
null = tv.IsNull()
177-
178150
case types.Set, *types.Set:
179151
tv := ValueToSetType(av)
180152
undefined = tv.IsUnknown()
@@ -185,10 +157,7 @@ func TestAttributeValueState(av attr.Value) error {
185157
tv := ValueToStringType(av)
186158
undefined = tv.IsUnknown()
187159
null = tv.IsNull()
188-
empty = StringValueToString(av) == ""
189-
190-
default:
191-
return ValueTypeUnhandledError("length_check", av)
160+
empty = AttributeValueToString(av) == ""
192161
}
193162

194163
if undefined {
@@ -203,59 +172,49 @@ func TestAttributeValueState(av attr.Value) error {
203172
}
204173

205174
// BoolValueToString accepts an instance of either types.Bool or *types.Bool, attempting to convert the value to a string.
175+
// DEPRECATED: use AttributeValueToString in all cases
206176
func BoolValueToString(v attr.Value) string {
207-
return strconv.FormatBool(ValueToBoolType(v).ValueBool())
177+
return AttributeValueToString(v)
208178
}
209179

210180
// Float64ValueToString accepts an instance of either types.Float64 or *types.Float64, attempting to convert the value to
211181
// a string.
182+
// DEPRECATED: use AttributeValueToString in all cases
212183
func Float64ValueToString(v attr.Value) string {
213-
return strconv.FormatFloat(ValueToFloat64Type(v).ValueFloat64(), 'g', int(FloatPrecision), 64)
184+
return AttributeValueToString(v)
214185
}
215186

216187
// Int64ValueToString accepts an instance of either types.Int64 or *types.Int64, attempting to convert the value to a string.
188+
// DEPRECATED: use AttributeValueToString in all cases
217189
func Int64ValueToString(v attr.Value) string {
218-
return strconv.FormatInt(ValueToInt64Type(v).ValueInt64(), 10)
190+
return AttributeValueToString(v)
219191
}
220192

221193
// NumberValueToString accepts an instance of either types.Number or *types.Number, attempting to convert the value to
222194
// a string.
195+
// DEPRECATED: use AttributeValueToString in all cases
223196
func NumberValueToString(v attr.Value) string {
224-
return ValueToNumberType(v).ValueBigFloat().String()
197+
return AttributeValueToString(v)
225198
}
226199

227200
// StringValueToString accepts an instance of either types.String or *types.String, returning the raw string value
201+
// DEPRECATED: use AttributeValueToString in all cases
228202
func StringValueToString(v attr.Value) string {
229-
return ValueToStringType(v).ValueString()
203+
return AttributeValueToString(v)
230204
}
231205

232206
// StringValueToBytes accepts an instance of either types.String or *types.String, returning the raw string value cast
233207
// to a byte slice
234208
func StringValueToBytes(v attr.Value) []byte {
235-
return []byte(StringValueToString(v))
209+
return []byte(AttributeValueToString(v))
236210
}
237211

238212
// AttributeValueToString will attempt to execute the appropriate AttributeStringerFunc from the ones registered.
239213
func AttributeValueToString(v attr.Value) string {
240-
switch v.(type) {
241-
case types.Bool, *types.Bool:
242-
return BoolValueToString(v)
243-
244-
case types.Float64, *types.Float64:
245-
return Float64ValueToString(v)
246-
247-
case types.Int64, *types.Int64:
248-
return Int64ValueToString(v)
249-
250-
case types.Number, *types.Number:
251-
return NumberValueToString(v)
252-
253-
case types.String, *types.String:
254-
return StringValueToString(v)
255-
256-
default:
257-
return fmt.Sprintf("%T", v)
214+
if s, ok := v.(types.String); ok {
215+
return s.ValueString()
258216
}
217+
return v.String()
259218
}
260219

261220
// AttributeValueToStrings attempts to convert the provided attr.Value into a slice of strings.
@@ -268,7 +227,7 @@ func AttributeValueToStrings(av attr.Value) []string {
268227
return StringSetToStrings(av)
269228
default:
270229
out := make([]string, 0)
271-
out = append(out, ValueToStringType(av).ValueString())
230+
out = append(out, AttributeValueToString(av))
272231
return out
273232
}
274233
}
@@ -435,7 +394,7 @@ func StringListToStrings(v attr.Value) []string {
435394
vt := ValueToListType(v)
436395
out := make([]string, len(vt.Elements()))
437396
for i, ve := range vt.Elements() {
438-
out[i] = StringValueToString(ve)
397+
out[i] = AttributeValueToString(ve)
439398
}
440399
return out
441400
}
@@ -446,7 +405,7 @@ func StringSetToStrings(v attr.Value) []string {
446405
vt := ValueToSetType(v)
447406
out := make([]string, len(vt.Elements()))
448407
for i, ve := range vt.Elements() {
449-
out[i] = StringValueToString(ve)
408+
out[i] = AttributeValueToString(ve)
450409
}
451410
return out
452411
}
@@ -563,7 +522,7 @@ func AttributeValueToBigFloat(v attr.Value) (*big.Float, error) {
563522
return NumberValueToBigFloat(v), nil
564523

565524
case types.String, *types.String:
566-
bf, _, err := big.ParseFloat(StringValueToString(v), 10, FloatPrecision, big.ToZero)
525+
bf, _, err := big.ParseFloat(AttributeValueToString(v), 10, FloatPrecision, big.ToZero)
567526
return bf, err
568527

569528
default:

validation/comparison.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func compareString(_ context.Context, av attr.Value, op CompareOp, target interf
241241
caseInsensitive = b
242242
}
243243
}
244-
actStr := conv.StringValueToString(av)
244+
actStr := conv.AttributeValueToString(av)
245245
tgtStr, ok := target.(string)
246246
if !ok {
247247
return UnexpectedComparisonTargetTypeError("compare_string", target, op, "", nil)

validation/validators.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type GenericConfig struct {
2828
SkipWhenUnknown bool
2929
}
3030

31-
// Generic is a validator that can be applied to variable attribute types
31+
// Generic a validator that can be applied to variable attribute types
3232
type Generic struct {
3333
validator.Describer
3434

@@ -417,7 +417,7 @@ func IsDurationString() Generic {
417417
// at runtime
418418
func TestEnvVarValued() TestFunc {
419419
return func(ctx context.Context, req GenericRequest, resp *GenericResponse) {
420-
varName := conv.StringValueToString(req.ConfigValue)
420+
varName := conv.AttributeValueToString(req.ConfigValue)
421421
if v, ok := os.LookupEnv(varName); !ok || strings.TrimSpace(v) == "" {
422422
resp.Diagnostics.AddAttributeError(
423423
req.Path,
@@ -446,7 +446,7 @@ func EnvVarValued() Generic {
446446
// attribute value.
447447
func TestFileIsReadable() TestFunc {
448448
return func(ctx context.Context, req GenericRequest, resp *GenericResponse) {
449-
fname := conv.StringValueToString(req.ConfigValue)
449+
fname := conv.AttributeValueToString(req.ConfigValue)
450450
fh, err := os.Open(fname)
451451

452452
if fh != nil {

0 commit comments

Comments
 (0)