-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
80 lines (68 loc) · 1.31 KB
/
config.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
78
79
80
package main
import (
"fmt"
"strings"
)
type Config struct {
Package Package `json:"package"`
Models []Model `json:"models"`
}
func (c Config) Valid() bool {
p := c.Package.Valid()
m := true
for _, model := range c.Models {
if !model.Valid() {
m = false
}
}
return p && m
}
type Package struct {
Name string `json:"name"`
Path string `json:"path"`
}
func (p Package) Valid() bool {
hasError := false
if p.Name == "" {
errorLog("package.name is required\n")
hasError = true
}
if p.Path == "" {
errorLog("package.path is required\n")
hasError = true
}
return !hasError
}
type Model struct {
Name string `json:"name"`
Prefix string `json:"prefix"`
Table string `json:"table"`
}
func (m Model) SQLColumn() string {
return fmt.Sprintf("%s.id", m.Table)
}
func (m Model) PrefixStr() string {
if m.Prefix != "" {
return m.Prefix
}
return strings.ToLower(m.Name)
}
func (m Model) TypePrefix() string {
return fmt.Sprintf("%sPrefix", m.Name)
}
func (m Model) TypeID() string {
return fmt.Sprintf("%sID", m.Name)
}
func (m Model) Valid() bool {
hasError := false
if m.Name == "" {
errorLog("model.name is required\n")
hasError = true
}
if m.Prefix == "" {
if p := m.PrefixStr(); p != "" {
warnLog("model.prefix is empty, defaulting to \"%s\"\n", p)
}
}
return !hasError
}