forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 6
/
attachment.go
168 lines (160 loc) · 3.66 KB
/
attachment.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package socketio
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
)
// Attachment is an attachment handler used in emit args. All attachments will be sent as binary data in the transport layer. When using an attachment, make sure it is a pointer.
//
// For example:
//
// type Arg struct {
// Title string `json:"title"`
// File *Attachment `json:"file"`
// }
//
// f, _ := os.Open("./some_file")
// arg := Arg{
// Title: "some_file",
// File: &Attachment{
// Data: f,
// }
// }
//
// socket.Emit("send file", arg)
// socket.On("get file", func(so Socket, arg Arg) {
// b, _ := ioutil.ReadAll(arg.File.Data)
// })
type Attachment struct {
Data io.ReadWriter
num int
}
func encodeAttachments(v interface{}) []io.Reader {
index := 0
return encodeAttachmentValue(reflect.ValueOf(v), &index)
}
func encodeAttachmentValue(v reflect.Value, index *int) []io.Reader {
v = reflect.Indirect(v)
ret := []io.Reader{}
if !v.IsValid() {
return ret
}
switch v.Kind() {
case reflect.Struct:
if v.Type().Name() == "Attachment" {
a, ok := v.Addr().Interface().(*Attachment)
if !ok {
panic("can't convert")
}
a.num = *index
ret = append(ret, a.Data)
(*index)++
return ret
}
for i, n := 0, v.NumField(); i < n; i++ {
var r []io.Reader
r = encodeAttachmentValue(v.Field(i), index)
ret = append(ret, r...)
}
case reflect.Map:
if v.IsNil() {
return ret
}
for _, key := range v.MapKeys() {
var r []io.Reader
r = encodeAttachmentValue(v.MapIndex(key), index)
ret = append(ret, r...)
}
case reflect.Slice:
if v.IsNil() {
return ret
}
fallthrough
case reflect.Array:
for i, n := 0, v.Len(); i < n; i++ {
var r []io.Reader
r = encodeAttachmentValue(v.Index(i), index)
ret = append(ret, r...)
}
case reflect.Interface:
ret = encodeAttachmentValue(reflect.ValueOf(v.Interface()), index)
}
return ret
}
func decodeAttachments(v interface{}, binary [][]byte) error {
return decodeAttachmentValue(reflect.ValueOf(v), binary)
}
func decodeAttachmentValue(v reflect.Value, binary [][]byte) error {
v = reflect.Indirect(v)
if !v.IsValid() {
return fmt.Errorf("invalid value")
}
switch v.Kind() {
case reflect.Struct:
if v.Type().Name() == "Attachment" {
a, ok := v.Addr().Interface().(*Attachment)
if !ok {
panic("can't convert")
}
if a.num >= len(binary) || a.num < 0 {
return fmt.Errorf("out of range")
}
if a.Data == nil {
a.Data = bytes.NewBuffer(nil)
}
for b := binary[a.num]; len(b) > 0; {
n, err := a.Data.Write(b)
if err != nil {
return err
}
b = b[n:]
}
return nil
}
for i, n := 0, v.NumField(); i < n; i++ {
if err := decodeAttachmentValue(v.Field(i), binary); err != nil {
return err
}
}
case reflect.Map:
if v.IsNil() {
return nil
}
for _, key := range v.MapKeys() {
if err := decodeAttachmentValue(v.MapIndex(key), binary); err != nil {
return err
}
}
case reflect.Slice:
if v.IsNil() {
return nil
}
fallthrough
case reflect.Array:
for i, n := 0, v.Len(); i < n; i++ {
if err := decodeAttachmentValue(v.Index(i), binary); err != nil {
return err
}
}
case reflect.Interface:
if err := decodeAttachmentValue(reflect.ValueOf(v.Interface()), binary); err != nil {
return err
}
}
return nil
}
func (a Attachment) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("{\"_placeholder\":true,\"num\":%d}", a.num)), nil
}
func (a *Attachment) UnmarshalJSON(b []byte) error {
var v struct {
Num int `json:"num"`
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
a.num = v.Num
return nil
}