-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathmarshal.go
247 lines (207 loc) · 5.05 KB
/
marshal.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2013-2015 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aerospike
import (
"math"
"reflect"
"strings"
"sync"
"time"
)
const (
aerospikeTag = "as"
keyTag = "key"
)
func valueToInterface(f reflect.Value) interface{} {
// get to the core value
for f.Kind() == reflect.Ptr {
if f.IsNil() {
return nil
}
f = reflect.Indirect(f)
}
switch f.Kind() {
case reflect.Uint64:
return int64(f.Uint())
case reflect.Float64, reflect.Float32:
return int(math.Float64bits(f.Float()))
case reflect.Struct:
if f.Type().PkgPath() == "time" && f.Type().Name() == "Time" {
return f.Interface().(time.Time).UTC().UnixNano()
} else {
return structToMap(f)
}
case reflect.Bool:
if f.Bool() == true {
return int64(1)
}
return int64(0)
case reflect.Map:
if f.IsNil() {
return nil
}
newMap := make(map[interface{}]interface{}, f.Len())
for _, mk := range f.MapKeys() {
newMap[valueToInterface(mk)] = valueToInterface(f.MapIndex(mk))
}
return f.Interface()
case reflect.Slice, reflect.Array:
if f.Kind() == reflect.Slice && f.IsNil() {
return nil
}
// convert to primitives recursively
newSlice := make([]interface{}, f.Len(), f.Cap())
for i := 0; i < len(newSlice); i++ {
newSlice[i] = valueToInterface(f.Index(i))
}
return newSlice
case reflect.Interface:
if f.IsNil() {
return nil
}
return f.Interface()
default:
return f.Interface()
}
}
func fieldAlias(f reflect.StructField) string {
alias := f.Tag.Get(aerospikeTag)
if alias != "" {
alias = strings.Trim(alias, " ")
// if tag is -, the field should not be persisted
if alias == "-" {
return ""
}
return alias
} else {
return f.Name
}
}
func structToMap(s reflect.Value) map[string]interface{} {
if !s.IsValid() {
return nil
}
// map tags
cacheObjectTags(s)
typeOfT := s.Type()
numFields := s.NumField()
var binMap map[string]interface{}
for i := 0; i < numFields; i++ {
// skip unexported fields
if typeOfT.Field(i).PkgPath != "" {
continue
}
binValue := valueToInterface(s.Field(i))
if binMap == nil {
binMap = make(map[string]interface{}, numFields)
}
alias := fieldAlias(typeOfT.Field(i))
if alias == "" {
continue
}
binMap[alias] = binValue
}
return binMap
}
func marshal(v interface{}) []*Bin {
s := reflect.Indirect(reflect.ValueOf(v).Elem())
typeOfT := s.Type()
// map tags
cacheObjectTags(s)
numFields := s.NumField()
bins := binPool.Get(numFields).([]*Bin)
binCount := 0
for i := 0; i < numFields; i++ {
// skip unexported fields
if typeOfT.Field(i).PkgPath != "" {
continue
}
binValue := valueToInterface(s.Field(i))
alias := fieldAlias(typeOfT.Field(i))
if alias == "" {
continue
}
bins[binCount].Name = alias
bins[binCount].Value = NewValue(binValue)
binCount++
}
return bins[:binCount]
}
type SyncMap struct {
objectMappings map[string]map[string]string
objectFields map[string][]string
mutex sync.RWMutex
}
func (sm *SyncMap) setMapping(obj reflect.Value, mapping map[string]string, fields []string) {
objType := obj.Type().Name()
sm.mutex.Lock()
sm.objectMappings[objType] = mapping
sm.objectFields[objType] = fields
sm.mutex.Unlock()
}
func (sm *SyncMap) mappingExists(obj reflect.Value) bool {
objType := obj.Type().Name()
sm.mutex.RLock()
_, exists := sm.objectMappings[objType]
sm.mutex.RUnlock()
return exists
}
func (sm *SyncMap) getMapping(obj reflect.Value) map[string]string {
objType := obj.Type().Name()
sm.mutex.RLock()
mapping := sm.objectMappings[objType]
sm.mutex.RUnlock()
return mapping
}
func (sm *SyncMap) getFields(obj reflect.Value) []string {
objType := obj.Type().Name()
sm.mutex.RLock()
fields := sm.objectFields[objType]
sm.mutex.RUnlock()
return fields
}
var objectMappings = &SyncMap{objectMappings: map[string]map[string]string{}, objectFields: map[string][]string{}}
func cacheObjectTags(obj reflect.Value) {
// exit if already processed
if objectMappings.mappingExists(obj) {
return
}
for obj.Kind() == reflect.Ptr {
if obj.IsNil() {
return
}
obj = reflect.Indirect(obj)
}
mapping := map[string]string{}
fields := []string{}
typeOfT := obj.Type()
numFields := obj.NumField()
for i := 0; i < numFields; i++ {
f := typeOfT.Field(i)
// skip unexported fields
if f.PkgPath != "" {
continue
}
tag := strings.Trim(f.Tag.Get(aerospikeTag), " ")
if tag != "-" {
if tag != "" {
mapping[tag] = f.Name
fields = append(fields, tag)
} else {
fields = append(fields, f.Name)
}
}
}
objectMappings.setMapping(obj, mapping, fields)
}