Repository: go-kratos/kratos
Pull request #1712 (2021-12-23 15:16:48)

fix(cmd): fixed a camel word enum error (#1618)
fix https://github.com/go-kratos/kratos/pull/1618

```
enum ErrorReason {
  option (errors.default_code) = 500;

  InvalidParameter = 0 [(errors.code) = 400];
}
```
Originally I got  IsInvalidParameter and ErrorInvalidParameter，but now i got IsInvalidparameter and ErrorInvalidparameter

This change compatible with  UPPER、Camel and Snake


codecov-commenter: # [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1712?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) Report
> Merging [#1712](https://codecov.io/gh/go-kratos/kratos/pull/1712?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (a9b85f9) into [main](https://codecov.io/gh/go-kratos/kratos/commit/0e72cc2dcb4d442433f6332ccdb685f2475529d1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (0e72cc2) will **decrease** coverage by `0.05%`.
> The diff coverage is `n/a`.

> :exclamation: Current head a9b85f9 differs from pull request most recent head 47d7b08. Consider uploading reports for the commit 47d7b08 to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/go-kratos/kratos/pull/1712/graphs/tree.svg?width=650&height=150&src=pr&token=XwnmBTz78x&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)](https://codecov.io/gh/go-kratos/kratos/pull/1712?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)

```diff
@@            Coverage Diff             @@
##             main    #1712      +/-   ##
==========================================
- Coverage   81.28%   81.23%   -0.06%     
==========================================
  Files          78       78              
  Lines        3442     3442              
==========================================
- Hits         2798     2796       -2     
- Misses        442      444       +2     
  Partials      202      202              
```


| [Impacted Files](https://codecov.io/gh/go-kratos/kratos/pull/1712?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) | Coverage Δ | |
|---|---|---|
| [internal/context/context.go](https://codecov.io/gh/go-kratos/kratos/pull/1712/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos#diff-aW50ZXJuYWwvY29udGV4dC9jb250ZXh0Lmdv) | `97.01% <0.00%> (-2.99%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1712?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/1712?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Last update [0e72cc2...47d7b08](https://codecov.io/gh/go-kratos/kratos/pull/1712?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).


Patch:
diff --git a/cmd/protoc-gen-go-errors/errors.go b/cmd/protoc-gen-go-errors/errors.go
index 684310b9..db5e5195 100644
--- a/cmd/protoc-gen-go-errors/errors.go
+++ b/cmd/protoc-gen-go-errors/errors.go
@@ -94,7 +94,11 @@ func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene
 
 func case2Camel(name string) string {
 	if !strings.Contains(name, "_") {
-		return strings.Title(strings.ToLower(name))
+		upperName := strings.ToUpper(name)
+		if upperName == name {
+			name = strings.ToLower(name)
+		}
+		return strings.Title(name)
 	}
 	name = strings.ToLower(name)
 	name = strings.Replace(name, "_", " ", -1)


Test patch:
diff --git a/cmd/protoc-gen-go-errors/errors_test.go b/cmd/protoc-gen-go-errors/errors_test.go
new file mode 100644
index 00000000..e7de565b
--- /dev/null
+++ b/cmd/protoc-gen-go-errors/errors_test.go
@@ -0,0 +1,62 @@
+package main
+
+import "testing"
+
+func Test_case2Camel(t *testing.T) {
+	type args struct {
+		name string
+	}
+	tests := []struct {
+		name string
+		args args
+		want string
+	}{
+		{
+			name: "snake1",
+			args: args{"SYSTEM_ERROR"},
+			want: "SystemError",
+		},
+		{
+			name: "snake2",
+			args: args{"System_Error"},
+			want: "SystemError",
+		},
+		{
+			name: "snake3",
+			args: args{"system_error"},
+			want: "SystemError",
+		},
+		{
+			name: "snake4",
+			args: args{"System_error"},
+			want: "SystemError",
+		},
+		{
+			name: "upper1",
+			args: args{"UNKNOWN"},
+			want: "Unknown",
+		},
+		{
+			name: "camel1",
+			args: args{"SystemError"},
+			want: "SystemError",
+		},
+		{
+			name: "camel2",
+			args: args{"systemError"},
+			want: "SystemError",
+		},
+		{
+			name: "lower1",
+			args: args{"system"},
+			want: "System",
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := case2Camel(tt.args.name); got != tt.want {
+				t.Errorf("case2Camel() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}