Skip to content

Commit

Permalink
feat: ignore load errors when loading old hub (#6)
Browse files Browse the repository at this point in the history
* feat: ingore load errors when loading old hub

* feat: gen test code

* feat: return load erros after Check or CheckCompatibility

---------

Co-authored-by: zinozhang <[email protected]>
Co-authored-by: wenchy <[email protected]>
  • Loading branch information
3 people authored May 20, 2024
1 parent 7317619 commit 8e7a3de
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 24 deletions.
58 changes: 47 additions & 11 deletions cmd/protoc-gen-go-tableau-checker/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ func (h *Hub) load(dir string, filter tableau.Filter, format format.Format, opti
var mu sync.Mutex // guard msgers
msgers := tableau.MessagerMap{}
var loadOpts []load.Option
loadOpts = append(loadOpts, load.SubdirRewrites(opts.SubdirRewrites))
if opts.IgnoreUnknownFields {
loadOpts = append(loadOpts, load.IgnoreUnknownFields())
}
var errsMu sync.Mutex
var errs []error
var eg errgroup.Group
for name, msger := range h.filteredCheckerMap {
name := name
Expand All @@ -85,7 +87,15 @@ func (h *Hub) load(dir string, filter tableau.Filter, format format.Format, opti
log.Infof("=== LOAD %s", name)
if err := msger.Messager().Load(dir, format, loadOpts...); err != nil {
bookName, sheetName := getBookAndSheet(opts.ProtoPackage, name)
return fmt.Errorf("error: workbook %s, worksheet %s, load failed: %+v\n", bookName, sheetName, err)
//lint:ignore ST1005 we want to prettify multiple error messages
err := fmt.Errorf("error: workbook %s, worksheet %s, load failed: %+v\n", bookName, sheetName, err)
if opts.SkipLoadErrors {
errsMu.Lock()
errs = append(errs, err)
errsMu.Unlock()
return nil
}
return err
}
log.Infof("--- DONE: %v", name)
Expand All @@ -99,7 +109,7 @@ func (h *Hub) load(dir string, filter tableau.Filter, format format.Format, opti
return err
}
h.SetMessagerMap(msgers)
return nil
return errors.Join(errs...)
}
func getBookAndSheet(protoPackage, msgName string) (bookName string, sheetName string) {
Expand Down Expand Up @@ -176,24 +186,29 @@ func (h *Hub) checkCompatibility(newHub *tableau.Hub, protoPackage string, break
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
loadErr := h.load(dir, filter, format, options...)
if loadErr != nil && !opts.SkipLoadErrors {
return loadErr
}
return h.check(opts.ProtoPackage, opts.BreakFailedCount)
checkErr := h.check(opts.ProtoPackage, opts.BreakFailedCount)
return errors.Join(loadErr, checkErr)
}
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
loadErr := h.load(dir, filter, format, options...);
if loadErr != nil && !opts.SkipLoadErrors {
return loadErr
}
// load new hub
newHub := NewHub()
if err := newHub.load(newDir, filter, format, options...); err != nil {
return err
loadErr1 := newHub.load(newDir, filter, format, options...)
if loadErr1 != nil && !opts.SkipLoadErrors {
return loadErr1
}
return h.checkCompatibility(newHub.Hub, opts.ProtoPackage, opts.BreakFailedCount)
checkErr := h.checkCompatibility(newHub.Hub, opts.ProtoPackage, opts.BreakFailedCount)
return errors.Join(loadErr, loadErr1, checkErr)
}
func register(gen tableau.MessagerGenerator) {
Expand All @@ -202,17 +217,31 @@ func register(gen tableau.MessagerGenerator) {
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
// Whether to ignore errors during loading.
//
// Errors may occur during loading old config files when do compatibility
// check. For example, some new worksheets you recently add are not
// existed, or proto schema are not compatible, just ignore the loading
// errors (then these proto message objects are nil after loading), so that
// compatibility check can continue to run.
//
// Default: false.
SkipLoadErrors bool
}
// Option is the functional option type.
Expand Down Expand Up @@ -246,6 +275,13 @@ func IgnoreUnknownFields() Option {
}
}
// SkipLoadErrors sets SkipLoadErrors option as true.
func SkipLoadErrors() Option {
return func(opts *Options) {
opts.SkipLoadErrors = true
}
}
// newDefault returns a default Options.
func newDefault() *Options {
return &Options{
Expand Down
58 changes: 47 additions & 11 deletions test/check/hub.check.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/check/item_conf.check.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/check/test_conf.check.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8e7a3de

Please sign in to comment.