-
Notifications
You must be signed in to change notification settings - Fork 800
/
filter.go
190 lines (166 loc) · 4.96 KB
/
filter.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package dynamicconfig
import (
"encoding/json"
"fmt"
"github.com/uber/cadence/common/types"
)
// Filter represents a filter on the dynamic config key
type Filter int
func (f Filter) String() string {
if f <= UnknownFilter || f > WorkflowID {
return filters[UnknownFilter]
}
return filters[f]
}
func ParseFilter(filterName string) Filter {
switch filterName {
case "domainName":
return DomainName
case "domainID":
return DomainID
case "taskListName":
return TaskListName
case "taskType":
return TaskType
case "shardID":
return ShardID
case "clusterName":
return ClusterName
case "workflowID":
return WorkflowID
case "workflowType":
return WorkflowType
default:
return UnknownFilter
}
}
var filters = []string{
"unknownFilter",
"domainName",
"domainID",
"taskListName",
"taskType",
"shardID",
"clusterName",
"workflowID",
"workflowType",
}
const (
UnknownFilter Filter = iota
// DomainName is the domain name
DomainName
// DomainID is the domain id
DomainID
// TaskListName is the tasklist name
TaskListName
// TaskType is the task type (0:Decision, 1:Activity)
TaskType
// ShardID is the shard id
ShardID
// ClusterName is the cluster name in a multi-region setup
ClusterName
// WorkflowID is the workflow id
WorkflowID
// WorkflowType is the workflow type name
WorkflowType
// LastFilterTypeForTest must be the last one in this const group for testing purpose
LastFilterTypeForTest
)
// FilterOption is used to provide filters for dynamic config keys
type FilterOption func(filterMap map[Filter]interface{})
// TaskListFilter filters by task list name
func TaskListFilter(name string) FilterOption {
return func(filterMap map[Filter]interface{}) {
filterMap[TaskListName] = name
}
}
// DomainFilter filters by domain name
func DomainFilter(name string) FilterOption {
return func(filterMap map[Filter]interface{}) {
filterMap[DomainName] = name
}
}
// DomainIDFilter filters by domain id
func DomainIDFilter(domainID string) FilterOption {
return func(filterMap map[Filter]interface{}) {
filterMap[DomainID] = domainID
}
}
// TaskTypeFilter filters by task type
func TaskTypeFilter(taskType int) FilterOption {
return func(filterMap map[Filter]interface{}) {
filterMap[TaskType] = taskType
}
}
// ShardIDFilter filters by shard id
func ShardIDFilter(shardID int) FilterOption {
return func(filterMap map[Filter]interface{}) {
filterMap[ShardID] = shardID
}
}
// ClusterNameFilter filters by cluster name
func ClusterNameFilter(clusterName string) FilterOption {
return func(filterMap map[Filter]interface{}) {
filterMap[ClusterName] = clusterName
}
}
// WorkflowIDFilter filters by workflowID
func WorkflowIDFilter(workflowID string) FilterOption {
return func(filterMap map[Filter]interface{}) {
filterMap[WorkflowID] = workflowID
}
}
// WorkflowType filters by workflow type name
func WorkflowTypeFilter(name string) FilterOption {
return func(filterMap map[Filter]interface{}) {
filterMap[WorkflowType] = name
}
}
// ToGetDynamicConfigFilterRequest generates a GetDynamicConfigRequest object
// by converting filters to DynamicConfigFilter objects and setting values
func ToGetDynamicConfigFilterRequest(configName string, filters []FilterOption) *types.GetDynamicConfigRequest {
filterMap := make(map[Filter]interface{}, len(filters))
for _, opt := range filters {
opt(filterMap)
}
var dcFilters []*types.DynamicConfigFilter
for f, entity := range filterMap {
filter := &types.DynamicConfigFilter{
Name: f.String(),
}
data, err := json.Marshal(entity)
if err != nil {
fmt.Errorf("could not marshall entity: %s", err)
}
encodingType := types.EncodingTypeJSON
filter.Value = &types.DataBlob{
EncodingType: &encodingType,
Data: data,
}
dcFilters = append(dcFilters, filter)
}
request := &types.GetDynamicConfigRequest{
ConfigName: configName,
Filters: dcFilters,
}
return request
}