diff options
Diffstat (limited to 'vendor/github.com/zclconf/go-cty/cty/convert/conversion_primitive.go')
-rw-r--r-- | vendor/github.com/zclconf/go-cty/cty/convert/conversion_primitive.go | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/vendor/github.com/zclconf/go-cty/cty/convert/conversion_primitive.go b/vendor/github.com/zclconf/go-cty/cty/convert/conversion_primitive.go new file mode 100644 index 0000000..e563ee3 --- /dev/null +++ b/vendor/github.com/zclconf/go-cty/cty/convert/conversion_primitive.go | |||
@@ -0,0 +1,50 @@ | |||
1 | package convert | ||
2 | |||
3 | import ( | ||
4 | "math/big" | ||
5 | |||
6 | "github.com/zclconf/go-cty/cty" | ||
7 | ) | ||
8 | |||
9 | var stringTrue = cty.StringVal("true") | ||
10 | var stringFalse = cty.StringVal("false") | ||
11 | |||
12 | var primitiveConversionsSafe = map[cty.Type]map[cty.Type]conversion{ | ||
13 | cty.Number: { | ||
14 | cty.String: func(val cty.Value, path cty.Path) (cty.Value, error) { | ||
15 | f := val.AsBigFloat() | ||
16 | return cty.StringVal(f.Text('f', -1)), nil | ||
17 | }, | ||
18 | }, | ||
19 | cty.Bool: { | ||
20 | cty.String: func(val cty.Value, path cty.Path) (cty.Value, error) { | ||
21 | if val.True() { | ||
22 | return stringTrue, nil | ||
23 | } else { | ||
24 | return stringFalse, nil | ||
25 | } | ||
26 | }, | ||
27 | }, | ||
28 | } | ||
29 | |||
30 | var primitiveConversionsUnsafe = map[cty.Type]map[cty.Type]conversion{ | ||
31 | cty.String: { | ||
32 | cty.Number: func(val cty.Value, path cty.Path) (cty.Value, error) { | ||
33 | f, _, err := big.ParseFloat(val.AsString(), 10, 512, big.ToNearestEven) | ||
34 | if err != nil { | ||
35 | return cty.NilVal, path.NewErrorf("a number is required") | ||
36 | } | ||
37 | return cty.NumberVal(f), nil | ||
38 | }, | ||
39 | cty.Bool: func(val cty.Value, path cty.Path) (cty.Value, error) { | ||
40 | switch val.AsString() { | ||
41 | case "true", "1": | ||
42 | return cty.True, nil | ||
43 | case "false", "0": | ||
44 | return cty.False, nil | ||
45 | default: | ||
46 | return cty.NilVal, path.NewErrorf("a bool is required") | ||
47 | } | ||
48 | }, | ||
49 | }, | ||
50 | } | ||