forked from hprose/hprose-golang
/
struct_encoder.go
203 lines (186 loc) · 5.42 KB
/
struct_encoder.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
192
193
194
195
196
197
198
199
200
201
202
203
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* hprose/struct_encoder.go *
* *
* hprose struct encoder for Go. *
* *
* LastModified: Oct 22, 2015 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package io
import (
"reflect"
"strings"
"sync"
"unsafe"
"github.com/hprose/hprose-golang/util"
)
type fieldCache struct {
Name string
Alias string
Index []int
Type reflect.Type
Kind reflect.Kind
}
type structCache struct {
Alias string
Tag string
Fields []*fieldCache
FieldMap map[string]*fieldCache
Data []byte
}
var structTypeCache = map[uintptr]*structCache{}
var structTypeCacheLocker = sync.RWMutex{}
var structTypes = map[string]reflect.Type{}
var structTypesLocker = sync.RWMutex{}
func getFieldAlias(f *reflect.StructField, tag string) (alias string) {
fname := f.Name
if fname != "" && 'A' <= fname[0] && fname[0] < 'Z' {
if tag != "" && f.Tag != "" {
alias = strings.SplitN(f.Tag.Get(tag), ",", 2)[0]
alias = strings.TrimSpace(strings.SplitN(alias, ">", 2)[0])
if alias == "-" {
return ""
}
}
if alias == "" {
alias = string(fname[0]-'A'+'a') + fname[1:]
}
}
return alias
}
func getSubFields(t reflect.Type, tag string, index []int) []*fieldCache {
subFields := getFields(t, tag)
for _, subField := range subFields {
subField.Index = append(index, subField.Index...)
}
return subFields
}
func getFields(t reflect.Type, tag string) []*fieldCache {
n := t.NumField()
fields := make([]*fieldCache, 0, n)
for i := 0; i < n; i++ {
f := t.Field(i)
ft := f.Type
fkind := ft.Kind()
if fkind == reflect.Chan ||
fkind == reflect.Func ||
fkind == reflect.UnsafePointer {
continue
}
if f.Anonymous {
if fkind == reflect.Struct {
subFields := getSubFields(ft, tag, f.Index)
fields = append(fields, subFields...)
continue
}
}
alias := getFieldAlias(&f, tag)
if alias == "" {
continue
}
field := fieldCache{}
field.Name = f.Name
field.Alias = alias
field.Type = ft
field.Kind = fkind
field.Index = f.Index
fields = append(fields, &field)
}
return fields
}
func initStructCacheData(cache *structCache) {
w := &ByteWriter{}
fields := cache.Fields
count := len(fields)
cache.FieldMap = make(map[string]*fieldCache, count)
w.writeByte(TagClass)
var buf [20]byte
w.write(util.GetIntBytes(buf[:], int64(util.UTF16Length(cache.Alias))))
w.writeByte(TagQuote)
w.writeString(cache.Alias)
w.writeByte(TagQuote)
if count > 0 {
w.write(util.GetIntBytes(buf[:], int64(count)))
}
w.writeByte(TagOpenbrace)
for _, field := range fields {
cache.FieldMap[field.Alias] = field
w.writeByte(TagString)
w.write(util.GetIntBytes(buf[:], int64(util.UTF16Length(field.Alias))))
w.writeByte(TagQuote)
w.writeString(field.Alias)
w.writeByte(TagQuote)
}
w.writeByte(TagClosebrace)
cache.Data = w.Bytes()
}
func getStructCache(structType reflect.Type) *structCache {
typ := (*emptyInterface)(unsafe.Pointer(&structType)).ptr
structTypeCacheLocker.RLock()
if cache, ok := structTypeCache[typ]; ok {
structTypeCacheLocker.RUnlock()
return cache
}
structTypeCacheLocker.RUnlock()
structTypeCacheLocker.Lock()
cache, ok := structTypeCache[typ]
if !ok {
cache = &structCache{}
cache.Alias = structType.Name()
cache.Fields = getFields(structType, "")
initStructCacheData(cache)
structTypeCache[typ] = cache
structTypesLocker.Lock()
structTypes[cache.Alias] = structType
structTypesLocker.Unlock()
}
structTypeCacheLocker.Unlock()
return cache
}
// Register the type of the proto with alias & tag.
func Register(proto interface{}, alias string, tag ...string) {
structType := reflect.TypeOf(proto)
if structType.Kind() == reflect.Ptr {
structType = structType.Elem()
}
if structType.Kind() != reflect.Struct {
panic("invalid type: " + structType.String())
}
structTypesLocker.Lock()
structTypes[alias] = structType
structTypesLocker.Unlock()
structTypeCacheLocker.Lock()
cache := &structCache{Alias: alias}
if len(tag) == 1 {
cache.Tag = tag[0]
}
cache.Fields = getFields(structType, cache.Tag)
initStructCacheData(cache)
structTypeCache[(*emptyInterface)(unsafe.Pointer(&structType)).ptr] = cache
structTypeCacheLocker.Unlock()
}
// GetStructType by alias.
func GetStructType(alias string) (structType reflect.Type) {
structTypesLocker.RLock()
structType = structTypes[alias]
structTypesLocker.RUnlock()
return structType
}
// GetAlias of structType
func GetAlias(structType reflect.Type) string {
return getStructCache(structType).Alias
}
// GetTag by structType.
func GetTag(structType reflect.Type) string {
return getStructCache(structType).Tag
}