This repository has been archived by the owner on Jan 6, 2022. It is now read-only.
/
instance.go
144 lines (125 loc) 路 2.88 KB
/
instance.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
132
133
134
135
136
137
138
139
140
141
142
143
144
package ProjectVO
import (
"strconv"
"go-app/app/errors/AppError"
"go-app/app/errors/InvalidVOError"
"go-app/service/validator"
)
var (
params = map[string]string{
"name": "name",
"url": "url",
"method": "method",
"json": "json",
"schedule": "schedule",
}
)
// Instance represents ValueObject for Project entity.
type Instance struct {
name string
url string
method string
json string
schedule int // seconds
}
// New gets new ProjectVO instance.
// This ValueObject contains all possible fields
// which may be stored in DataStore.
func New(data map[string]string) Instance {
vo := Instance{}
vo.initFromMap(data)
return vo
}
func (i *Instance) initFromMap(data map[string]string) {
err := InvalidVOError.New()
i.initName(data, err)
i.initURL(data, err)
i.initMethod(data, err)
i.initJSON(data, err)
i.initSchedule(data, err)
if !err.IsEmpty() {
panic(err)
}
}
func (i *Instance) initName(data map[string]string, err *InvalidVOError.Instance) {
v, exists := data[params["name"]]
if !exists {
err.SetError("name", "name is required.")
return
}
if validator.IsProjectName(v) {
i.name = v
} else {
err.SetError("name", "Invalid name.")
}
}
func (i *Instance) initURL(data map[string]string, err *InvalidVOError.Instance) {
v, exists := data[params["url"]]
if !exists {
err.SetError("url", "url is required.")
return
}
if validator.IsURL(v) {
i.url = v
} else {
err.SetError("url", "Invalid url.")
}
}
func (i *Instance) initMethod(data map[string]string, err *InvalidVOError.Instance) {
v, exists := data[params["method"]]
if !exists {
err.SetError("method", "method is required.")
return
}
if validator.IsMethod(v) {
i.method = v
} else {
err.SetError("method", "Invalid method.")
}
}
func (i *Instance) initJSON(data map[string]string, err *InvalidVOError.Instance) {
v, exists := data[params["json"]]
if exists && v != "" {
i.json = v
}
}
func (i *Instance) initSchedule(data map[string]string, err *InvalidVOError.Instance) {
v, exists := data[params["schedule"]]
if !exists {
err.SetError("schedule", "schedule is required.")
return
}
if validator.IsAlpha(v) {
val, er := strconv.Atoi(v)
if er != nil {
AppError.Panic(er)
}
if val > 0 {
i.schedule = val * 60 // from minutes to seconds
} else {
err.SetError("schedule", "Invalid schedule, must be greater than 0.")
}
} else {
err.SetError("schedule", "Invalid schedule.")
}
}
// GetName gets field name value.
func (i Instance) GetName() string {
return i.name
}
// GetURL gets field url value.
func (i Instance) GetURL() string {
return i.url
}
// GetMethod gets field method value.
func (i Instance) GetMethod() string {
return i.method
}
// GetJSON gets field json value.
func (i Instance) GetJSON() string {
return i.json
}
// GetSchedule gets field schedule value.
func (i Instance) GetSchedule() int {
return i.schedule
}