-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
131 lines (106 loc) · 2.81 KB
/
client.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package spoon
import (
"github.com/pkg/errors"
)
var (
errIgnoreField = errors.New("error ignore this field")
)
const (
defaultTagPrefix = "db"
defaultIgnoreTag = "-"
)
type optionParam struct {
tagPrefix string
ignoreTag string
}
// Client is Google Cloud Spanner schema generator
type Client struct {
param *optionParam
parser *parser
}
// New creates a Client and returns it.
func New(opts ...Option) (*Client, error) {
op := &optionParam{
tagPrefix: defaultTagPrefix,
ignoreTag: defaultIgnoreTag,
}
for _, opt := range opts {
if err := opt(op); err != nil {
return nil, err
}
}
c := &Client{
parser: newParser(op.tagPrefix, op.ignoreTag),
}
return c, nil
}
// GenerateCreateTable outputs the `CREATE TABLE` schema of the specified Entity as a string.
func (c *Client) GenerateCreateTable(eb EntityBehavior) (string, error) {
t, err := c.parser.Parse(eb)
if err != nil {
return "", err
}
return t.CreateTableSchema(), nil
}
// GenerateCreateTables outputs the `CREATE TABLE` schema of the specified Entity as a string slices.
func (c *Client) GenerateCreateTables(ebs []EntityBehavior) ([]string, error) {
tables, err := c.parser.ParseMulti(ebs)
if err != nil {
return nil, err
}
ss := make([]string, 0, len(ebs))
for i := range tables {
t := tables[i]
ss = append(ss, t.CreateTableSchema())
}
return ss, nil
}
// GenerateDropTable outputs the `DROP TABLE` schema of the specified Entity as a string.
func (c *Client) GenerateDropTable(eb EntityBehavior) (string, error) {
t, err := c.parser.Parse(eb)
if err != nil {
return "", err
}
return t.DropTableSchema(), nil
}
// GenerateDropTables outputs the `DROP TABLE` schema of the specified Entity as a string slices.
func (c *Client) GenerateDropTables(ebs []EntityBehavior) ([]string, error) {
tables, err := c.parser.ParseMulti(ebs)
if err != nil {
return nil, err
}
ss := make([]string, 0, len(ebs))
for i := range tables {
t := tables[i]
ss = append(ss, t.DropTableSchema())
}
return ss, nil
}
// GenerateCreateIndexes outputs the `CREATE INDEX` schema of the specified Entity as a string slices.
func (c *Client) GenerateCreateIndexes(eb EntityBehavior) ([]string, error) {
t, err := c.parser.Parse(eb)
if err != nil {
return nil, err
}
indexes := t.Indexes()
ss := make([]string, 0, len(indexes))
for i := range indexes {
idx := indexes[i]
ss = append(ss, idx.CreateIndexSchema())
}
return ss, nil
}
// GenerateDropIndexes outputs the `DROP INDEX` schema of the specified Entity as a string slices.
func (c *Client) GenerateDropIndexes(eb EntityBehavior) ([]string, error) {
t, err := c.parser.Parse(eb)
if err != nil {
return nil, err
}
indexes := t.Indexes()
ss := make([]string, 0, len(indexes))
for i := range indexes {
idx := indexes[i]
ss = append(ss, idx.DropIndexSchema())
}
return ss, nil
}