-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
76 lines (64 loc) · 1.91 KB
/
model.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
// Content managed by Project Forge, see [projectforge.md] for details.
package model
import (
"fmt"
"github.com/pkg/errors"
"github.com/samber/lo"
"admini.dev/admini/app/lib/schema/field"
"admini.dev/admini/app/util"
)
type Model struct {
Key string `json:"key"`
Pkg util.Pkg `json:"pkg,omitempty"`
Type Type `json:"type"`
Title string `json:"-"` // override only
Plural string `json:"-"` // override only
Interfaces []string `json:"interfaces,omitempty"`
Fields field.Fields `json:"fields,omitempty"`
Indexes Indexes `json:"indexes,omitempty"`
Relationships Relationships `json:"relationships,omitempty"`
References References `json:"-"` // internal cache
Metadata *Metadata `json:"metadata,omitempty"`
pk []string // internal cache
}
func NewModel(pkg util.Pkg, key string) *Model {
return &Model{Key: key, Pkg: pkg}
}
func (m *Model) String() string {
if len(m.Pkg) == 0 {
return m.Key
}
return m.Pkg.ToPath(m.Key)
}
func (m *Model) Name() string {
if m.Title == "" {
return util.StringToSingular(util.StringToTitle(m.Key))
}
return m.Title
}
func (m *Model) PluralName() string {
if m.Plural == "" {
ret := m.Name()
return util.StringToPlural(ret)
}
return m.Plural
}
func (m *Model) Description() string {
return fmt.Sprintf("%s model [%s]", m.Type.String(), m.Key)
}
func (m *Model) Path() util.Pkg {
return m.Pkg.With(m.Key)
}
func (m *Model) PathString() string {
return m.Pkg.ToPath(m.Key)
}
func (m *Model) OrderedMap(data []any) (*util.OrderedMap[any], error) {
if len(data) != len(m.Fields) {
return nil, errors.Errorf("expected [%d] data elements, found [%d]", len(m.Fields), len(data))
}
ret := util.NewOrderedMap[any](false, len(m.Fields))
lo.ForEach(m.Fields, func(f *field.Field, idx int) {
ret.Append(f.Key, data[idx])
})
return ret, nil
}