-
Notifications
You must be signed in to change notification settings - Fork 0
/
pk.go
55 lines (49 loc) · 1.18 KB
/
pk.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
package model
import (
"github.com/pkg/errors"
"github.com/samber/lo"
"admini.dev/admini/app/lib/schema/field"
"admini.dev/admini/app/util"
)
func (m *Model) GetPK(logger util.Logger) []string {
if m.pk == nil {
lo.ForEach(m.Indexes, func(idx *Index, _ int) {
if idx.Primary {
if m.pk != nil && logger != nil {
logger.Error("multiple primary keys?!")
}
m.pk = idx.Fields
}
})
}
return m.pk
}
func (m *Model) IsPK(key string, logger util.Logger) bool {
pk := m.GetPK(logger)
return lo.ContainsBy(pk, func(col string) bool {
return col == key
})
}
func GetValues(src field.Fields, tgt []string, vals []any) ([]any, error) {
if len(src) != len(vals) {
return nil, errors.Errorf("[%d] fields provided, but [%d] values expected", len(vals), len(src))
}
ret := make([]any, 0, len(tgt))
lo.ForEach(tgt, func(t string, _ int) {
for idx, f := range src {
if f.Key == t {
ret = append(ret, vals[idx])
break
}
}
})
return ret, nil
}
func GetStrings(src field.Fields, tgt []string, vals []any) ([]string, error) {
is, err := GetValues(src, tgt, vals)
if err != nil {
return nil, err
}
ret := util.StringArrayFromAny(is, 0)
return ret, nil
}