Skip to content

Commit

Permalink
confgen(merger/scatter): auto find the primary workbook if only input…
Browse files Browse the repository at this point in the history
… a secondary workbook
  • Loading branch information
wenchy committed Mar 11, 2024
1 parent 432085d commit 819b377
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
13 changes: 7 additions & 6 deletions internal/confgen/confgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ func (gen *Generator) GenWorkbook(bookSpecifiers ...string) error {
return errors.Wrapf(err, "parse book specifier failed: %s", specifier)
}
relCleanSlashPath := fs.CleanSlashPath(bookName)
if err != nil {
return errors.Wrapf(err, "get clean slash path failed: %s", bookName)
}
log.Debugf("convert relWorkbookPath to relCleanSlashPath: %s -> %s", bookName, relCleanSlashPath)
primaryBookIndexInfo, ok := bookIndexes[relCleanSlashPath]
if !ok {
Expand All @@ -118,9 +115,13 @@ func (gen *Generator) GenWorkbook(bookSpecifiers ...string) error {
}
return errors.Errorf("primary workbook not found: %s, protoPaths: %v", relCleanSlashPath, gen.InputOpt.ProtoPaths)
}
eg.Go(func() error {
return gen.convert(prFiles, primaryBookIndexInfo.fd, sheetName)
})
// NOTE: one book maybe relates to multiple primary books
for _, fd := range primaryBookIndexInfo.books {
fd := fd
eg.Go(func() error {
return gen.convert(prFiles, fd, sheetName)
})
}
}
return eg.Wait()
}
Expand Down
21 changes: 16 additions & 5 deletions internal/confgen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ func parseBookSpecifier(bookSpecifier string) (bookName string, sheetName string
}

type bookIndexInfo struct {
primaryBookName string
fd protoreflect.FileDescriptor
books map[string]protoreflect.FileDescriptor // primary book name -> fd
}

// buildWorkbookIndex builds the secondary workbook name (including self) -> primary workbook info indexes.
Expand All @@ -152,7 +151,12 @@ func buildWorkbookIndex(protoPackage protoreflect.FullName, inputDir string, sub
}
// add self: rewrite subdir
rewrittenWorkbookName := fs.RewriteSubdir(workbook.Name, subdirRewrites)
bookIndexes[rewrittenWorkbookName] = &bookIndexInfo{primaryBookName: workbook.Name, fd: fd}
if bookIndexes[rewrittenWorkbookName] == nil {
bookIndexes[rewrittenWorkbookName] = &bookIndexInfo{
books: make(map[string]protoreflect.FileDescriptor),
}
}
bookIndexes[rewrittenWorkbookName].books[workbook.Name] = fd
// merger or scatter (only one can be set at once)
msgs := fd.Messages()
for i := 0; i < msgs.Len(); i++ {
Expand All @@ -173,7 +177,12 @@ func buildWorkbookIndex(protoPackage protoreflect.FullName, inputDir string, sub
return false
}
for relBookPath := range relBookPaths {
bookIndexes[relBookPath] = &bookIndexInfo{primaryBookName: workbook.Name, fd: fd}
if bookIndexes[relBookPath] == nil {
bookIndexes[relBookPath] = &bookIndexInfo{
books: make(map[string]protoreflect.FileDescriptor),
}
}
bookIndexes[relBookPath].books[workbook.Name] = fd
}
}
}
Expand All @@ -184,7 +193,9 @@ func buildWorkbookIndex(protoPackage protoreflect.FullName, inputDir string, sub
return nil, err
}
for k, v := range bookIndexes {
log.Debugf("primary book index: %s -> %s", k, v.primaryBookName)
for primaryBookName := range v.books {
log.Debugf("primary book index: %s -> %s", k, primaryBookName)
}
}
return bookIndexes, nil
}
Expand Down

0 comments on commit 819b377

Please sign in to comment.