-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathval.go
More file actions
99 lines (94 loc) · 1.86 KB
/
val.go
File metadata and controls
99 lines (94 loc) · 1.86 KB
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
package wasm
import (
"syscall/js"
"reflect"
"encoding/json"
)
func toValue(v interface{}) interface{} {
if v == nil {
return undefined
}
switch vv := v.(type) {
case int,int8,int16,int32,int64,
uint,uint8,uint16,uint32,uint64,
float32,float64,
string,bool:
return v
case []byte:
return string(vv)
case map[string]interface{}:
return vv
case js.Value:
return vv
default:
v2 := reflect.ValueOf(v)
switch v2.Kind() {
case reflect.Slice, reflect.Array:
r := make([]interface{}, v2.Len())
for i:=0; i<v2.Len(); i++ {
r[i] = toValue(v2.Index(i).Interface())
}
return r
/*
case reflect.Map:
r := make(map[interface{}]interface{})
iter := v2.MapRange()
for iter.Next() {
k, v1 := iter.Key(), iter.Value()
r[toValue(k.Interface())] = toValue(v1.Interface())
}
return r
*/
case reflect.Struct:
return bindGoStruct(v2)
case reflect.Ptr:
e := v2.Elem()
if e.Kind() == reflect.Struct {
return bindGoStruct(v2)
}
return toValue(e.Interface())
case reflect.Func:
if f, err := bindGoFunc(v); err == nil {
return f
}
return undefined
case reflect.Interface:
return bindGoInterface(v2)
default:
return null
}
}
}
func fromValue(v js.Value) (interface{}) {
switch v.Type() {
case js.TypeUndefined:
return nil
case js.TypeNull:
return nil
case js.TypeBoolean:
return v.Bool()
case js.TypeNumber:
return v.Float()
case js.TypeString:
return v.String()
case js.TypeSymbol:
return v.String()
case js.TypeObject:
if v.InstanceOf(array) {
l := v.Length()
res := make([]interface{}, l)
for i:=0; i<l; i++ {
res[i] = fromValue(v.Index(i))
}
return res
}
obj := jsJSON.Call("stringify", v).String()
res := make(map[string]interface{})
json.Unmarshal([]byte(obj), &res)
return res
case js.TypeFunction:
return v
default:
return nil
}
}