Skip to content

Commit

Permalink
Merge pull request #287 from sevennt/feature/errors
Browse files Browse the repository at this point in the history
errors 支持 i18n
  • Loading branch information
sevennt authored May 16, 2022
2 parents 4e13260 + c11ea48 commit 13eee85
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cmd/protoc-gen-go-errors/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,20 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.
const (
fileLevelCommentAnnotation = "plugins"
fieldLevelCommentAnnotation = "code"
fieldLevelI18nAnnotation = "i18n"
)

func generationErrorsSection(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, enum *protogen.Enum) bool {
var ew errorWrapper
for _, v := range enum.Values {
var i18n = map[string]string{}
annos := getAnnotations(string(v.Comments.Leading))
eCode := annos[fieldLevelCommentAnnotation]
for _, v := range annos {
if newName := strings.TrimPrefix(v.name, fieldLevelI18nAnnotation+"."); len(newName) != len(v.name) {
i18n[newName] = v.val
}
}
desc := string(v.Desc.Name())

comment := v.Comments.Leading.String()
Expand All @@ -103,6 +110,7 @@ func generationErrorsSection(gen *protogen.Plugin, file *protogen.File, g *proto
Key: string(v.Desc.FullName()),
Comment: comment,
HasComment: len(comment) > 0,
I18n: i18n,
}
ew.Errors = append(ew.Errors, err)
}
Expand All @@ -123,7 +131,8 @@ func buildComment(upperCamelValue, comment string) string {
return fmt.Sprintf("// %s %s", upperCamelValue, comment)
}

var filedLevelCommentRgx, _ = regexp.Compile(`@(\w+)=([_a-zA-Z0-9-,]+)`)
var filedLevelCommentRgx, _ = regexp.Compile(`@([\w\.]+)=([_a-zA-Z0-9-,]+)`)
var filedLevelCommentQuotedRgx, _ = regexp.Compile(`@([\w\.]+)="(.+)"`)
var fileLevelCommentRgx, _ = regexp.Compile(`@(\w+)=([_a-zA-Z0-9-,]+)`)

type annotation struct {
Expand All @@ -133,17 +142,24 @@ type annotation struct {

func getAnnotations(comment string) map[string]annotation {
matches := filedLevelCommentRgx.FindAllStringSubmatch(comment, -1)
return findMatchesFromComments(matches)
quotedMatches := filedLevelCommentQuotedRgx.FindAllStringSubmatch(comment, -1)
return findMatchesFromComments(matches, quotedMatches)
}

func findMatchesFromComments(matches [][]string) map[string]annotation {
func findMatchesFromComments(matches [][]string, quotedMatches [][]string) map[string]annotation {
annotations := make(map[string]annotation)
for _, v := range matches {
annotations[v[1]] = annotation{
name: v[1],
val: v[2],
}
}
for _, v := range quotedMatches {
annotations[v[1]] = annotation{
name: v[1],
val: v[2],
}
}
return annotations
}

Expand All @@ -153,7 +169,7 @@ func getFileLevelAnnotations(locs []*descriptorpb.SourceCodeInfo_Location) map[s
comments += loc.String()
}
matches := fileLevelCommentRgx.FindAllStringSubmatch(comments, -1)
return findMatchesFromComments(matches)
return findMatchesFromComments(matches, nil)
}

func needGenerate(locs []*descriptorpb.SourceCodeInfo_Location) bool {
Expand Down
15 changes: 15 additions & 0 deletions cmd/protoc-gen-go-errors/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ const errorsTpl = `
var {{.LowerCamelValue}} *eerrors.EgoError
{{- end }}
var i18n = map[string]map[string]string{
{{- range .Errors }}
"{{.Key}}": map[string]string{
{{- range $k,$v := .I18n }}
"{{$k}}": "{{$v}}",
{{- end }}
},
{{- end }}
}
func ReasonI18n(e *eerrors.EgoError, lan string) string {
return i18n[e.Reason][lan]
}
func init() {
{{- range .Errors }}
{{.LowerCamelValue}} = eerrors.New(int(codes.{{.Code}}), "{{.Key}}", {{.Name}}_{{.Value}}.String())
Expand All @@ -57,6 +71,7 @@ type errorInfo struct {
Key string
Comment string
HasComment bool
I18n map[string]string
}

type errorWrapper struct {
Expand Down

0 comments on commit 13eee85

Please sign in to comment.