modify proto-gen-go-errors version and func name (#1018)

* modify proto-gen-go-errors version and func name
pull/1019/head
包子 3 years ago committed by GitHub
parent 0834128e57
commit 7585257f9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      cmd/protoc-gen-go-errors/errors.go
  2. 4
      cmd/protoc-gen-go-errors/go.mod
  3. 2
      cmd/protoc-gen-go-errors/go.sum
  4. 11
      cmd/protoc-gen-go-errors/template.go

@ -4,6 +4,7 @@ import (
pb "github.com/go-kratos/kratos/v2/errors" pb "github.com/go-kratos/kratos/v2/errors"
"google.golang.org/protobuf/compiler/protogen" "google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"strings"
) )
const ( const (
@ -63,9 +64,20 @@ func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene
err := &errorInfo{ err := &errorInfo{
Name: string(enum.Desc.Name()), Name: string(enum.Desc.Name()),
Value: string(v.Desc.Name()), Value: string(v.Desc.Name()),
CamelValue: Case2Camel(string(v.Desc.Name())),
HttpCode: enumCode, HttpCode: enumCode,
} }
ew.Errors = append(ew.Errors, err) ew.Errors = append(ew.Errors, err)
} }
g.P(ew.execute()) 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)
}

@ -3,6 +3,6 @@ module github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2
go 1.15 go 1.15
require ( 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 google.golang.org/protobuf v1.26.0
) )

@ -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/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/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/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/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/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=

@ -8,7 +8,7 @@ import (
var errorsTemplate = ` var errorsTemplate = `
{{ range .Errors }} {{ range .Errors }}
func Is{{.Value}}(err error) bool { func Is{{.CamelValue}}(err error) bool {
e := errors.FromError(err) e := errors.FromError(err)
if e.Reason == {{.Name}}_{{.Value}}.String() && e.Code == {{.HttpCode}} { if e.Reason == {{.Name}}_{{.Value}}.String() && e.Code == {{.HttpCode}} {
return true return true
@ -16,7 +16,7 @@ func Is{{.Value}}(err error) bool {
return false 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...)) 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 { type errorInfo struct {
Name string Name string
Value string Value string
HttpCode int HttpCode int
CamelValue string
} }
type errorWrapper struct { type errorWrapper struct {
Errors []*errorInfo Errors []*errorInfo

Loading…
Cancel
Save