forked from valkey-io/valkey-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv.go
214 lines (203 loc) · 5.24 KB
/
conv.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
package om
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"unsafe"
"github.com/valkey-io/valkey-go"
)
func newHashConvFactory(t reflect.Type, schema schema) *hashConvFactory {
factory := &hashConvFactory{fields: make(map[string]fieldConv, len(schema.fields))}
for name, f := range schema.fields {
conv, ok := converters.val[f.typ.Kind()]
switch f.typ.Kind() {
case reflect.Ptr:
conv, ok = converters.ptr[f.typ.Elem().Kind()]
case reflect.Slice:
conv, ok = converters.slice[f.typ.Elem().Kind()]
}
if !ok {
k := f.typ.Kind()
panic(fmt.Sprintf("schema %q should not contain unsupported field type %s.", t, k))
}
factory.fields[name] = fieldConv{conv: conv, idx: f.idx}
}
return factory
}
type hashConvFactory struct {
fields map[string]fieldConv
}
type fieldConv struct {
conv converter
idx int
}
func (f hashConvFactory) NewConverter(entity reflect.Value) hashConv {
return hashConv{factory: f, entity: entity}
}
type hashConv struct {
factory hashConvFactory
entity reflect.Value
}
func (r hashConv) ToHash() (fields map[string]string) {
fields = make(map[string]string, len(r.factory.fields))
for k, f := range r.factory.fields {
ref := r.entity.Field(f.idx)
if f.conv.ValueToString == nil {
if bs, err := json.Marshal(ref.Interface()); err == nil {
fields[k] = valkey.BinaryString(bs)
}
} else if v, ok := f.conv.ValueToString(ref); ok {
fields[k] = v
}
}
return fields
}
func (r hashConv) FromHash(fields map[string]string) error {
for k, f := range r.factory.fields {
v, ok := fields[k]
if !ok {
continue
}
if f.conv.StringToValue == nil {
if err := json.Unmarshal(unsafe.Slice(unsafe.StringData(v), len(v)), r.entity.Field(f.idx).Addr().Interface()); err != nil {
return err
}
} else {
val, err := f.conv.StringToValue(v)
if err != nil {
return err
}
r.entity.Field(f.idx).Set(val)
}
}
return nil
}
type converter struct {
ValueToString func(value reflect.Value) (string, bool)
StringToValue func(value string) (reflect.Value, error)
}
var converters = struct {
val map[reflect.Kind]converter
ptr map[reflect.Kind]converter
slice map[reflect.Kind]converter
}{
ptr: map[reflect.Kind]converter{
reflect.Int64: {
ValueToString: func(value reflect.Value) (string, bool) {
if value.IsNil() {
return "", false
}
return strconv.FormatInt(value.Elem().Int(), 10), true
},
StringToValue: func(value string) (reflect.Value, error) {
v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(&v), nil
},
},
reflect.String: {
ValueToString: func(value reflect.Value) (string, bool) {
if value.IsNil() {
return "", false
}
return value.Elem().String(), true
},
StringToValue: func(value string) (reflect.Value, error) {
return reflect.ValueOf(&value), nil
},
},
reflect.Bool: {
ValueToString: func(value reflect.Value) (string, bool) {
if value.IsNil() {
return "", false
}
if value.Elem().Bool() {
return "t", true
}
return "f", true
},
StringToValue: func(value string) (reflect.Value, error) {
b := value == "t"
return reflect.ValueOf(&b), nil
},
},
reflect.Struct: {
ValueToString: nil,
StringToValue: nil,
},
},
val: map[reflect.Kind]converter{
reflect.Int64: {
ValueToString: func(value reflect.Value) (string, bool) {
return strconv.FormatInt(value.Int(), 10), true
},
StringToValue: func(value string) (reflect.Value, error) {
v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return reflect.Value{}, err
}
return reflect.ValueOf(v), nil
},
},
reflect.String: {
ValueToString: func(value reflect.Value) (string, bool) {
return value.String(), true
},
StringToValue: func(value string) (reflect.Value, error) {
return reflect.ValueOf(value), nil
},
},
reflect.Bool: {
ValueToString: func(value reflect.Value) (string, bool) {
if value.Bool() {
return "t", true
}
return "f", true
},
StringToValue: func(value string) (reflect.Value, error) {
b := value == "t"
return reflect.ValueOf(b), nil
},
},
reflect.Struct: {
ValueToString: nil,
StringToValue: nil,
},
},
slice: map[reflect.Kind]converter{
reflect.Uint8: {
ValueToString: func(value reflect.Value) (string, bool) {
return valkey.BinaryString(value.Bytes()), true
},
StringToValue: func(value string) (reflect.Value, error) {
buf := unsafe.Slice(unsafe.StringData(value), len(value))
return reflect.ValueOf(buf), nil
},
},
reflect.Float32: {
ValueToString: func(value reflect.Value) (string, bool) {
vs, ok := value.Interface().([]float32)
return valkey.VectorString32(vs), ok
},
StringToValue: func(value string) (reflect.Value, error) {
return reflect.ValueOf(valkey.ToVector32(value)), nil
},
},
reflect.Float64: {
ValueToString: func(value reflect.Value) (string, bool) {
vs, ok := value.Interface().([]float64)
return valkey.VectorString64(vs), ok
},
StringToValue: func(value string) (reflect.Value, error) {
return reflect.ValueOf(valkey.ToVector64(value)), nil
},
},
reflect.Struct: {
ValueToString: nil,
StringToValue: nil,
},
},
}