/
reflect.go
134 lines (105 loc) · 2.91 KB
/
reflect.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
package internal
import (
"errors"
"fmt"
"reflect"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/expr"
"github.com/stephenafamo/bob/internal/mappings"
"github.com/stephenafamo/bob/orm"
)
//nolint:gochecknoglobals
var unsettableTyp = reflect.TypeOf((*interface{ IsUnset() bool })(nil)).Elem()
func MappingCols(m mappings.Mapping, table ...string) orm.Columns {
// to make sure we don't modify the passed slice
cols := make([]string, 0, len(m.All))
for _, col := range m.All {
if col == "" {
continue
}
cols = append(cols, col)
}
copy(cols, m.All)
return orm.NewColumns(cols...).WithParent(table...)
}
// Get the values for non generated columns
func GetColumnValues[T any](mapping mappings.Mapping, filter []string, objs ...T) ([]string, [][]bob.Expression, error) {
if len(objs) == 0 {
return nil, nil, nil
}
allvalues := make([][]bob.Expression, 0, len(objs))
refVal1 := reflect.ValueOf(objs[0])
cols, vals1, err := getObjColsVals(mapping, filter, refVal1)
if err != nil {
return nil, nil, fmt.Errorf("get column list: %w", err)
}
allvalues = append(allvalues, vals1)
for index, obj := range objs[1:] {
refVal := reflect.ValueOf(obj)
values, err := getObjVals(mapping, cols, refVal)
if err != nil {
return nil, nil, fmt.Errorf("row %d: %w", index+2, err)
}
allvalues = append(allvalues, values)
}
return cols, allvalues, nil
}
func getObjColsVals(mapping mappings.Mapping, filter []string, val reflect.Value) ([]string, []bob.Expression, error) {
cols := make([]string, 0, len(mapping.NonGenerated))
values := make([]bob.Expression, 0, len(mapping.NonGenerated))
if val.Kind() == reflect.Pointer {
if val.IsNil() {
return nil, nil, errors.New("object is nil")
}
val = val.Elem()
}
hasFilter := len(filter) > 0
filterMap := sliceToMap(filter)
for colIndex, name := range mapping.NonGenerated {
if name == "" {
continue
}
if _, ok := filterMap[name]; !ok && hasFilter {
continue
}
field := val.Field(colIndex)
shoudSet := true
if field.Type().Implements(unsettableTyp) {
shoudSet = !field.MethodByName("IsUnset").Call(nil)[0].Interface().(bool)
}
if !shoudSet {
continue
}
cols = append(cols, name)
values = append(values, expr.Arg(field.Interface()))
}
return cols, values, nil
}
func getObjVals(mapping mappings.Mapping, cols []string, val reflect.Value) ([]bob.Expression, error) {
if val.Kind() == reflect.Pointer {
if val.IsNil() {
return nil, errors.New("object is nil")
}
val = val.Elem()
}
values := make([]bob.Expression, 0, len(cols))
for index, name := range mapping.NonGenerated {
if name == "" {
continue
}
for _, c := range cols {
if name == c {
field := val.Field(index)
values = append(values, expr.Arg(field.Interface()))
}
}
}
return values, nil
}
func sliceToMap[T comparable](s []T) map[T]int {
m := make(map[T]int, len(s))
for k, v := range s {
m[v] = k
}
return m
}