/
types.go
191 lines (151 loc) · 4.55 KB
/
types.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package gowt
import "strings"
import "os"
import "fmt"
import "strconv"
import "reflect"
import "time"
func GwtParse(reg *Registry, strtable, payload []string, partype string, idxv int) (interface{}, int, os.Error) {
idxp := strings.Index(partype, "/")
if idxp >= 0 {
partype = partype[0:idxp]
}
isarray := false
if partype[0] == '[' {
isarray = true
partype = partype[1:]
}
equiv := reg.Data[partype]
if equiv == nil {
return nil, 1, os.NewError(fmt.Sprintf("Invalid type: %s\n", partype))
}
if isarray == false {
return equiv.Parser(reg, strtable, payload, partype, idxv)
}
idxvp := idxv + 2
total := ToInt(payload[idxv + 1])
arr := reflect.MakeSlice(reflect.TypeOf([]interface{} {}), 0, total)
for i := 0; i < total; i++ {
v, adv, err := equiv.Parser(reg, strtable, payload, partype, idxvp)
if err != nil {
return nil, idxvp - idxv, err
}
arr = reflect.Append(arr, reflect.ValueOf(v))
idxvp += adv
}
return arr, idxvp - idxv, nil
}
func GwtInt(reg *Registry, strtable, payload []string, partype string, idxv int) (interface{}, int, os.Error) {
v, err := strconv.Atoi(payload[idxv])
if err != nil {
return nil, 1, err
}
return v, 1, nil
}
func GwtLong(reg *Registry, strtable, payload []string, partype string, idxv int) (interface{}, int, os.Error) {
v, err := ToLong(payload[idxv])
if err != nil {
return nil, 1, err
}
return v, 1, nil
}
func GwtFloat(reg *Registry, strtable, payload []string, partype string, idxv int) (interface{}, int, os.Error) {
v, err := strconv.Atof32(payload[idxv])
if err != nil {
return nil, 1, err
}
return v, 1, nil
}
func GwtDouble(reg *Registry, strtable, payload []string, partype string, idxv int) (interface{}, int, os.Error) {
v, err := strconv.Atof64(payload[idxv])
if err != nil {
return nil, 1, err
}
return v, 1, nil
}
func GwtChar(reg *Registry, strtable, payload []string, partype string, idxv int) (interface{}, int, os.Error) {
v, err := strconv.Atoui(payload[idxv])
if err != nil {
return nil, 1, err
}
return uint8(v), 1, nil
}
func GwtByte(reg *Registry, strtable, payload []string, partype string, idxv int) (interface{}, int, os.Error) {
v, err := strconv.Atoi(payload[idxv])
if err != nil {
return nil, 1, err
}
return int8(v), 1, nil
}
func GwtBool(reg *Registry, strtable, payload []string, partype string, idxv int) (interface{}, int, os.Error) {
return ToBool(payload[idxv]), 1, nil
}
func GwtString(reg *Registry, strtable, payload []string, partype string, idxv int) (interface{}, int, os.Error) {
return strtable[ToInt(payload[idxv])], 1, nil
}
func GwtDate(reg *Registry, strtable, payload []string, partype string, idxv int) (interface{}, int, os.Error) {
vb64 := payload[idxv + 1]
datab, err := ToLong(vb64)
if err != nil {
return nil, 2, err
}
//milli := datab % 1000 (maybe when GAE supports Nanoseconds)
datab = datab / 1000
t := time.SecondsToLocalTime(datab)
return t, 2, nil
}
func GwtArray(reg *Registry, strtable, payload []string, partype string, idxv int) (interface{}, int, os.Error) {
idxvp := idxv + 1
total := ToInt(payload[idxvp])
idxvp += 1
elems := make(map[int]interface{})
array := make([]interface{}, total)
for i := 0; i < total; i++ {
v := ToInt(payload[idxvp])
if v < 0 {
array[i] = elems[v]
idxvp += 1
} else {
a, adv, err := GwtParse(reg, strtable, payload, strtable[v], idxvp)
if err != nil {
return nil, 1, err
}
array[i] = a
idxvp += adv
elems[(i + 1) * -2] = a
}
}
return array, idxvp - idxv, nil
}
func GwtObject(reg *Registry, strtable, payload []string, partype string, idxv int) (interface{}, int, os.Error) {
equiv := reg.Data[partype]
if equiv == nil {
return nil, 1, os.NewError(fmt.Sprintf("Invalid type: %s", partype))
}
if equiv.GoType.Kind() != reflect.Struct {
return nil, 1, os.NewError(fmt.Sprintf("The type: %s is not a structure, it's a %s", equiv.JavaType, equiv.GoType.Kind().String()))
}
idxvp := idxv + 1
obj := reflect.New(equiv.GoType).Elem()
total := obj.NumField()
for i := 0; i < total; i++ {
fvalue := obj.Field(i)
ftype := fvalue.Type()
equivf := reg.Parsers[ftype]
parser := equivf.Parser
if parser == nil {
return nil, 1, os.NewError(fmt.Sprintf("Cannot find parser for type %s", ftype.Kind().String()))
}
if ToInt(payload[idxvp]) == 0 {
return nil, 1, nil
}
pvalue, adv, err := parser(reg, strtable, payload, equivf.JavaType, idxvp)
if err != nil {
return nil, 1, err
}
idxvp += adv
fvalue.Set(reflect.ValueOf(pvalue))
}
adv := idxvp - idxv
return obj.Interface(), adv, nil
}