-
Notifications
You must be signed in to change notification settings - Fork 0
/
column_map.go
75 lines (63 loc) · 2.02 KB
/
column_map.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
package database
import (
"database/sql/driver"
"github.com/jmoiron/sqlx/reflectx"
"reflect"
"sync"
)
// ColumnMap provides a cached mapping of structs exported fields to their database column names.
type ColumnMap interface {
// Columns returns database column names for a struct's exported fields in a cached manner.
// Thus, the returned slice MUST NOT be modified directly.
// By default, all exported struct fields are mapped to database column names using snake case notation.
// The - (hyphen) directive for the db tag can be used to exclude certain fields.
Columns(any) []string
}
// NewColumnMap returns a new ColumnMap.
func NewColumnMap(mapper *reflectx.Mapper) ColumnMap {
return &columnMap{
cache: make(map[reflect.Type][]string),
mapper: mapper,
}
}
type columnMap struct {
mutex sync.Mutex
cache map[reflect.Type][]string
mapper *reflectx.Mapper
}
func (m *columnMap) Columns(subject any) []string {
m.mutex.Lock()
defer m.mutex.Unlock()
t, ok := subject.(reflect.Type)
if !ok {
t = reflect.TypeOf(subject)
}
columns, ok := m.cache[t]
if !ok {
columns = m.getColumns(t)
m.cache[t] = columns
}
return columns
}
func (m *columnMap) getColumns(t reflect.Type) []string {
fields := m.mapper.TypeMap(t).Names
columns := make([]string, 0, len(fields))
FieldLoop:
for _, f := range fields {
// If one of the parent fields implements the driver.Valuer interface, the field can be ignored.
for parent := f.Parent; parent != nil && parent.Zero.IsValid(); parent = parent.Parent {
// Check for pointer types.
if _, ok := reflect.New(parent.Field.Type).Interface().(driver.Valuer); ok {
continue FieldLoop
}
// Check for non-pointer types.
if _, ok := reflect.Zero(parent.Field.Type).Interface().(driver.Valuer); ok {
continue FieldLoop
}
}
columns = append(columns, f.Path)
}
// Shrink/reduce slice length and capacity:
// For a three-index slice (slice[a:b:c]), the length of the returned slice is b-a and the capacity is c-a.
return columns[0:len(columns):len(columns)]
}