-
Notifications
You must be signed in to change notification settings - Fork 38
/
generic.go
92 lines (73 loc) · 2.17 KB
/
generic.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
package request
import (
"fmt"
"reflect"
)
type GenericRequest interface {
Common
SetPayload(m map[string]interface{}) error
GetPayload() map[string]interface{}
}
type BaseGenericRequest struct {
CommonBase
payload map[string]interface{}
}
func (r *BaseGenericRequest) SetPayload(m map[string]interface{}) error {
if m["Region"] != nil && reflect.ValueOf(m["Region"]).Type().Kind() != reflect.String {
return fmt.Errorf("request SetPayload error, the Region must set a String value")
}
if m["Zone"] != nil && reflect.ValueOf(m["Zone"]).Type().Kind() != reflect.String {
return fmt.Errorf("request SetPayload error, the Zone must set a String value")
}
if m["Action"] != nil && reflect.ValueOf(m["Action"]).Type().Kind() != reflect.String {
return fmt.Errorf("request SetPayload error, the Action must set a String value")
}
if m["ProjectId"] != nil && reflect.ValueOf(m["ProjectId"]).Type().Kind() != reflect.String {
return fmt.Errorf("request SetPayload error, the ProjectId must set a String value")
}
r.payload = m
return nil
}
func (r BaseGenericRequest) GetPayload() map[string]interface{} {
m := make(map[string]interface{})
if len(r.CommonBase.GetRegion()) != 0 {
m["Region"] = r.CommonBase.GetRegion()
}
if len(r.CommonBase.GetZone()) != 0 {
m["Zone"] = r.CommonBase.GetZone()
}
if len(r.CommonBase.GetAction()) != 0 {
m["Action"] = r.CommonBase.GetAction()
}
if len(r.CommonBase.GetProjectId()) != 0 {
m["ProjectId"] = r.CommonBase.GetProjectId()
}
for k, v := range r.payload {
m[k] = v
}
return m
}
func (r *BaseGenericRequest) GetAction() string {
if r.payload["Action"] != nil {
return r.payload["Action"].(string)
}
return r.CommonBase.GetAction()
}
func (r *BaseGenericRequest) GetRegion() string {
if r.payload["Region"] != nil {
return r.payload["Region"].(string)
}
return r.CommonBase.GetRegion()
}
func (r *BaseGenericRequest) GetZone() string {
if r.payload["Zone"] != nil {
return r.payload["Zone"].(string)
}
return r.CommonBase.GetZone()
}
func (r *BaseGenericRequest) GetProjectId() string {
if r.payload["ProjectId"] != nil {
return r.payload["ProjectId"].(string)
}
return r.CommonBase.GetProjectId()
}