Skip to content
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
22 changes: 14 additions & 8 deletions internal/confgen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,22 @@ func loadProtoRegistryFiles(protoPackage string, protoPaths []string, protoFiles
return protoc.NewFiles(protoPaths, protoFiles, excludeProtoFiles...)
}

// parseOutputFormats parses the output formats of the specified message.
func parseOutputFormats(msg proto.Message, opt *options.ConfOutputOption) []format.Format {
messagerName := string(msg.ProtoReflect().Descriptor().Name())
if formats, ok := opt.MessagerFormats[messagerName]; ok && len(formats) != 0 {
return formats
}
if formats := opt.Formats; len(formats) != 0 {
return formats
}
return format.OutputFormats
}

// storeMessage stores a message to one or multiple file formats.
func storeMessage(msg proto.Message, name, locationName, outputDir string, opt *options.ConfOutputOption) error {
outputDir = filepath.Join(outputDir, opt.Subdir)
formats := format.OutputFormats
if len(opt.Formats) != 0 {
formats = opt.Formats
}
formats := parseOutputFormats(msg, opt)
for _, fmt := range formats {
err := store.Store(msg, outputDir, fmt,
store.Name(name),
Expand All @@ -289,10 +298,7 @@ func storeMessage(msg proto.Message, name, locationName, outputDir string, opt *
// formats. It will not emit unpopulated fields for clear reading.
func storePatchMergeMessage(msg proto.Message, name, locationName, outputDir string, opt *options.ConfOutputOption) error {
outputDir = filepath.Join(outputDir, opt.Subdir)
formats := format.OutputFormats
if len(opt.Formats) != 0 {
formats = opt.Formats
}
formats := parseOutputFormats(msg, opt)
for _, fmt := range formats {
err := store.Store(msg, outputDir, fmt,
store.Name(name),
Expand Down
56 changes: 56 additions & 0 deletions internal/confgen/util_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package confgen

import (
"reflect"
"testing"

"github.com/tableauio/tableau/format"
Expand Down Expand Up @@ -132,3 +133,58 @@ func Test_storeMessage(t *testing.T) {
})
}
}

func Test_parseOutputFormats(t *testing.T) {
type args struct {
msg proto.Message
opt *options.ConfOutputOption
}
tests := []struct {
name string
args args
want []format.Format
}{
{
name: "default",
args: args{
msg: &unittestpb.ItemConf{},
opt: &options.ConfOutputOption{},
},
want: []format.Format{format.JSON, format.Bin, format.Text},
},
{
name: "global",
args: args{
msg: &unittestpb.ItemConf{},
opt: &options.ConfOutputOption{
Formats: []format.Format{format.Bin},
MessagerFormats: map[string][]format.Format{
"TaskConf": {format.JSON},
},
},
},
want: []format.Format{format.Bin},
},
{
name: "messager-level",
args: args{
msg: &unittestpb.ItemConf{},
opt: &options.ConfOutputOption{
Formats: []format.Format{format.Bin},
MessagerFormats: map[string][]format.Format{
"TaskConf": {format.JSON},
"ItemConf": {format.Text},
},
},
},
want: []format.Format{format.Text},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseOutputFormats(tt.args.msg, tt.args.opt); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseOutputFormats() = %v, want %v", got, tt.want)
}
})
}
}
6 changes: 6 additions & 0 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ type ConfOutputOption struct {
// Default: nil.
Formats []format.Format

// Specify generated conf file formats in messager level. This overrides
// Formats option.
//
// Default: nil.
MessagerFormats map[string][]format.Format `yaml:"messagerFormats"`

// Output pretty format of JSON and Text, with multiline and indent.
//
// Default: false.
Expand Down
Loading