-
Notifications
You must be signed in to change notification settings - Fork 0
/
relationship.go
46 lines (37 loc) · 1.05 KB
/
relationship.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
package model
import (
"fmt"
"strings"
"github.com/samber/lo"
"admini.dev/admini/app/util"
)
type Relationship struct {
Key string `json:"key"`
SourceFields []string `json:"srcFields"`
TargetPkg util.Pkg `json:"tgtPackage"`
TargetModel string `json:"tgtModel"`
TargetFields []string `json:"tgtFields"`
}
func (r *Relationship) String() string {
return fmt.Sprintf("%s: [%s] -> %s[%s]", r.Key, strings.Join(r.SourceFields, ", "), r.TargetModel, strings.Join(r.TargetFields, ", "))
}
func (r *Relationship) Path() string {
return strings.Join(append(r.TargetPkg, r.TargetModel), "/")
}
type Relationships []*Relationship
func (s Relationships) Get(key string) *Relationship {
return lo.FindOrElse(s, nil, func(x *Relationship) bool {
return x.Key == key
})
}
func (m *Model) ApplicableRelations(key string) Relationships {
ret := Relationships{}
lo.ForEach(m.Relationships, func(r *Relationship, _ int) {
lo.ForEach(r.SourceFields, func(sf string, _ int) {
if sf == key {
ret = append(ret, r)
}
})
})
return ret
}