/
filter.go
173 lines (155 loc) · 3.76 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package schema
import (
"fmt"
"strings"
"github.com/minio/pkg/wildcard"
"github.com/pkg/errors"
"github.com/samber/lo"
)
type FilterOption struct {
Include []string
Exclude []string
IncludeLabels []string
Distance int
}
func (s *Schema) Filter(opt *FilterOption) error {
_, excludes, err := s.SepareteTablesThatAreIncludedOrNot(opt)
if err != nil {
return err
}
for _, t := range excludes {
err := excludeTableFromSchema(t.Name, s)
if err != nil {
return errors.Wrap(errors.WithStack(err), fmt.Sprintf("failed to filter table '%s'", t.Name))
}
}
return nil
}
func (s *Schema) SepareteTablesThatAreIncludedOrNot(opt *FilterOption) ([]*Table, []*Table, error) {
i := append(opt.Include, s.NormalizeTableNames(opt.Include)...)
e := append(opt.Exclude, s.NormalizeTableNames(opt.Exclude)...)
includes := []*Table{}
excludes := []*Table{}
for _, t := range s.Tables {
li, mi := matchLength(i, t.Name)
le, me := matchLength(e, t.Name)
ml := matchTableOrColumnLabels(opt.IncludeLabels, t)
switch {
case mi:
if me && li < le {
excludes = append(excludes, t)
continue
}
includes = append(includes, t)
case ml:
if me {
excludes = append(excludes, t)
continue
}
includes = append(includes, t)
case len(opt.Include) == 0 && len(opt.IncludeLabels) == 0:
if me {
excludes = append(excludes, t)
continue
}
includes = append(includes, t)
default:
excludes = append(excludes, t)
}
}
includes2 := []*Table{}
for _, t := range includes {
includes2 = append(includes2, t)
ts, _, err := t.CollectTablesAndRelations(opt.Distance, true)
if err != nil {
return nil, nil, err
}
for _, tt := range ts {
if !lo.ContainsBy(includes, func(t *Table) bool {
return tt.Name == t.Name
}) {
includes2 = append(includes2, tt)
}
}
}
excludes2 := []*Table{}
for _, t := range excludes {
if lo.ContainsBy(includes2, func(tt *Table) bool {
return tt.Name == t.Name
}) {
continue
}
excludes2 = append(excludes2, t)
}
// assert
if len(s.Tables) != len(includes2)+len(excludes2) {
return nil, nil, errors.Errorf("failed to separate tables. expected: %d, actual: %d", len(s.Tables), len(includes2)+len(excludes2))
}
return includes2, excludes2, nil
}
func excludeTableFromSchema(name string, s *Schema) error {
// Tables
tables := []*Table{}
for _, t := range s.Tables {
if t.Name != name {
tables = append(tables, t)
}
for _, c := range t.Columns {
// ChildRelations
childRelations := []*Relation{}
for _, r := range c.ChildRelations {
if r.Table.Name != name && r.ParentTable.Name != name {
childRelations = append(childRelations, r)
}
}
c.ChildRelations = childRelations
// ParentRelations
parentRelations := []*Relation{}
for _, r := range c.ParentRelations {
if r.Table.Name != name && r.ParentTable.Name != name {
parentRelations = append(parentRelations, r)
}
}
c.ParentRelations = parentRelations
}
}
s.Tables = tables
// Relations
relations := []*Relation{}
for _, r := range s.Relations {
if r.Table.Name != name && r.ParentTable.Name != name {
relations = append(relations, r)
}
}
s.Relations = relations
return nil
}
func matchTableOrColumnLabels(il []string, t *Table) bool {
if matchLabels(il, t.Labels) {
return true
}
for _, c := range t.Columns {
if matchLabels(il, c.Labels) {
return true
}
}
return false
}
func matchLabels(il []string, l Labels) bool {
for _, ll := range l {
for _, ill := range il {
if wildcard.MatchSimple(ill, ll.Name) {
return true
}
}
}
return false
}
func matchLength(s []string, e string) (int, bool) {
for _, v := range s {
if wildcard.MatchSimple(v, e) {
return len(strings.ReplaceAll(v, "*", "")), true
}
}
return 0, false
}