From 16db9c889ae9a9544f4e5fc617f80c0883d30c89 Mon Sep 17 00:00:00 2001 From: longxboy Date: Mon, 23 May 2022 17:49:22 +0800 Subject: [PATCH] fix:fix error camel case (#2032) * fix error camel case --- cmd/protoc-gen-go-errors/errors.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/cmd/protoc-gen-go-errors/errors.go b/cmd/protoc-gen-go-errors/errors.go index 724dc1add..6a0973303 100644 --- a/cmd/protoc-gen-go-errors/errors.go +++ b/cmd/protoc-gen-go-errors/errors.go @@ -3,6 +3,7 @@ package main import ( "fmt" "strings" + "unicode" "golang.org/x/text/cases" "golang.org/x/text/language" @@ -112,8 +113,22 @@ func case2Camel(name string) string { } return enCases.String(name) } - name = strings.ToLower(name) - name = strings.Replace(name, "_", " ", -1) - name = enCases.String(name) - return strings.Replace(name, " ", "", -1) + strs := strings.Split(name, "_") + words := make([]string, 0, len(strs)) + for _, w := range strs { + hasLower := false + for _, r := range w { + if unicode.IsLower(r) { + hasLower = true + break + } + } + if !hasLower { + w = strings.ToLower(w) + } + w = enCases.String(w) + words = append(words, w) + } + + return strings.Join(words, "") }