/
marshal.go
217 lines (182 loc) · 4.4 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
// Copyright 2013-2014 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, reflect.Slice, 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 binValue != nil {
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))
if binValue != nil {
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(objType string, mapping map[string]string, fields []string) {
sm.mutex.Lock()
sm.objectMappings[objType] = mapping
sm.mutex.Unlock()
}
func (sm *SyncMap) mappingExists(objType string) bool {
sm.mutex.RLock()
_, exists := sm.objectMappings[objType]
sm.mutex.RUnlock()
return exists
}
func (sm *SyncMap) getMapping(objType string) map[string]string {
sm.mutex.RLock()
mapping := sm.objectMappings[objType]
sm.mutex.RUnlock()
return mapping
}
func (sm *SyncMap) getFields(objType string) []string {
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) {
objType := obj.Type().Name()
// exit if already processed
if objectMappings.mappingExists(objType) {
return
}
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(objType, mapping, fields)
}