-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta.go
148 lines (130 loc) · 3.32 KB
/
meta.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
package dbutil
import (
"fmt"
"reflect"
"strings"
"sync"
"unicode"
)
func ToSnake(in string) string {
runes := []rune(in)
length := len(runes)
var out []rune
for i := 0; i < length; i++ {
if i > 0 && unicode.IsUpper(runes[i]) &&
((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
out = append(out, '_')
}
out = append(out, unicode.ToLower(runes[i]))
}
return string(out)
}
type TableMeta struct {
Type reflect.Type
fieldValues map[string]func(reflect.Value) reflect.Value
}
func (m *TableMeta) GetScanFields(names []string, v interface{}) (fields []interface{}, err error) {
val := reflect.ValueOf(v)
if val.Kind() == reflect.Ptr {
if val.Type().Elem() != m.Type {
return nil, fmt.Errorf("mismatched type: %v, meta type: %v", val.Type(), m.Type)
}
val = val.Elem()
} else {
return nil, fmt.Errorf("mismatched type: %v, meta type: %v", val.Type(), m.Type)
}
for _, name := range names {
f := m.fieldValues[name]
if f == nil {
fields = append(fields, new(interface{}))
} else {
fields = append(fields, f(val).Addr().Interface())
}
}
return
}
type PreparedScan struct {
typ reflect.Type
fs []func(reflect.Value) reflect.Value
}
func (p *PreparedScan) GetScanFields(v interface{}) (fields []interface{}, err error) {
val := reflect.ValueOf(v)
if val.Kind() == reflect.Ptr {
if val.Type().Elem() != p.typ {
return nil, fmt.Errorf("mismatched type: %v, meta type: %v", val.Type(), p.typ)
}
val = val.Elem()
} else {
return nil, fmt.Errorf("mismatched type: %v, meta type: %v", val.Type(), p.typ)
}
for _, f := range p.fs {
if f == nil {
fields = append(fields, new(interface{}))
} else {
fields = append(fields, f(val).Addr().Interface())
}
}
return
}
func (m *TableMeta) PrepareScan(names []string) (*PreparedScan, error) {
p := &PreparedScan{typ: m.Type, fs: make([]func(reflect.Value) reflect.Value, 0)}
for _, name := range names {
f := m.fieldValues[name]
if f == nil {
p.fs = append(p.fs, nil)
} else {
p.fs = append(p.fs, f)
}
}
return p, nil
}
var tableLk = &sync.RWMutex{}
var tableMetas = make(map[reflect.Type]*TableMeta)
func getTableFields(typ reflect.Type, preindex []int,
fields map[string]func(reflect.Value) reflect.Value) {
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
if field.Anonymous {
p := append(preindex, field.Index...)
getTableFields(field.Type, p, fields)
continue
}
if field.PkgPath != "" {
continue
}
fieldName := ToSnake(field.Name)
if dbTag := field.Tag.Get("db"); dbTag != "" {
parts := strings.Split(dbTag, ",")
if len(parts) > 0 && parts[0] != "" {
fieldName = parts[0]
}
}
idx := append(preindex, field.Index...)
fields[fieldName] = func(v reflect.Value) reflect.Value {
return v.FieldByIndex(idx)
}
}
}
func GetTableMeta(typ reflect.Type) *TableMeta {
if typ.Kind() != reflect.Struct {
panic("GetTableMeta only accept struct type")
}
tableLk.RLock()
meta, exists := tableMetas[typ]
tableLk.RUnlock()
if exists {
return meta
} else {
meta = &TableMeta{Type: typ,
fieldValues: make(map[string]func(reflect.Value) reflect.Value)}
getTableFields(typ, nil, meta.fieldValues)
tableLk.Lock()
if m, exists := tableMetas[typ]; !exists {
tableMetas[typ] = meta
} else {
meta = m
}
tableLk.Unlock()
return meta
}
}