<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/subex/filter.go
blob: 309d6c7687f1d1da7fe5f44f14a1521cf04d301b (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
package subex

import (
	"main/walk"
)

type valueFilter interface {
	valueFilter(value walk.Value) bool
}

type selectScalarFilter struct {
	scalar walk.Scalar
}
func (scalar selectScalarFilter) valueFilter(value walk.Value) bool {
	return value == scalar.scalar
}

type anyNumberFilter struct {}
func (_ anyNumberFilter) valueFilter(value walk.Value) bool {
	_, isNumber := value.(walk.NumberValue)
	return isNumber
}

type anyBoolFilter struct {}
func (_ anyBoolFilter) valueFilter(value walk.Value) bool {
	_, isBool := value.(walk.BoolValue)
	return isBool
}

type simpleValueFilter struct {}
func (_ simpleValueFilter) valueFilter(value walk.Value) bool {
	switch value := value.(type) {
	case walk.NullValue:
		return true
	case walk.BoolValue:
		return true
	case walk.NumberValue:
		return true
	case walk.StringValue:
		return true
	case walk.ArrayValue:
		return len(value) == 0
	case walk.MapValue:
		return len(value) == 0
	default:
		panic("Invalid value type")
	}
}

type anyValueFilter struct {}
func (_ anyValueFilter) valueFilter(value walk.Value) bool {
	return true
}

type anyArrayFilter struct {}
func (_ anyArrayFilter) valueFilter(value walk.Value) bool {
	_, isArray := value.(walk.ArrayValue)
	return isArray
}

type anyMapFilter struct {}
func (_ anyMapFilter) valueFilter(value walk.Value) bool {
	_, isMap := value.(walk.MapValue)
	return isMap
}

type anyStringFilter struct {}
func (_ anyStringFilter) valueFilter(value walk.Value) bool {
	_, isString := value.(walk.StringValue)
	return isString
}


type runeFilter interface {
	runeFilter(r rune) bool
}

type anyRuneFilter struct {}
func (_ anyRuneFilter) runeFilter(r rune) bool {
	return true
}

type selectRuneFilter struct {
	r rune
}
func (f selectRuneFilter) runeFilter(r rune) bool {
	return f.r == r
}