<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/pathfilter.go
blob: b64872e98986e15055d331457614ca6f2e5b0b51 (plain)
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
package main

type AnySegmentPathFilter struct {
	next PathFilterState
}
func (filter AnySegmentPathFilter) eat(segment PathSegment) map[PathFilterState]struct{} {
	res := make(map[PathFilterState]struct{})
	res[filter.next] = struct{}{}
	return res
}
func (filter AnySegmentPathFilter) accept() bool {
	return false
}

type GroupPathFilter struct {
	filters []PathFilterState
}
func (filter GroupPathFilter) eat(segment PathSegment) map[PathFilterState]struct{} {
	res := make(map[PathFilterState]struct{})
	for _, f := range filter.filters {
		for r := range f.eat(segment) {
			res[r] = struct{}{}
		}
	}
	return res
}
func (filter GroupPathFilter) accept() bool {
	for _, f := range filter.filters {
		if f.accept() {
			return true
		}
	}
	return false
}

type NonePathFilter struct {}
func (filter NonePathFilter) eat(segment PathSegment) map[PathFilterState]struct{} {
	return make(map[PathFilterState]struct{})
}
func (filter NonePathFilter) accept() bool {
	return true
}

type StringSegmentPathFilter struct {
	index string
	next PathFilterState
}
func (filter StringSegmentPathFilter) eat(segment PathSegment) map[PathFilterState]struct{} {
	s, isString := segment.(string)
	res := make(map[PathFilterState]struct{})
	if isString && s == filter.index {
		res[filter.next] = struct{}{}
	}
	return res
}
func (filter StringSegmentPathFilter) accept() bool {
	return false
}

type IntegerSegmentPathFilter struct {
	index int
	next PathFilterState
}
func (filter IntegerSegmentPathFilter) eat(segment PathSegment) map[PathFilterState]struct{} {
	i, isInteger := segment.(int)
	res := make(map[PathFilterState]struct{})
	if isInteger && i == filter.index {
		res[filter.next] = struct{}{}
	}
	return res
}
func (filter IntegerSegmentPathFilter) accept() bool {
	return false
}

type PathFilterState interface {
	eat(PathSegment) map[PathFilterState]struct{}
	accept() bool
}