-
Notifications
You must be signed in to change notification settings - Fork 0
/
change_search.go
107 lines (102 loc) · 2.8 KB
/
change_search.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
package data
import (
"fmt"
"strconv"
"strings"
)
type ChangeSearch struct {
ChangeIds []string `json:"change_ids,omitempty"`
DataIds []string `json:"data_ids,omitempty"`
Types []string `json:"types,omitempty"`
Actions []string `json:"actions,omitempty"`
ServiceNames []string `json:"service_names,omitempty"`
LatestVersion *bool `json:"latest_version,omitempty"`
Since *int64 `json:"since,string,omitempty"`
}
func (c *ChangeSearch) ToParams() string {
const parameterf string = "%s=%s"
var parameters []string
if len(c.ChangeIds) > 0 {
parameters = append(parameters,
fmt.Sprintf(parameterf, ParameterChangeIds, strings.Join(c.ChangeIds, ",")))
}
if len(c.DataIds) > 0 {
parameters = append(parameters,
fmt.Sprintf(parameterf, ParameterDataIds, strings.Join(c.DataIds, ",")))
}
if len(c.Types) > 0 {
parameters = append(parameters,
fmt.Sprintf(parameterf, ParameterTypes, strings.Join(c.Types, ",")))
}
if len(c.ServiceNames) > 0 {
parameters = append(parameters,
fmt.Sprintf(parameterf, ParameterServiceNames, strings.Join(c.ServiceNames, ",")))
}
if len(c.Actions) > 0 {
parameters = append(parameters,
fmt.Sprintf(parameterf, ParameterActions, strings.Join(c.Actions, ",")))
}
if c.LatestVersion != nil {
parameters = append(parameters, fmt.Sprint(*c.LatestVersion))
}
if c.Since != nil {
parameters = append(parameters, fmt.Sprint(*c.Since))
}
return "?" + strings.Join(parameters, "&")
}
func (c *ChangeSearch) FromParams(params map[string][]string) {
for key, value := range params {
switch strings.ToLower(key) {
case ParameterActions:
for _, value := range value {
if value != "" {
c.Actions = append(c.Actions, strings.Split(value, ",")...)
}
}
case ParameterChangeIds:
for _, value := range value {
if value != "" {
c.ChangeIds = append(c.ChangeIds, strings.Split(value, ",")...)
}
}
case ParameterDataIds:
for _, value := range value {
if value != "" {
c.DataIds = append(c.DataIds, strings.Split(value, ",")...)
}
}
case ParameterTypes:
for _, value := range value {
if value != "" {
c.Types = append(c.Types, strings.Split(value, ",")...)
}
}
case ParameterServiceNames:
for _, value := range value {
if value != "" {
c.ServiceNames = append(c.ServiceNames, strings.Split(value, ",")...)
}
}
case ParameterLatestVersion:
for _, value := range value {
latestVersion, err := strconv.ParseBool(value)
if err != nil {
continue
}
c.LatestVersion = new(bool)
*c.LatestVersion = latestVersion
break
}
case ParameterSince:
for _, value := range value {
since, err := strconv.ParseInt(value, 10, 64)
if err != nil {
continue
}
c.Since = new(int64)
*c.Since = since
break
}
}
}
}