-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter.go
128 lines (109 loc) · 2.78 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
package minimatch
import (
"fmt"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
pb "github.com/castaneai/minimatch/gen/openmatch"
)
type filteredEntity interface {
GetId() string
GetSearchFields() *pb.SearchFields
GetCreateTime() *timestamppb.Timestamp
}
// base: https://github.com/googleforgames/open-match/blob/98e7a02ebf9e470e746265a212d9770ca353267a/internal/filter/filter.go
type poolFilter struct {
DoubleRangeFilters []*pb.DoubleRangeFilter
StringEqualsFilters []*pb.StringEqualsFilter
TagPresentFilters []*pb.TagPresentFilter
CreatedBefore time.Time
CreatedAfter time.Time
}
func newPoolFilter(pool *pb.Pool) (*poolFilter, error) {
var ca, cb time.Time
var err error
if pool.GetCreatedBefore() != nil {
if err = pool.GetCreatedBefore().CheckValid(); err != nil {
return nil, fmt.Errorf("invalid created_before value")
}
cb = pool.GetCreatedBefore().AsTime()
}
if pool.GetCreatedAfter() != nil {
if err = pool.GetCreatedAfter().CheckValid(); err != nil {
return nil, fmt.Errorf("invalid created_after value")
}
ca = pool.GetCreatedAfter().AsTime()
}
return &poolFilter{
DoubleRangeFilters: pool.GetDoubleRangeFilters(),
StringEqualsFilters: pool.GetStringEqualsFilters(),
TagPresentFilters: pool.GetTagPresentFilters(),
CreatedBefore: cb,
CreatedAfter: ca,
}, nil
}
func (pf *poolFilter) In(entity filteredEntity) bool {
s := entity.GetSearchFields()
if s == nil {
s = &pb.SearchFields{}
}
if !pf.CreatedAfter.IsZero() || !pf.CreatedBefore.IsZero() {
// CreateTime is only populated by Open Match and hence expected to be valid.
if err := entity.GetCreateTime().CheckValid(); err == nil {
ct := entity.GetCreateTime().AsTime()
if !pf.CreatedAfter.IsZero() {
if !ct.After(pf.CreatedAfter) {
return false
}
}
if !pf.CreatedBefore.IsZero() {
if !ct.Before(pf.CreatedBefore) {
return false
}
}
}
}
for _, f := range pf.DoubleRangeFilters {
v, ok := s.DoubleArgs[f.DoubleArg]
if !ok {
return false
}
switch f.Exclude {
case pb.DoubleRangeFilter_NONE:
// Not simplified so that NaN cases are handled correctly.
if !(v >= f.Min && v <= f.Max) {
return false
}
case pb.DoubleRangeFilter_MIN:
if !(v > f.Min && v <= f.Max) {
return false
}
case pb.DoubleRangeFilter_MAX:
if !(v >= f.Min && v < f.Max) {
return false
}
case pb.DoubleRangeFilter_BOTH:
if !(v > f.Min && v < f.Max) {
return false
}
}
}
for _, f := range pf.StringEqualsFilters {
v, ok := s.StringArgs[f.StringArg]
if !ok {
return false
}
if f.Value != v {
return false
}
}
outer:
for _, f := range pf.TagPresentFilters {
for _, v := range s.Tags {
if v == f.Tag {
continue outer
}
}
return false
}
return true
}