Skip to content

Commit

Permalink
feat: protoc-gen-go-errors add comment (#1961)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyBool authored May 5, 2022
1 parent 9098e9d commit 245f55d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/protoc-gen-go-errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,27 @@ func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene
if enumCode == 0 {
continue
}

comment := v.Comments.Leading.String()
if comment == "" {
comment = v.Comments.Trailing.String()
}

err := &errorInfo{
Name: string(enum.Desc.Name()),
Value: string(v.Desc.Name()),
CamelValue: case2Camel(string(v.Desc.Name())),
HTTPCode: enumCode,
Comment: comment,
HasComment: len(comment) > 0,
}
ew.Errors = append(ew.Errors, err)
}
if len(ew.Errors) == 0 {
return true
}
g.P(ew.execute())

return false
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/protoc-gen-go-errors/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
var errorsTemplate = `
{{ range .Errors }}
{{if .HasComment}}{{.Comment}}{{end}}
func Is{{.CamelValue}}(err error) bool {
if err == nil {
return false
Expand All @@ -16,6 +17,7 @@ func Is{{.CamelValue}}(err error) bool {
return e.Reason == {{.Name}}_{{.Value}}.String() && e.Code == {{.HTTPCode}}
}
{{if .HasComment}}{{.Comment}}{{end}}
func Error{{.CamelValue}}(format string, args ...interface{}) *errors.Error {
return errors.New({{.HTTPCode}}, {{.Name}}_{{.Value}}.String(), fmt.Sprintf(format, args...))
}
Expand All @@ -28,6 +30,8 @@ type errorInfo struct {
Value string
HTTPCode int
CamelValue string
Comment string
HasComment bool
}

type errorWrapper struct {
Expand Down

0 comments on commit 245f55d

Please sign in to comment.