Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protogen: support workbook alias in metasheet @TABLEAU #89

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 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 @@ -148,10 +148,10 @@ func (b *Book) ParseMetaAndPurge() (err error) {
}

if len(b.meta.MetasheetMap) == 0 {
// need all sheets except the metasheet "@TABLEAU"
// need all sheets except the MetasheetName and BookNameInMetasheet
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 special sign which represents workbook itself 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
12 changes: 12 additions & 0 deletions internal/protogen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"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,16 @@
return nil
}

// getWorkbookAlias gets the workbook alias from importer.
func getWorkbookAlias(imp importer.Importer) string {
sheetMap := imp.Metabook().GetMetasheetMap()
if sheetMap == nil {
return ""
}

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

View check run for this annotation

Codecov / codecov/patch

internal/protogen/util.go#L68-L69

Added lines #L68 - L69 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."
}
22 changes: 22 additions & 0 deletions test/functest/conf/HorizontalIncellInStructList.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"taskList": [
{
"paramList": [
1
]
},
{
"paramList": [
2,
3
]
},
{
"paramList": [
4,
5,
6
]
}
]
}
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
9 changes: 9 additions & 0 deletions test/functest/proto/excel__list__horizontal_list.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ message HorizontalEnumList {
repeated protoconf.FruitType param_list = 1 [(tableau.field) = {name:"Param" layout:LAYOUT_HORIZONTAL}];
}

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

repeated Task task_list = 1 [(tableau.field) = {name:"Task" layout:LAYOUT_HORIZONTAL}];
message Task {
repeated int32 param_list = 1 [(tableau.field) = {name:"Param" layout:LAYOUT_INCELL}];
}
}

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

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"}];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Task1Param,Task2Param,Task3Param
[Task][]int32,[]int32,[]int32
Task1,Task2,Task3
1,"2,3","4,5,6"
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.
Loading