-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.go
141 lines (127 loc) · 2.92 KB
/
filters.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
package main
import (
"encoding/json"
"fmt"
"os"
)
type Filter struct {
Predicates []interface{} `json:"-"` // Filter | Predicate
Operator string `json:"operator"` // and | or
RawPredicates []json.RawMessage `json:"predicates"`
}
type Predicate struct {
Column string `json:"column"`
Test string `json:"test"` // is_empty | is_not_empty
}
func renameQuery(q *Filter, oldCol string, newCol string) *Filter {
newF := Filter{
Operator: q.Operator,
}
for _, predicate := range q.Predicates {
fmt.Printf("Renaming %s to %s for predicate %s\n", oldCol, newCol, predicate)
newF.Predicates = append(newF.Predicates, renameQueryHelper(q, oldCol, newCol))
}
return &newF
}
func renameQueryHelper(q interface{}, oldCol string, newCol string) interface{} {
switch query := q.(type) {
case *Filter:
return renameFilter(query, oldCol, newCol)
case *Predicate:
return renamePredicate(query, oldCol, newCol)
default:
panic("unknown predicate type")
}
}
func renameFilter(q *Filter, oldCol string, newCol string) *Filter {
newQ := Filter{
Operator: q.Operator,
}
for _, predicate := range q.Predicates {
newQ.Predicates = append(newQ.Predicates, renameQueryHelper(predicate, oldCol, newCol))
}
return &newQ
}
func renamePredicate(p *Predicate, oldCol string, newCol string) *Predicate {
if p.Column == oldCol {
return &Predicate{
Column: newCol,
Test: p.Test,
}
} else {
return p
}
}
func main() {
str := `{
"predicates": [
{
"column": "Address",
"test": "is_not_empty"
},
{
"predicates": [
{
"column": "Email",
"test": "is_empty"
},
{
"column": "Phone",
"test": "is_empty"
}
],
"operator": "or"
}
],
"operator": "and"
}`
var input Filter
if err := json.Unmarshal([]byte(str), &input); err != nil {
panic(err)
}
output := renameQuery(&input, "Address", "Line 1")
fmt.Println("Output:")
enc := json.NewEncoder(os.Stdout)
enc.Encode(output)
}
// Helpers for de/serializing polymorphic JSON
func (f *Filter) UnmarshalJSON(b []byte) error {
type filter Filter
err := json.Unmarshal(b, (*filter)(f))
if err != nil {
return err
}
for _, raw := range f.RawPredicates {
var p map[string]interface{}
err = json.Unmarshal(raw, &p)
if err != nil {
panic(err)
}
var i interface{}
if _, ok := p["column"]; ok {
i = &Predicate{}
} else {
i = &Filter{}
}
err = json.Unmarshal(raw, i)
if err != nil {
panic(err)
}
f.Predicates = append(f.Predicates, i)
}
return nil
}
func (f *Filter) MarshalJSON() ([]byte, error) {
type filter Filter
if f.Predicates != nil {
f.RawPredicates = nil
for _, p := range f.Predicates {
b, err := json.Marshal(p)
if err != nil {
panic(err)
}
f.RawPredicates = append(f.RawPredicates, b)
}
}
return json.Marshal((*filter)(f))
}