From 7585257f9ea11aa3c57ce8d200c7bfec297a49d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8C=85=E5=AD=90?= Date: Tue, 8 Jun 2021 16:53:08 +0800 Subject: [PATCH] modify proto-gen-go-errors version and func name (#1018) * modify proto-gen-go-errors version and func name --- cmd/protoc-gen-go-errors/errors.go | 12 ++++++++++++ cmd/protoc-gen-go-errors/go.mod | 4 ++-- cmd/protoc-gen-go-errors/go.sum | 2 ++ cmd/protoc-gen-go-errors/template.go | 11 ++++++----- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/cmd/protoc-gen-go-errors/errors.go b/cmd/protoc-gen-go-errors/errors.go index ff4a6bf2b..1d66d36d6 100644 --- a/cmd/protoc-gen-go-errors/errors.go +++ b/cmd/protoc-gen-go-errors/errors.go @@ -4,6 +4,7 @@ import ( pb "github.com/go-kratos/kratos/v2/errors" "google.golang.org/protobuf/compiler/protogen" "google.golang.org/protobuf/proto" + "strings" ) const ( @@ -63,9 +64,20 @@ func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene err := &errorInfo{ Name: string(enum.Desc.Name()), Value: string(v.Desc.Name()), + CamelValue: Case2Camel(string(v.Desc.Name())), HttpCode: enumCode, } ew.Errors = append(ew.Errors, err) } g.P(ew.execute()) } + +func Case2Camel(name string) string { + if !strings.Contains(name,"_") { + return name + } + name = strings.ToLower(name) + name = strings.Replace(name, "_", " ", -1) + name = strings.Title(name) + return strings.Replace(name, " ", "", -1) +} \ No newline at end of file diff --git a/cmd/protoc-gen-go-errors/go.mod b/cmd/protoc-gen-go-errors/go.mod index e8e023e06..b0f2b1d30 100644 --- a/cmd/protoc-gen-go-errors/go.mod +++ b/cmd/protoc-gen-go-errors/go.mod @@ -3,6 +3,6 @@ module github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2 go 1.15 require ( - github.com/go-kratos/kratos/v2 v2.0.0-beta2 + github.com/go-kratos/kratos/v2 v2.0.0-20210608032802-c551448bbd64 google.golang.org/protobuf v1.26.0 -) \ No newline at end of file +) diff --git a/cmd/protoc-gen-go-errors/go.sum b/cmd/protoc-gen-go-errors/go.sum index 2ce48ad09..74e17b32b 100644 --- a/cmd/protoc-gen-go-errors/go.sum +++ b/cmd/protoc-gen-go-errors/go.sum @@ -10,6 +10,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-kratos/kratos/v2 v2.0.0-20210608032802-c551448bbd64 h1:+x/Zgv46tYhyhdWgoASZRWQpwuaEoHtNz0PMfwT9svM= +github.com/go-kratos/kratos/v2 v2.0.0-20210608032802-c551448bbd64/go.mod h1:puDEhRv+dqKa4ZdlInEAbO8ibGxqNhAXFjGleIRh9V4= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= diff --git a/cmd/protoc-gen-go-errors/template.go b/cmd/protoc-gen-go-errors/template.go index ff92e8b67..72be3a9f8 100644 --- a/cmd/protoc-gen-go-errors/template.go +++ b/cmd/protoc-gen-go-errors/template.go @@ -8,7 +8,7 @@ import ( var errorsTemplate = ` {{ range .Errors }} -func Is{{.Value}}(err error) bool { +func Is{{.CamelValue}}(err error) bool { e := errors.FromError(err) if e.Reason == {{.Name}}_{{.Value}}.String() && e.Code == {{.HttpCode}} { return true @@ -16,7 +16,7 @@ func Is{{.Value}}(err error) bool { return false } -func Error{{.Value}}(format string, args ...interface{}) *errors.Error { +func Error{{.CamelValue}}(format string, args ...interface{}) *errors.Error { return errors.New({{.HttpCode}}, {{.Name}}_{{.Value}}.String(), fmt.Sprintf(format, args...)) } @@ -24,9 +24,10 @@ func Error{{.Value}}(format string, args ...interface{}) *errors.Error { ` type errorInfo struct { - Name string - Value string - HttpCode int + Name string + Value string + HttpCode int + CamelValue string } type errorWrapper struct { Errors []*errorInfo