-
Notifications
You must be signed in to change notification settings - Fork 9
/
utility.go
64 lines (56 loc) · 1.44 KB
/
utility.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
package socketio
import (
"io"
seri "github.com/njones/socketio/serialize"
)
func ampersand(s string) *string { return &s }
// stoi is string to interface
func stoi(s []string) []interface{} {
rtn := make([]interface{}, len(s))
for i, v := range s {
rtn[i] = v
}
return rtn
}
func serviceError(err error) map[string]interface{} {
return map[string]interface{}{"message": err.Error()}
}
func scrub(useBinary bool, event Event, data []seri.Serializable) (hasBinary bool, out interface{}, cb eventCallback, err error) {
if !useBinary {
rtn := make([]string, len(data)+1)
rtn[0] = event
for i, v := range data {
if _, ok := v.(io.Reader); ok {
hasBinary = true
}
if cbv, ok := v.(eventCallback); ok && i == len(data)-1 {
return hasBinary, rtn[:len(rtn)-1], cbv, nil
}
rtn[i+1], err = v.Serialize()
if err != nil {
return hasBinary, nil, cb, ErrScrubFailed.F(err)
}
}
return hasBinary, rtn, nil, nil
}
type ifa interface{ Interface() interface{} }
rtn := make([]interface{}, len(data)+1)
rtn[0] = event
for i, v := range data {
if _, ok := v.(io.Reader); ok && !hasBinary {
hasBinary = true
}
if cbv, ok := v.(eventCallback); ok && i == len(data)-1 {
return hasBinary, rtn[:len(rtn)-1], cbv, nil
}
if vi, ok := v.(ifa); ok {
rtn[i+1] = vi.Interface()
if err, ok := rtn[i+1].(error); ok {
rtn[i+1] = err.Error()
}
continue
}
rtn[i+1] = v
}
return hasBinary, rtn, nil, nil
}