-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathargs.go
77 lines (68 loc) · 2.03 KB
/
args.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
77
package model
import (
"encoding/json"
"github.com/pkg/errors"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/project/export/enum"
"projectforge.dev/projectforge/app/util"
)
type Args struct {
Config util.ValueMap `json:"config,omitempty"`
ConfigFile json.RawMessage `json:"-"`
Enums enum.Enums `json:"enums,omitempty"`
EnumFiles map[string]json.RawMessage `json:"-"`
Models Models `json:"models,omitempty"`
ModelFiles map[string]json.RawMessage `json:"-"`
Groups Groups `json:"groups,omitempty"`
Acronyms []string `json:"acronyms,omitempty"`
GroupsFile json.RawMessage `json:"-"`
Modules []string `json:"-"`
Database string `json:"-"`
}
func (a *Args) HasModule(key string) bool {
return lo.Contains(a.Modules, key)
}
func (a *Args) DBRef() string {
if a.HasModule("readonlydb") {
return "dbRead"
}
return "db"
}
func (a *Args) DatabaseNow() string {
switch a.Database {
case util.DatabaseSQLite:
return "current_timestamp"
case util.DatabaseSQLServer:
return "getdate()"
default:
return "now()"
}
}
func (a *Args) Validate() error {
packages := make(map[string]struct{}, len(a.Models))
err := a.Models.Validate(a.Modules, a.Groups)
if err != nil {
return err
}
for _, m := range a.Models {
for _, rel := range m.Relations {
relTable := a.Models.Get(rel.Table)
if relTable == nil {
return errors.Errorf("relation [%s] refers to missing table [%s]", rel.Name, rel.Table)
}
for _, t := range rel.Tgt {
if relTable.Columns.Get(t) == nil {
return errors.Errorf("relation [%s] references missing target column [%s]", rel.Name, t)
}
}
}
if _, ok := packages[m.Package]; ok {
return errors.Wrap(err, "multiple models are in package ["+m.Package+"]")
}
packages[m.Package] = struct{}{}
}
return nil
}
func (a *Args) Audit(m *Model) bool {
return m.HasTag("audit") && lo.Contains(a.Modules, "audit")
}