<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/filter.go
blob: f69d01a0289129177474b81bf8062726914cd310 (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
80
81
82
83
84
85
86
87
88
89
package main

type PathFilter struct {
	initial PathFilterState
}
func (filter PathFilter) exec(space WalkItem) bool {
	pathFilterState := make(map[PathFilterState]struct{})
	pathFilterState[filter.initial] = struct{}{}
	for _, segment := range space.path {
		nextPathFilterState := make(map[PathFilterState]struct{})
		for curState := range pathFilterState {
			for nextState := range curState.eat(segment) {
				nextPathFilterState[nextState] = struct{}{}
			}
		}
		pathFilterState = nextPathFilterState
	}
	for pathState := range pathFilterState {
		if pathState.accept() {
			return true
		}
	}
	return false
}

type MapTerminalFilter struct {}
func (filter MapTerminalFilter) exec(space WalkItem) bool {
	terminal, isTerminal := space.value.(TerminalValue)
	if !isTerminal {
		return false
	}
	return terminal == MapBegin || terminal == MapEnd
}

type BeginTerminalFilter struct {}
func (filter BeginTerminalFilter) exec(space WalkItem) bool {
	terminal, isTerminal := space.value.(TerminalValue)
	if !isTerminal {
		return false
	}
	return terminal == ArrayBegin || terminal == MapBegin
}

type EndTerminalFilter struct {}
func (filter EndTerminalFilter) exec(space WalkItem) bool {
	terminal, isTerminal := space.value.(TerminalValue)
	if !isTerminal {
		return false
	}
	return terminal == ArrayEnd || terminal == MapEnd
}

type TerminalFilter struct {}
func (filter TerminalFilter) exec(space WalkItem) bool {
	_, isTerminal := space.value.(TerminalValue)
	return isTerminal
}

type RootFilter struct {}
func (filter RootFilter) exec(space WalkItem) bool {
	return len(space.path) == 0
}

type AndFilter struct {
	left Filter
	right Filter
}
func (filter AndFilter) exec(space WalkItem) bool {
	return filter.left.exec(space) && filter.right.exec(space)
}

type OrFilter struct {
	left Filter
	right Filter
}
func (filter OrFilter) exec(space WalkItem) bool {
	return filter.left.exec(space) || filter.right.exec(space)
}

type NotFilter struct {
	content Filter
}
func (filter NotFilter) exec(space WalkItem) bool {
	return !filter.content.exec(space)
}

type Filter interface {
	exec(WalkItem) bool
}