-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.go
More file actions
62 lines (51 loc) · 1.09 KB
/
Copy pathconvert.go
File metadata and controls
62 lines (51 loc) · 1.09 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
//go:build js && wasm
package grpcwasm
import (
"syscall/js"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func statusToGo(v js.Value) *status.Status {
return status.New(
codes.Code(v.Get("code").Int()),
v.Get("message").String(),
)
}
func statusToJs(v *status.Status) js.Value {
return js.ValueOf(map[string]any{
"code": js.ValueOf(int(v.Code())),
"message": js.ValueOf(v.Message()),
})
}
// MetaFromJS does not check type of [src].
// The [src] is expected to be:
//
// type Metadata = {
// [key: string]: string[]
// };
func metaToGo(dst metadata.MD, src js.Value) {
ks := js.Global().Get("Object").Call("keys", src)
l := ks.Length()
for i := range l {
k := ks.Index(i).String()
a := src.Get(k)
l := a.Length()
vs := make([]string, l)
for j := range l {
vs[j] = a.Index(j).String()
}
dst.Set(k, vs...)
}
}
func metaToJs(v metadata.MD) js.Value {
md := js.ValueOf(map[string]any{})
for k, vs := range v {
a := js.ValueOf([]any{})
for i, v := range vs {
a.SetIndex(i, v)
}
md.Set(k, a)
}
return md
}