-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference.go
49 lines (41 loc) · 1.24 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
49
// Content managed by Project Forge, see [projectforge.md] for details.
package model
import (
"fmt"
"strings"
"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 := make([]string, 0, len(rel.SourceFields))
for _, x := range rel.SourceFields {
_, col := m.Fields.Get(x)
if col != nil {
fields = append(fields, col.Name())
}
}
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 {
for _, x := range s {
if x.Key == key {
return x
}
}
return nil
}