]>
Commit | Line | Data |
---|---|---|
15c0b25d AP |
1 | package convert |
2 | ||
3 | import ( | |
15c0b25d AP |
4 | "github.com/zclconf/go-cty/cty" |
5 | ) | |
6 | ||
7 | var stringTrue = cty.StringVal("true") | |
8 | var stringFalse = cty.StringVal("false") | |
9 | ||
10 | var primitiveConversionsSafe = map[cty.Type]map[cty.Type]conversion{ | |
11 | cty.Number: { | |
12 | cty.String: func(val cty.Value, path cty.Path) (cty.Value, error) { | |
13 | f := val.AsBigFloat() | |
14 | return cty.StringVal(f.Text('f', -1)), nil | |
15 | }, | |
16 | }, | |
17 | cty.Bool: { | |
18 | cty.String: func(val cty.Value, path cty.Path) (cty.Value, error) { | |
19 | if val.True() { | |
20 | return stringTrue, nil | |
21 | } else { | |
22 | return stringFalse, nil | |
23 | } | |
24 | }, | |
25 | }, | |
26 | } | |
27 | ||
28 | var primitiveConversionsUnsafe = map[cty.Type]map[cty.Type]conversion{ | |
29 | cty.String: { | |
30 | cty.Number: func(val cty.Value, path cty.Path) (cty.Value, error) { | |
107c1cdb | 31 | v, err := cty.ParseNumberVal(val.AsString()) |
15c0b25d AP |
32 | if err != nil { |
33 | return cty.NilVal, path.NewErrorf("a number is required") | |
34 | } | |
107c1cdb | 35 | return v, nil |
15c0b25d AP |
36 | }, |
37 | cty.Bool: func(val cty.Value, path cty.Path) (cty.Value, error) { | |
38 | switch val.AsString() { | |
39 | case "true", "1": | |
40 | return cty.True, nil | |
41 | case "false", "0": | |
42 | return cty.False, nil | |
43 | default: | |
44 | return cty.NilVal, path.NewErrorf("a bool is required") | |
45 | } | |
46 | }, | |
47 | }, | |
48 | } |