-
Notifications
You must be signed in to change notification settings - Fork 14
/
number.go
374 lines (344 loc) · 12.1 KB
/
number.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
package reflect
import (
"context"
"fmt"
"math"
"math/big"
"reflect"
"strconv"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/attr/xattr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// Number creates a *big.Float and populates it with the data in `val`. It then
// gets converted to the type of `target`, as long as `target` is a valid
// number type (any of the built-in int, uint, or float types, *big.Float, and
// *big.Int).
//
// Number will loudly fail when a number cannot be losslessly represented using
// the requested type, unless opts.AllowRoundingNumbers is set to true. This
// setting is mildly dangerous, because Terraform does not like when you round
// things, as a general rule of thumb.
//
// It is meant to be called through Into, not directly.
func Number(ctx context.Context, typ attr.Type, val tftypes.Value, target reflect.Value, opts Options, path path.Path) (reflect.Value, diag.Diagnostics) {
var diags diag.Diagnostics
result := big.NewFloat(0)
err := val.As(&result)
if err != nil {
diags.Append(diag.WithPath(path, DiagIntoIncompatibleType{
Err: err,
TargetType: target.Type(),
Val: val,
}))
return target, diags
}
roundingError := fmt.Errorf("cannot store %s in %s", result.String(), target.Type())
roundingErrorDiag := diag.NewAttributeErrorDiagnostic(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert to number. This is always an error in the provider. Please report the following to the provider developer:\n\n"+roundingError.Error(),
)
switch target.Type() {
case reflect.TypeOf(big.NewFloat(0)):
return reflect.ValueOf(result), diags
case reflect.TypeOf(big.NewInt(0)):
intResult, acc := result.Int(nil)
if acc != big.Exact && !opts.AllowRoundingNumbers {
return reflect.ValueOf(result), append(diags, roundingErrorDiag)
}
return reflect.ValueOf(intResult), diags
}
switch target.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64:
intResult, acc := result.Int64()
if acc != big.Exact && !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
switch target.Kind() {
case reflect.Int:
if strconv.IntSize == 32 && intResult > math.MaxInt32 {
if !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
intResult = math.MaxInt32
}
if strconv.IntSize == 32 && intResult < math.MinInt32 {
if !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
intResult = math.MinInt32
}
return reflect.ValueOf(int(intResult)), diags
case reflect.Int8:
if intResult > math.MaxInt8 {
if !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
intResult = math.MaxInt8
}
if intResult < math.MinInt8 {
if !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
intResult = math.MinInt8
}
return reflect.ValueOf(int8(intResult)), diags
case reflect.Int16:
if intResult > math.MaxInt16 {
if !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
intResult = math.MaxInt16
}
if intResult < math.MinInt16 {
if !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
intResult = math.MinInt16
}
return reflect.ValueOf(int16(intResult)), diags
case reflect.Int32:
if intResult > math.MaxInt32 {
if !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
intResult = math.MaxInt32
}
if intResult < math.MinInt32 {
if !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
intResult = math.MinInt32
}
return reflect.ValueOf(int32(intResult)), diags
case reflect.Int64:
return reflect.ValueOf(intResult), diags
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32,
reflect.Uint64:
uintResult, acc := result.Uint64()
if acc != big.Exact && !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
switch target.Kind() {
case reflect.Uint:
if strconv.IntSize == 32 && uintResult > math.MaxUint32 {
if !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
uintResult = math.MaxUint32
}
return reflect.ValueOf(uint(uintResult)), diags
case reflect.Uint8:
if uintResult > math.MaxUint8 {
if !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
uintResult = math.MaxUint8
}
return reflect.ValueOf(uint8(uintResult)), diags
case reflect.Uint16:
if uintResult > math.MaxUint16 {
if !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
uintResult = math.MaxUint16
}
return reflect.ValueOf(uint16(uintResult)), diags
case reflect.Uint32:
if uintResult > math.MaxUint32 {
if !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
uintResult = math.MaxUint32
}
return reflect.ValueOf(uint32(uintResult)), diags
case reflect.Uint64:
return reflect.ValueOf(uintResult), diags
}
case reflect.Float32:
floatResult, acc := result.Float32()
if acc != big.Exact && !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
} else if acc == big.Above {
floatResult = math.MaxFloat32
} else if acc == big.Below {
floatResult = math.SmallestNonzeroFloat32
} else if acc != big.Exact {
err := fmt.Errorf("unsure how to round %s and %f", acc, floatResult)
diags.AddAttributeError(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert to number. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error(),
)
return target, diags
}
return reflect.ValueOf(floatResult), diags
case reflect.Float64:
floatResult, acc := result.Float64()
if acc != big.Exact && !opts.AllowRoundingNumbers {
return target, append(diags, roundingErrorDiag)
}
if acc == big.Above {
if floatResult == math.Inf(1) || floatResult == math.MaxFloat64 {
floatResult = math.MaxFloat64
} else if floatResult == 0.0 || floatResult == math.SmallestNonzeroFloat64 {
floatResult = -math.SmallestNonzeroFloat64
} else {
err := fmt.Errorf("not sure how to round %s and %f", acc, floatResult)
diags.AddAttributeError(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert to number. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error(),
)
return target, diags
}
} else if acc == big.Below {
if floatResult == math.Inf(-1) || floatResult == -math.MaxFloat64 {
floatResult = -math.MaxFloat64
} else if floatResult == 0.0 || floatResult == -math.SmallestNonzeroFloat64 {
floatResult = math.SmallestNonzeroFloat64
} else {
err := fmt.Errorf("not sure how to round %s and %f", acc, floatResult)
diags.AddAttributeError(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert to number. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error(),
)
return target, diags
}
} else if acc != big.Exact {
err := fmt.Errorf("not sure how to round %s and %f", acc, floatResult)
diags.AddAttributeError(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert to number. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error(),
)
return target, diags
}
return reflect.ValueOf(floatResult), diags
}
err = fmt.Errorf("cannot convert number to %s", target.Type())
diags.AddAttributeError(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert to number. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error(),
)
return target, diags
}
// FromInt creates an attr.Value using `typ` from an int64.
//
// It is meant to be called through FromValue, not directly.
func FromInt(ctx context.Context, typ attr.Type, val int64, path path.Path) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics
err := tftypes.ValidateValue(tftypes.Number, val)
if err != nil {
return nil, append(diags, validateValueErrorDiag(err, path))
}
tfNum := tftypes.NewValue(tftypes.Number, val)
if typeWithValidate, ok := typ.(xattr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfNum, path)...)
if diags.HasError() {
return nil, diags
}
}
num, err := typ.ValueFromTerraform(ctx, tfNum)
if err != nil {
return nil, append(diags, valueFromTerraformErrorDiag(err, path))
}
return num, diags
}
// FromUint creates an attr.Value using `typ` from a uint64.
//
// It is meant to be called through FromValue, not directly.
func FromUint(ctx context.Context, typ attr.Type, val uint64, path path.Path) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics
err := tftypes.ValidateValue(tftypes.Number, val)
if err != nil {
return nil, append(diags, validateValueErrorDiag(err, path))
}
tfNum := tftypes.NewValue(tftypes.Number, val)
if typeWithValidate, ok := typ.(xattr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfNum, path)...)
if diags.HasError() {
return nil, diags
}
}
num, err := typ.ValueFromTerraform(ctx, tfNum)
if err != nil {
return nil, append(diags, valueFromTerraformErrorDiag(err, path))
}
return num, diags
}
// FromFloat creates an attr.Value using `typ` from a float64.
//
// It is meant to be called through FromValue, not directly.
func FromFloat(ctx context.Context, typ attr.Type, val float64, path path.Path) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics
err := tftypes.ValidateValue(tftypes.Number, val)
if err != nil {
return nil, append(diags, validateValueErrorDiag(err, path))
}
tfNum := tftypes.NewValue(tftypes.Number, val)
if typeWithValidate, ok := typ.(xattr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfNum, path)...)
if diags.HasError() {
return nil, diags
}
}
num, err := typ.ValueFromTerraform(ctx, tfNum)
if err != nil {
return nil, append(diags, valueFromTerraformErrorDiag(err, path))
}
return num, diags
}
// FromBigFloat creates an attr.Value using `typ` from a *big.Float.
//
// It is meant to be called through FromValue, not directly.
func FromBigFloat(ctx context.Context, typ attr.Type, val *big.Float, path path.Path) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics
err := tftypes.ValidateValue(tftypes.Number, val)
if err != nil {
return nil, append(diags, validateValueErrorDiag(err, path))
}
tfNum := tftypes.NewValue(tftypes.Number, val)
if typeWithValidate, ok := typ.(xattr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfNum, path)...)
if diags.HasError() {
return nil, diags
}
}
num, err := typ.ValueFromTerraform(ctx, tfNum)
if err != nil {
return nil, append(diags, valueFromTerraformErrorDiag(err, path))
}
return num, diags
}
// FromBigInt creates an attr.Value using `typ` from a *big.Int.
//
// It is meant to be called through FromValue, not directly.
func FromBigInt(ctx context.Context, typ attr.Type, val *big.Int, path path.Path) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics
fl := big.NewFloat(0).SetInt(val)
err := tftypes.ValidateValue(tftypes.Number, fl)
if err != nil {
return nil, append(diags, validateValueErrorDiag(err, path))
}
tfNum := tftypes.NewValue(tftypes.Number, fl)
if typeWithValidate, ok := typ.(xattr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfNum, path)...)
if diags.HasError() {
return nil, diags
}
}
num, err := typ.ValueFromTerraform(ctx, tfNum)
if err != nil {
return nil, append(diags, valueFromTerraformErrorDiag(err, path))
}
return num, diags
}