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

confgen: support multiple protobuf packages #90

Merged
merged 3 commits into from
Mar 13, 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
4 changes: 0 additions & 4 deletions internal/confgen/confgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ func NewGenerator(protoPackage, indir, outdir string, setters ...options.Option)
}

func NewGeneratorWithOptions(protoPackage, indir, outdir string, opts *options.Options) *Generator {
// TODO: define tableau in package constants.
if protoPackage == "tableau" {
log.Panicf(`proto package can not be "tableau" which is reserved`)
}
g := &Generator{
ProtoPackage: protoPackage,
InputDir: indir,
Expand Down
6 changes: 0 additions & 6 deletions internal/confgen/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/tableauio/tableau/internal/importer/book"
"github.com/tableauio/tableau/internal/types"
"github.com/tableauio/tableau/internal/xproto"
"github.com/tableauio/tableau/log"
"github.com/tableauio/tableau/options"
"github.com/tableauio/tableau/proto/tableaupb"
"github.com/tableauio/tableau/xerrors"
Expand Down Expand Up @@ -341,13 +340,8 @@ func (sp *sheetParser) Parse(protomsg proto.Message, sheet *book.Sheet) error {
// parseFieldOptions is aimed to parse the options of all the fields of a protobuf message.
func (sp *sheetParser) parseFieldOptions(msg protoreflect.Message, rc *book.RowCells, prefix string) (present bool, err error) {
md := msg.Descriptor()
pkg := md.ParentFile().Package()
for i := 0; i < md.Fields().Len(); i++ {
fd := md.Fields().Get(i)
if string(pkg) != sp.ProtoPackage && pkg != "google.protobuf" {
log.Debugf("no need to process package: %v", pkg)
return false, nil
}
err := func() error {
field := parseFieldDescriptor(fd, sp.opts.Sep, sp.opts.Subsep)
defer field.release()
Expand Down
10 changes: 3 additions & 7 deletions internal/protogen/protogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ import (
"google.golang.org/protobuf/reflect/protoregistry"
)

const (
TableauProtoPackage = "tableau"
)

type Generator struct {
ProtoPackage string // protobuf package name.
InputDir string // input dir of workbooks.
Expand Down Expand Up @@ -265,7 +261,7 @@ func (gen *Generator) convert(dir, filename string, checkProtoFileConflicts bool
absPath := filepath.Join(dir, filename)
var imp importer.Importer
if pass == firstPass {
parser := confgen.NewSheetParser(TableauProtoPackage, gen.LocationName, book.MetasheetOptions())
parser := confgen.NewSheetParser(xproto.TableauProtoPackage, gen.LocationName, book.MetasheetOptions())
imp, err = importer.New(absPath, importer.Parser(parser), importer.Mode(importer.Protogen))
if err != nil {
return xerrors.WrapKV(err, xerrors.KeyBookName, absPath)
Expand Down Expand Up @@ -450,7 +446,7 @@ func (gen *Generator) extractTypeInfoFromSpecialSheetMode(mode tableaupb.Mode, s
Namerow: 1,
Datarow: 2,
}
parser := confgen.NewSheetParser(TableauProtoPackage, gen.LocationName, sheetOpts)
parser := confgen.NewSheetParser(xproto.TableauProtoPackage, gen.LocationName, sheetOpts)
// parse each special sheet mode
switch mode {
case tableaupb.Mode_MODE_ENUM_TYPE:
Expand Down Expand Up @@ -529,7 +525,7 @@ func (gen *Generator) parseSpecialSheetMode(mode tableaupb.Mode, ws *tableaupb.W
Namerow: 1,
Datarow: 2,
}
parser := confgen.NewSheetParser(TableauProtoPackage, gen.LocationName, sheetOpts)
parser := confgen.NewSheetParser(xproto.TableauProtoPackage, gen.LocationName, sheetOpts)

// parse each special sheet mode
switch mode {
Expand Down
5 changes: 5 additions & 0 deletions internal/xproto/package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package xproto

const (
TableauProtoPackage = "tableau"
)
3 changes: 2 additions & 1 deletion tableau.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/tableauio/tableau/internal/localizer"
"github.com/tableauio/tableau/internal/protogen"
"github.com/tableauio/tableau/internal/xlsxgen"
"github.com/tableauio/tableau/internal/xproto"
"github.com/tableauio/tableau/log"
"github.com/tableauio/tableau/options"
)
Expand Down Expand Up @@ -92,6 +93,6 @@ func Proto2Excel(protoPackage, indir, outdir string) {

// NewImporter creates a new importer of the specified workbook.
func NewImporter(workbookPath string) (importer.Importer, error) {
parser := confgen.NewSheetParser(protogen.TableauProtoPackage, "", book.MetasheetOptions())
parser := confgen.NewSheetParser(xproto.TableauProtoPackage, "", book.MetasheetOptions())
return importer.New(workbookPath, importer.Parser(parser))
}
28 changes: 28 additions & 0 deletions test/functest/conf/PredefinedCrossCellStruct.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"itemMap": {
"1": {
"id": 1,
"prop": {
"id": 1,
"name": "Apple",
"desc": "A kind of delicious fruit."
}
},
"2": {
"id": 2,
"prop": {
"id": 2,
"name": "Orange",
"desc": ""
}
},
"3": {
"id": 3,
"prop": {
"id": 3,
"name": "",
"desc": ""
}
}
}
}
10 changes: 10 additions & 0 deletions test/functest/proto/excel__struct__struct.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ message InCellStruct {
}
}

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

map<uint32, Item> item_map = 1 [(tableau.field) = {key:"ID" layout:LAYOUT_VERTICAL}];
message Item {
uint32 id = 1 [(tableau.field) = {name:"ID"}];
base.Property prop = 2 [(tableau.field) = {name:"Prop"}];
}
}

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

Expand Down
6 changes: 3 additions & 3 deletions test/functest/testdata/excel/metasheet/BookAlias#@TABLEAU.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Sheet,Alias
#,AliasedWorkbook
Scalar,AliasedSheetScalar,
Sheet,Alias
#,AliasedWorkbook
Scalar,AliasedSheetScalar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ID,PropID,PropName,PropDesc
"map<uint32, Item>",{base.Property}int32,string,string
Item's ID,Property ID,Property name,Property desc
1,1,Apple,A kind of delicious fruit.
2,2,Orange,
3,3,,
Loading