Repository: tableauio/tableau
Pull request #179 (2025-02-26 07:24:50)

protogen: add option EnumValueWithPrefix to prefixing enum value
See **Enums** part of https://protobuf.dev/programming-guides/style/

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/tableauio/tableau/pull/179?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tableauio) Report
Attention: Patch coverage is `77.77778%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 63.58%. Comparing base [(`cb8b269`)](https://app.codecov.io/gh/tableauio/tableau/commit/cb8b26957cf209ae9c52cfba3951aca5d40bbeea?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tableauio) to head [(`9969450`)](https://app.codecov.io/gh/tableauio/tableau/commit/99694509485a5247e2dec65cdf4cf54d0b3a5b55?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tableauio).

| [Files with missing lines](https://app.codecov.io/gh/tableauio/tableau/pull/179?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tableauio) | Patch % | Lines |
|---|---|---|
| [internal/protogen/protogen.go](https://app.codecov.io/gh/tableauio/tableau/pull/179?src=pr&el=tree&filepath=internal%2Fprotogen%2Fprotogen.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tableauio#diff-aW50ZXJuYWwvcHJvdG9nZW4vcHJvdG9nZW4uZ28=) | 0.00% | [0 Missing and 2 partials :warning: ](https://app.codecov.io/gh/tableauio/tableau/pull/179?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tableauio) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #179      +/-   ##
==========================================
+ Coverage   63.56%   63.58%   +0.01%     
==========================================
  Files          75       75              
  Lines       10191    10196       +5     
==========================================
+ Hits         6478     6483       +5     
  Misses       3106     3106              
  Partials      607      607              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/tableauio/tableau/pull/179?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tableauio).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tableauio).


Patch:
diff --git a/internal/protogen/protogen.go b/internal/protogen/protogen.go
index 75ba645..bfef5be 100644
--- a/internal/protogen/protogen.go
+++ b/internal/protogen/protogen.go
@@ -605,7 +605,7 @@ func (gen *Generator) parseSpecialSheetMode(mode tableaupb.Mode, ws *internalpb.
 	// parse each special sheet mode
 	switch mode {
 	case tableaupb.Mode_MODE_ENUM_TYPE:
-		if err := parseEnumType(ws, sheet, parser); err != nil {
+		if err := parseEnumType(ws, sheet, parser, gen.OutputOpt.EnumValueWithPrefix); err != nil {
 			return nil, err
 		}
 		return []*internalpb.Worksheet{ws}, nil
@@ -628,7 +628,7 @@ func (gen *Generator) parseSpecialSheetMode(mode tableaupb.Mode, ws *internalpb.
 				block, blockEndRow := sheet.Table.ExtractBlock(blockBeginRow)
 				row = blockEndRow // skip row to next block
 				subSheet := book.NewTableSheet(subWs.Name, block)
-				if err := parseEnumType(subWs, subSheet, parser); err != nil {
+				if err := parseEnumType(subWs, subSheet, parser, gen.OutputOpt.EnumValueWithPrefix); err != nil {
 					return nil, err
 				}
 				worksheets = append(worksheets, subWs)
diff --git a/internal/protogen/sheet_mode.go b/internal/protogen/sheet_mode.go
index 969f185..098434a 100644
--- a/internal/protogen/sheet_mode.go
+++ b/internal/protogen/sheet_mode.go
@@ -5,6 +5,7 @@ import (
 	"strings"
 
 	"github.com/tableauio/tableau/internal/importer/book"
+	"github.com/tableauio/tableau/internal/strcase"
 	"github.com/tableauio/tableau/internal/types"
 	"github.com/tableauio/tableau/internal/x/xproto"
 	"github.com/tableauio/tableau/proto/tableaupb"
@@ -42,19 +43,24 @@ func isEnumTypeBlockHeader(cols []string) bool {
 	return false
 }
 
-func parseEnumType(ws *internalpb.Worksheet, sheet *book.Sheet, parser book.SheetParser) error {
+func parseEnumType(ws *internalpb.Worksheet, sheet *book.Sheet, parser book.SheetParser, withPrefix bool) error {
 	desc := &internalpb.EnumDescriptor{}
 	if err := parser.Parse(desc, sheet); err != nil {
 		return xerrors.Wrapf(err, "failed to parse enum type sheet (block): %s", sheet.Name)
 	}
+	prefix := strcase.ToScreamingSnake(ws.Name) + "_"
 	for i, value := range desc.Values {
 		number := int32(i + 1)
 		if value.Number != nil {
 			number = *value.Number
 		}
+		name := value.Name
+		if withPrefix && !strings.HasPrefix(name, prefix) {
+			name = prefix + name
+		}
 		field := &internalpb.Field{
 			Number: number,
-			Name:   value.Name,
+			Name:   name,
 			Alias:  value.Alias,
 		}
 		ws.Fields = append(ws.Fields, field)
diff --git a/options/options.go b/options/options.go
index 135a974..e028c27 100644
--- a/options/options.go
+++ b/options/options.go
@@ -148,6 +148,17 @@ type ProtoOutputOption struct {
 	//
 	// Default: nil.
 	FileOptions map[string]string `yaml:"fileOptions"`
+
+	// EnumValueWithPrefix specifies whether to prepend prefix
+	// "UPPER_SNAKE_CASE of EnumType" to each enum value name.
+	//
+	// If set, the enum value name is prepended with "ENUM_TYPE_". For example:
+	// enum ItemType has a value "EQUIP", then converted to "ITEM_TYPE_EQUIP".
+	// If the enum value name is already prefixed with "ENUM_TYPE_", then it will
+	// not be prefixed again.
+	//
+	// Default: false.
+	EnumValueWithPrefix bool `yaml:"enumValueWithPrefix"`
 }
 
 // Options for generating conf files. Only for confgen.


Test patch:
diff --git a/test/dev/proto/hero_conf.proto b/test/dev/proto/hero_conf.proto
index b35b7c3..466b6e0 100644
--- a/test/dev/proto/hero_conf.proto
+++ b/test/dev/proto/hero_conf.proto
@@ -1,4 +1,4 @@
-// Code generated by tableau (protogen v0.6.1). DO NOT EDIT.
+// Code generated by tableau (protogen v0.7.0). DO NOT EDIT.
 // clang-format off
 
 syntax = "proto3";
diff --git a/test/dev/proto/list_conf.proto b/test/dev/proto/list_conf.proto
index df08c99..4488e7a 100644
--- a/test/dev/proto/list_conf.proto
+++ b/test/dev/proto/list_conf.proto
@@ -1,4 +1,4 @@
-// Code generated by tableau (protogen v0.6.1). DO NOT EDIT.
+// Code generated by tableau (protogen v0.7.0). DO NOT EDIT.
 // clang-format off
 
 syntax = "proto3";
diff --git a/test/dev/proto/map_conf.proto b/test/dev/proto/map_conf.proto
index 05c92ad..0de1b65 100644
--- a/test/dev/proto/map_conf.proto
+++ b/test/dev/proto/map_conf.proto
@@ -1,4 +1,4 @@
-// Code generated by tableau (protogen v0.6.1). DO NOT EDIT.
+// Code generated by tableau (protogen v0.7.0). DO NOT EDIT.
 // clang-format off
 
 syntax = "proto3";
diff --git a/test/dev/proto/metasheet_conf.proto b/test/dev/proto/metasheet_conf.proto
index b1318ed..e3cf416 100644
--- a/test/dev/proto/metasheet_conf.proto
+++ b/test/dev/proto/metasheet_conf.proto
@@ -1,4 +1,4 @@
-// Code generated by tableau (protogen v0.6.1). DO NOT EDIT.
+// Code generated by tableau (protogen v0.7.0). DO NOT EDIT.
 // clang-format off
 
 syntax = "proto3";
diff --git a/test/dev/proto/test_conf.proto b/test/dev/proto/test_conf.proto
index 7c81ab4..4cfa867 100644
--- a/test/dev/proto/test_conf.proto
+++ b/test/dev/proto/test_conf.proto
@@ -1,4 +1,4 @@
-// Code generated by tableau (protogen v0.6.1). DO NOT EDIT.
+// Code generated by tableau (protogen v0.7.0). DO NOT EDIT.
 // clang-format off
 
 syntax = "proto3";
diff --git a/test/functest/testdata/excel/metasheet/SheetMode#EnumType.csv b/test/functest/testdata/excel/metasheet/SheetMode#EnumType.csv
index 6391765..ec72cd0 100644
--- a/test/functest/testdata/excel/metasheet/SheetMode#EnumType.csv
+++ b/test/functest/testdata/excel/metasheet/SheetMode#EnumType.csv
@@ -14,6 +14,6 @@ Number,Name,Alias
 ,,
 BirdType,BirdType note,
 Number,Name,Alias
-1,BIRD_TYPE_CANARY,Canary
-2,BIRD_TYPE_WOODPECKER,Woodpecker
-3,BIRD_TYPE_OWL,Owl
+1,CANARY,Canary
+2,WOODPECKER,Woodpecker
+3,OWL,Owl
diff --git a/test/functest/util.go b/test/functest/util.go
index 55b9606..0bf3fe5 100644
--- a/test/functest/util.go
+++ b/test/functest/util.go
@@ -72,6 +72,7 @@ func genProto(logLevel string) error {
 					FileOptions: map[string]string{
 						"go_package": "github.com/tableauio/tableau/test/functest/protoconf",
 					},
+					EnumValueWithPrefix: true,
 				},
 			},
 		),