-
Notifications
You must be signed in to change notification settings - Fork 2
/
hub.check.go
262 lines (234 loc) · 7.31 KB
/
hub.check.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package devcheck
import (
tableau "github.com/tableauio/checker/test/protoconf/tableau"
"fmt"
"sync"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
"github.com/pkg/errors"
"github.com/tableauio/tableau/format"
"github.com/tableauio/tableau/load"
"github.com/tableauio/tableau/log"
"github.com/tableauio/tableau/proto/tableaupb"
)
var registrarSingleton *tableau.Registrar
var once sync.Once
func getRegistrar() *tableau.Registrar {
once.Do(func() {
registrarSingleton = tableau.NewRegistrar()
})
return registrarSingleton
}
type Hub struct {
*tableau.Hub
filteredCheckerMap tableau.MessagerMap
}
func NewHub() *Hub {
return &Hub{
Hub: tableau.NewHub(),
filteredCheckerMap: tableau.MessagerMap{},
}
}
func (h *Hub) load(dir string, filter tableau.Filter, format format.Format, options ...Option) error {
opts := ParseOptions(options...)
filteredCheckerMap := h.NewMessagerMap(filter)
for name, gen := range registrarSingleton.Generators {
if filter == nil || filter.Filter(name) {
filteredCheckerMap[name] = gen()
}
}
h.filteredCheckerMap = filteredCheckerMap
var mu sync.Mutex // guard msgers
msgers := tableau.MessagerMap{}
var eg errgroup.Group
for name, msger := range h.filteredCheckerMap {
name := name
msger := msger
eg.Go(func() error {
log.Infof("=== LOAD %s", name)
if err := msger.Messager().Load(dir, format,
load.SubdirRewrites(opts.SubdirRewrites),
load.IgnoreUnknownFields(opts.IgnoreUnknownFields)); err != nil {
bookName, sheetName := getBookAndSheet(opts.ProtoPackage, name)
log.Errorf("--- FAIL: workbook %s, worksheet %s", bookName, sheetName)
log.Errorf("load error:%+v, workbook %s, worksheet %s", err, bookName, sheetName)
return errors.WithMessagef(err, "failed to load %v", name)
}
log.Infof("--- DONE: %v", name)
mu.Lock()
msgers[name] = msger.Messager()
mu.Unlock()
return nil
})
}
if err := eg.Wait(); err != nil {
log.Errorf("--- FAIL: load failed: %v", err)
return err
}
h.SetMessagerMap(msgers)
return nil
}
func getBookAndSheet(protoPackage, msgName string) (bookName string, sheetName string) {
fullName := protoreflect.FullName(protoPackage + "." + msgName)
mt, err := protoregistry.GlobalTypes.FindMessageByName(fullName)
if err != nil {
log.Errorf("failed to find messager %s: %+v", fullName, err)
return "", ""
}
mopts := mt.Descriptor().Options()
worksheet := proto.GetExtension(mopts, tableaupb.E_Worksheet).(*tableaupb.WorksheetOptions)
fd := mt.Descriptor().ParentFile()
opts := fd.Options().(*descriptorpb.FileOptions)
workbook := proto.GetExtension(opts, tableaupb.E_Workbook).(*tableaupb.WorkbookOptions)
return workbook.GetName(), worksheet.GetName()
}
func (h *Hub) check(protoPackage string, breakFailedCount int) int {
failedCount := 0
for name, checker := range h.filteredCheckerMap {
log.Infof("=== RUN %v", name)
// built-in auto-generated check logic
err1 := checker.Messager().Check(h.Hub)
// custom check logic
err2 := checker.Check(h.Hub)
if err1 != nil || err2 != nil {
bookName, sheetName := getBookAndSheet(protoPackage, name)
log.Errorf("--- FAIL: workbook %s, worksheet %s", bookName, sheetName)
if err1 != nil {
log.Errorf("auto check error: %+v, workbook %s, worksheet %s", err1, bookName, sheetName)
}
if err2 != nil {
log.Errorf("custom check error: %+v, workbook %s, worksheet %s", err2, bookName, sheetName)
}
failedCount++
} else {
log.Infof("--- PASS: %v", name)
}
if failedCount != 0 && failedCount >= breakFailedCount {
break
}
}
return failedCount
}
func (h *Hub) checkCompatibility(newHub *tableau.Hub, protoPackage string, breakFailedCount int) int {
failedCount := 0
for name, checker := range h.filteredCheckerMap {
log.Infof("=== RUN %v", name)
// built-in auto-generated check logic
err1 := checker.Messager().Check(h.Hub)
// custom check logic
err2 := checker.CheckCompatibility(h.Hub, newHub)
if err1 != nil || err2 != nil {
bookName, sheetName := getBookAndSheet(protoPackage, name)
log.Errorf("--- FAIL: workbook %s, worksheet %s", bookName, sheetName)
if err1 != nil {
log.Errorf("auto check error: %+v, workbook %s, worksheet %s", err1, bookName, sheetName)
}
if err2 != nil {
log.Errorf("custom check error: %+v, workbook %s, worksheet %s", err2, bookName, sheetName)
}
failedCount++
} else {
log.Infof("--- PASS: %v", name)
}
if failedCount != 0 && failedCount >= breakFailedCount {
break
}
}
return failedCount
}
func (h *Hub) Check(dir string, filter tableau.Filter, format format.Format, options ...Option) error {
opts := ParseOptions(options...)
// load hub
if err := h.load(dir, filter, format, options...); err != nil {
return err
}
// check
failedCount := h.check(opts.ProtoPackage, opts.BreakFailedCount)
if failedCount != 0 {
return fmt.Errorf("Check failed count: %d", failedCount)
}
return nil
}
func (h *Hub) CheckCompatibility(dir, newDir string, filter tableau.Filter, format format.Format, options ...Option) error {
opts := ParseOptions(options...)
// load hub
if err := h.load(dir, filter, format, options...); err != nil {
return err
}
// load new hub
newHub := NewHub()
if err := newHub.load(newDir, filter, format, options...); err != nil {
return err
}
// check
failedCount := h.checkCompatibility(newHub.Hub, opts.ProtoPackage, opts.BreakFailedCount)
if failedCount != 0 {
return fmt.Errorf("Check failed count: %d", failedCount)
}
return nil
}
func register(name string, gen tableau.MessagerGenerator) {
getRegistrar().Register(name, gen)
}
type Options struct {
// Break check loop if failed count is equal to or more than BreakFailedCount.
// Default: 1.
BreakFailedCount int
// Rewrite subdir path (relative to workbook name option in .proto file).
// Default: nil.
SubdirRewrites map[string]string
// The proto package name of .proto files.
// Default: "protoconf".
ProtoPackage string
// Whether to ignore unknown JSON fields during parsing.
// Default: false.
IgnoreUnknownFields bool
}
// Option is the functional option type.
type Option func(*Options)
// BreakFailedCount sets BreakFailedCount option.
func BreakFailedCount(count int) Option {
return func(opts *Options) {
opts.BreakFailedCount = count
}
}
// SubdirRewrites sets SubdirRewrites option.
func SubdirRewrites(subdirRewrites map[string]string) Option {
return func(opts *Options) {
opts.SubdirRewrites = subdirRewrites
}
}
// ProtoPackage sets ProtoPackage option.
func ProtoPackage(protoPackage string) Option {
return func(opts *Options) {
opts.ProtoPackage = protoPackage
}
}
// IgnoreUnknownFields sets IgnoreUnknownFields option as true.
func IgnoreUnknownFields() Option {
return func(opts *Options) {
opts.IgnoreUnknownFields = true
}
}
// newDefault returns a default Options.
func newDefault() *Options {
return &Options{
BreakFailedCount: 1,
ProtoPackage: "protoconf",
}
}
// ParseOptions parses functional options and merge them to default Options.
func ParseOptions(setters ...Option) *Options {
// Default Options
opts := newDefault()
for _, setter := range setters {
setter(opts)
}
if opts.BreakFailedCount <= 1 {
opts.BreakFailedCount = 1
}
return opts
}