-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverters.go
97 lines (86 loc) · 2.25 KB
/
converters.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
package main
import (
"fmt"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/gocty"
)
type goType uint64
const (
gtInt goType = iota + 1<<32
gtString
gtFloat32
gtFloat64
)
var stringGoTypeCty = map[string]cty.Value{
"int": cty.NumberUIntVal(uint64(gtInt)),
"string": cty.NumberUIntVal(uint64(gtString)),
"float32": cty.NumberUIntVal(uint64(gtFloat32)),
"float64": cty.NumberUIntVal(uint64(gtFloat64)),
}
type converterType uint64
const (
ctStringToSlice converterType = iota + 1<<33
ctSliceToString
)
var stringConverterTypeCty = map[string]cty.Value{
"stringToSlice": cty.NumberUIntVal(uint64(ctStringToSlice)),
"sliceToString": cty.NumberUIntVal(uint64(ctSliceToString)),
}
func joinCtyVariables(maps ...map[string]cty.Value) map[string]cty.Value {
var totalSize int
for _, m := range maps {
totalSize += len(m)
}
final := make(map[string]cty.Value, totalSize)
for _, m := range maps {
for k, v := range m {
final[k] = v
}
}
return final
}
var stringFuncTypeCty = map[string]function.Function{
"converter": function.New(&function.Spec{
Params: []function.Parameter{
function.Parameter{
Name: "type",
Type: cty.Number,
},
},
Type: function.StaticReturnType(cty.Number),
Impl: converter,
}),
"gotype": function.New(&function.Spec{
Params: []function.Parameter{
function.Parameter{
Name: "type",
Type: cty.Number,
},
},
Type: function.StaticReturnType(cty.Number),
Impl: gotype,
}),
}
func converter(args []cty.Value, retType cty.Type) (cty.Value, error) {
var val uint64
err := gocty.FromCtyValue(args[0], &val)
if err != nil {
return cty.NullVal(cty.Number), fmt.Errorf("converter func: %w", err)
}
if val&uint64(1<<33) != uint64(1<<33) {
return cty.NullVal(cty.Number), fmt.Errorf("converter was passed in unkown type")
}
return cty.NumberUIntVal(val), nil
}
func gotype(args []cty.Value, retType cty.Type) (cty.Value, error) {
var val uint64
err := gocty.FromCtyValue(args[0], &val)
if err != nil {
return cty.NullVal(cty.Number), fmt.Errorf("gotype func: %w", err)
}
if val&uint64(1<<32) != uint64(1<<32) {
return cty.NullVal(cty.Number), fmt.Errorf("gotype was passed in unkown type")
}
return cty.NumberUIntVal(val), nil
}