Skip to content

Commit

Permalink
feat: support workbook alias
Browse files Browse the repository at this point in the history
  • Loading branch information
wenchy committed Mar 11, 2024
1 parent 2e43cc5 commit 3e7b640
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 5 deletions.
9 changes: 6 additions & 3 deletions internal/importer/book/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type Book struct {

// NewBook creates a new book.
// Example:
// - bookName: Test
// - filename: testdata/Test.xlsx
// - bookName: Test
// - filename: testdata/Test.xlsx
func NewBook(bookName, filename string, parser SheetParser) *Book {
return &Book{
name: bookName,
Expand Down Expand Up @@ -151,7 +151,7 @@ func (b *Book) ParseMetaAndPurge() (err error) {
// need all sheets except the metasheet "@TABLEAU"
b.meta.MetasheetMap = make(map[string]*tableaupb.Metasheet) // init
for _, sheet := range b.GetSheets() {
if sheet.Name != MetasheetName {
if sheet.Name != MetasheetName && sheet.Name != BookNameInMetasheet {
b.meta.MetasheetMap[sheet.Name] = &tableaupb.Metasheet{
Sheet: sheet.Name,
}
Expand All @@ -163,6 +163,9 @@ func (b *Book) ParseMetaAndPurge() (err error) {

var keepedSheetNames []string
for sheetName, sheetMeta := range b.meta.MetasheetMap {
if sheetName == BookNameInMetasheet {
continue
}
sheet := b.GetSheet(sheetName)
if sheet == nil {
return xerrors.E0001(sheetName, b.Filename())
Expand Down
6 changes: 5 additions & 1 deletion internal/importer/book/sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ import (
)

// MetasheetName is the name of metasheet which defines the metadata
// of each worksheet. Default metasheet name is "@TABLEAU".
// of each worksheet. Default is "@TABLEAU".
var MetasheetName = "@TABLEAU"

// BookNameInMetasheet is the name which represents workbook in metasheet.
// Default is "#".
const BookNameInMetasheet = "#"

// SetMetasheetName change the metasheet name to the specified name.
//
// NOTE: If will not change MetasheetName value if the specified name
Expand Down
8 changes: 7 additions & 1 deletion internal/protogen/protogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,13 @@ func (gen *Generator) convert(dir, filename string, checkProtoFileConflicts bool
var bp *bookParser
if pass == firstPass {
// create a book parser
bp = newBookParser(imp.BookName(), rewrittenWorkbookName, gen)
bookName := imp.BookName()
alias := getWorkbookAlias(imp)
if alias != "" {
bookName = alias
debugWorkbookName += " (alias: " + alias + ")"
}
bp = newBookParser(bookName, rewrittenWorkbookName, gen)
// cache this new bookParser
gen.addBookParser(absPath, bp)
} else {
Expand Down
11 changes: 11 additions & 0 deletions internal/protogen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"

"github.com/tableauio/tableau/internal/fs"
"github.com/tableauio/tableau/internal/importer"
"github.com/tableauio/tableau/internal/importer/book"
"github.com/tableauio/tableau/options"
"github.com/tableauio/tableau/proto/tableaupb"
"github.com/tableauio/tableau/xerrors"
Expand Down Expand Up @@ -59,6 +61,15 @@ func prepareOutdir(outdir string, importFiles []string, delExisted bool) error {
return nil
}

func getWorkbookAlias(imp importer.Importer) string {
sheetMap := imp.Metabook().GetMetasheetMap()
if sheetMap == nil {
return ""
}

Check warning on line 68 in internal/protogen/util.go

View check run for this annotation

Codecov / codecov/patch

internal/protogen/util.go#L67-L68

Added lines #L67 - L68 were not covered by tests
meta := sheetMap[book.BookNameInMetasheet]
return meta.GetAlias()
}

func getRelCleanSlashPath(rootdir, dir, filename string) (string, error) {
relativeDir, err := filepath.Rel(rootdir, dir)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions test/functest/conf/AliasedSheetScalar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": 1,
"name": "Apple",
"desc": "A kind of delicious fruit."
}
6 changes: 6 additions & 0 deletions test/functest/func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,14 @@ func Test_CompareGeneratedJSON(t *testing.T) {
}
oldPath := filepath.Join(oldConfDir, file.Name())
absOldPath, err := filepath.Abs(oldPath)
if err != nil {
t.Fatal(err)
}
newPath := filepath.Join(newConfDir, file.Name())
absNewPath, err := filepath.Abs(newPath)
if err != nil {
t.Fatal(err)
}
oldData, err := os.ReadFile(oldPath)
if err != nil {
t.Fatal(err)
Expand Down
19 changes: 19 additions & 0 deletions test/functest/proto/excel__metasheet__aliased_workbook.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Code generated by tableau (protogen v0.5.0). DO NOT EDIT.
// clang-format off

syntax = "proto3";

package protoconf;

import "tableau/protobuf/tableau.proto";

option go_package = "github.com/tableauio/tableau/test/functest/protoconf";
option (tableau.workbook) = {name:"excel/metasheet/BookAlias#*.csv"};

message AliasedSheetScalar {
option (tableau.worksheet) = {name:"Scalar" namerow:1 typerow:2 noterow:3 datarow:4};

uint32 id = 1 [(tableau.field) = {name:"ID"}];
string name = 2 [(tableau.field) = {name:"Name"}];
string desc = 3 [(tableau.field) = {name:"Desc"}];
}
3 changes: 3 additions & 0 deletions test/functest/testdata/excel/metasheet/BookAlias#@TABLEAU.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Sheet,Alias
#,AliasedWorkbook
Scalar,AliasedSheetScalar,
4 changes: 4 additions & 0 deletions test/functest/testdata/excel/metasheet/BookAlias#Scalar.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ID,Name,Desc
uint32,string,string
Item's ID,Item's Name,Item's Description
1,Apple,A kind of delicious fruit.

0 comments on commit 3e7b640

Please sign in to comment.