feat: protoc-gen-go-errors add comment (#1961)

pull/1973/head
JeffreyBool 3 years ago committed by GitHub
parent 9098e9dd39
commit 245f55d3ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      cmd/protoc-gen-go-errors/errors.go
  2. 4
      cmd/protoc-gen-go-errors/template.go

@ -82,11 +82,19 @@ func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene
if enumCode == 0 { if enumCode == 0 {
continue continue
} }
comment := v.Comments.Leading.String()
if comment == "" {
comment = v.Comments.Trailing.String()
}
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())), CamelValue: case2Camel(string(v.Desc.Name())),
HTTPCode: enumCode, HTTPCode: enumCode,
Comment: comment,
HasComment: len(comment) > 0,
} }
ew.Errors = append(ew.Errors, err) ew.Errors = append(ew.Errors, err)
} }
@ -94,6 +102,7 @@ func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene
return true return true
} }
g.P(ew.execute()) g.P(ew.execute())
return false return false
} }

@ -8,6 +8,7 @@ import (
var errorsTemplate = ` var errorsTemplate = `
{{ range .Errors }} {{ range .Errors }}
{{if .HasComment}}{{.Comment}}{{end}}
func Is{{.CamelValue}}(err error) bool { func Is{{.CamelValue}}(err error) bool {
if err == nil { if err == nil {
return false return false
@ -16,6 +17,7 @@ func Is{{.CamelValue}}(err error) bool {
return e.Reason == {{.Name}}_{{.Value}}.String() && e.Code == {{.HTTPCode}} return e.Reason == {{.Name}}_{{.Value}}.String() && e.Code == {{.HTTPCode}}
} }
{{if .HasComment}}{{.Comment}}{{end}}
func Error{{.CamelValue}}(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...))
} }
@ -28,6 +30,8 @@ type errorInfo struct {
Value string Value string
HTTPCode int HTTPCode int
CamelValue string CamelValue string
Comment string
HasComment bool
} }
type errorWrapper struct { type errorWrapper struct {

Loading…
Cancel
Save