-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
85 lines (66 loc) · 1.65 KB
/
database.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
package api
import (
"context"
"fmt"
"strings"
"time"
"unicode/utf8"
"github.com/moonrhythm/validator"
)
type Database interface {
Create(context.Context, *DatabaseCreate) (*Empty, error)
}
type DatabaseCreate struct {
Project string
Location string
Name string
}
func (m *DatabaseCreate) Valid() error {
m.Name = strings.TrimSpace(m.Name)
v := validator.New()
v.Must(m.Project != "", "project required")
v.Must(m.Location != "", "location required")
v.Must(ReValidName.MatchString(m.Name), "name invalid "+ReValidNameStr)
{
cnt := utf8.RuneCountInString(m.Name)
v.Mustf(cnt >= MinNameLength && cnt <= MaxNameLength, "name must have length between %d-%d characters", MinNameLength, MaxNameLength)
}
return WrapValidate(v)
}
type DatabaseList struct {
Project string `json:"project" yaml:"project"`
Location string `json:"location" yaml:"location"`
}
func (m *DatabaseList) Valid() error {
v := validator.New()
v.Must(m.Project != "", "project required")
return WrapValidate(v)
}
type DatabaseListResult struct {
Items []*DatabaseItem
}
type DatabaseItem struct {
ID int64
ProjectID int64
Location string
Name string
Status Status
Action string
CreatedAt time.Time
CreatedBy string
SuccessAt time.Time
}
type DatabaseDelete struct {
Project string
Name string
}
func (m *DatabaseDelete) Valid() error {
m.Name = strings.TrimSpace(m.Name)
v := validator.New()
v.Must(ReValidName.MatchString(m.Name), "name invalid "+ReValidNameStr)
if cnt := utf8.RuneCountInString(m.Name); cnt > MaxNameLength {
return fmt.Errorf("name invalid")
}
v.Must(m.Project != "", "project required")
return WrapValidate(v)
}