This repository has been archived by the owner on Aug 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
value.go
86 lines (65 loc) · 1.49 KB
/
value.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
// +build js,wasm
package vecty
import (
"reflect"
"strings"
"syscall/js"
)
const (
structTag = "js"
structTagOptionIncludeEmpty = "includeEmpty"
)
const valueFieldName = "Value"
var jsValueType = reflect.TypeOf(js.Value{})
type JSValuer interface {
JSValue() js.Value
}
// Value Returns the js value of a type
func Value(p interface{}) js.Value {
vr, ok := p.(JSValuer)
if ok {
return vr.JSValue()
}
t := reflect.TypeOf(p)
rv := reflect.ValueOf(p)
switch t.Kind() {
case reflect.Struct:
// If the struct has an embedded js.Value then we return that.
f, ok := t.FieldByName(valueFieldName)
if ok && f.Anonymous && f.Type == jsValueType {
return rv.FieldByName(valueFieldName).Interface().(js.Value)
}
v := js.Global().Get("Object").New()
structValue(v, p)
return v
case reflect.Ptr:
return Value(rv.Elem().Interface())
default:
return js.ValueOf(p)
}
}
func structValue(v js.Value, p interface{}) {
t := reflect.TypeOf(p)
rv := reflect.ValueOf(p)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
fv := rv.Field(i)
if !fv.CanInterface() {
continue
}
fn := field.Name
tag := strings.Split(field.Tag.Get(structTag), ",")
if len(tag[0]) > 0 {
fn = tag[0]
}
if field.Anonymous {
structValue(v, fv.Interface())
continue
}
includeEmpty := len(tag) > 1 && tag[1] == structTagOptionIncludeEmpty
if !includeEmpty && fv.Interface() == reflect.Zero(field.Type).Interface() {
continue
}
v.Set(fn, Value(fv.Interface()))
}
}