-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference.go
48 lines (39 loc) · 1.27 KB
/
reference.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
// Content managed by Project Forge, see [projectforge.md] for details.
package model
import (
"fmt"
"strings"
"github.com/samber/lo"
"admini.dev/admini/app/util"
)
type Reference struct {
Key string `json:"key"`
TargetFields []string `json:"tgtFields"`
SourcePkg util.Pkg `json:"sourcePackage"`
SourceModel string `json:"sourceModel"`
SourceFields []string `json:"srcFields"`
str string
}
func ReferenceFromRelation(rel *Relationship, m *Model) *Reference {
fields := lo.FilterMap(rel.SourceFields, func(x string, _ int) (string, bool) {
_, col := m.Fields.Get(x)
if col != nil {
return col.Name(), true
}
return "", false
})
str := fmt.Sprintf("%s by %s", m.PluralName(), strings.Join(fields, ", "))
return &Reference{Key: rel.Key, TargetFields: rel.TargetFields, SourcePkg: m.Pkg, SourceModel: m.Key, SourceFields: rel.SourceFields, str: str}
}
func (r *Reference) String() string {
return r.str
}
func (r *Reference) Debug() string {
return fmt.Sprintf("%s: [%s] -> %s[%s]", r.Key, strings.Join(r.TargetFields, ", "), r.SourceModel, strings.Join(r.SourceFields, ", "))
}
type References []*Reference
func (s References) Get(key string) *Reference {
return lo.FindOrElse(s, nil, func(x *Reference) bool {
return x.Key == key
})
}