/
casts.go
44 lines (39 loc) · 994 Bytes
/
casts.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
package javascript
import (
"errors"
"github.com/dop251/goja"
)
func getMapFromValue(val goja.Value) (map[string]interface{}, error) {
outVal := val.Export()
v, ok := outVal.(map[string]interface{})
if !ok {
return nil, errors.New("value is not of type map")
}
return v, nil
}
func getSliceFromValue(val goja.Value) ([]interface{}, error) {
outVal := val.Export()
v, ok := outVal.([]interface{})
if !ok {
return nil, errors.New("value is not of type slice")
}
return v, nil
}
func getMapSliceFromValue(val goja.Value) ([]map[string]interface{}, error) {
outVal := val.Export()
if v, ok := outVal.([]map[string]interface{}); ok {
return v, nil
}
vSlice, ok := outVal.([]interface{})
if !ok {
return nil, errors.New("value is not of type map slice")
}
v := make([]map[string]interface{}, len(vSlice))
for i, e := range vSlice {
v[i], ok = e.(map[string]interface{})
if !ok {
return nil, errors.New("value is not of type map slice")
}
}
return v, nil
}