代码拉取完成,页面将自动刷新
package main
import (
"fmt"
"strings"
"time"
"google.golang.org/protobuf/compiler/protogen"
)
const (
errorsPackage = protogen.GoImportPath("gitee.com/dhcy/go-protobuf/errors")
enumNameRequired = "REASON" // 枚举命名需要带有字段
enumValueMinxLength = 4
enumModuleNumKey = "+module="
dateFormat = "2006-01-02 15:04:05"
)
// generateFile generates a _errors.pb.go file containing go-protobuf errors definitions.
func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
if len(file.Enums) == 0 {
return nil
}
filename := file.GeneratedFilenamePrefix + "_errors.pb.go"
g := gen.NewGeneratedFile(filename, file.GoImportPath)
g.P("// Code generated by protoc-gen-go-errors. DO NOT EDIT.")
g.P(strings.Join([]string{"// protoc-gen-go-errors:", release}, " "))
g.P(strings.Join([]string{"// build-date:", time.Now().Format(dateFormat)}, " "))
g.P()
g.P("package ", file.GoPackageName)
g.P()
generateFileContent(gen, file, g)
return g
}
// generateFileContent generates the go-protobuf errors definitions, excluding the package statement.
func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile) {
if len(file.Enums) == 0 {
return
}
g.P("// This is a compile-time assertion to ensure that this generated file")
g.P("// is compatible with the go-protobuf package it is being compiled against.")
g.P("const _ = ", errorsPackage.Ident("SupportPackageIsVersion1"))
g.P()
index := 0
for _, enum := range file.Enums {
skip := genErrorsReason(gen, file, g, enum)
if !skip {
index++
}
}
// If all enums do not contain 'errors.code', the current file is skipped
if index == 0 {
g.Skip()
}
}
func genErrorsReason(gen *protogen.Plugin, file *protogen.File,
g *protogen.GeneratedFile, enum *protogen.Enum) bool {
// 匹配符合规则的枚举
name := string(enum.Desc.Name())
nameUpper := strings.ToUpper(name)
if !strings.Contains(nameUpper, enumNameRequired) {
return true
}
nameModule := strings.Replace(nameUpper, enumNameRequired, "", -1)
moduleNum := getNotModuleNum(enum)
// 对module进行表
var ew errorWrapper
for _, v := range enum.Values {
// 枚举值 如:INFO_SYSTEM_CACHE_TIMEOUT
enumValue := string(v.Desc.Name())
values := strings.Split(enumValue, "_")
if len(values) < enumValueMinxLength {
continue
}
// module
module := strings.ToUpper(values[2])
// 检查当前枚举中module命名
if module != nameModule {
panic(fmt.Sprintf("enmu %s name not equal to the module name %s err %s != %s",
enum.Desc.Name(), enumValue, nameModule, module))
}
// 获取注释message
message := string(v.Comments.Leading)
if len(message) == 0 {
panic(fmt.Errorf("enmu %s no valid comment %s, please check", enum.Desc.Name(), enumValue))
}
message = strings.Replace(message, "//", "", -1)
message = strings.Replace(message, "\n", "", -1)
message = strings.Replace(message, " ", "", -1)
// typ
typ := strings.ToUpper(values[0])
// source
source := strings.ToUpper(values[1])
// code
code := buildErrorCode(typ, source, moduleNum, int32(v.Desc.Number()))
err := &errorInfo{
Name: string(enum.Desc.Name()),
Value: enumValue,
Code: code,
Message: message,
CamelValue: case2Camel(enumValue),
}
ew.Errors = append(ew.Errors, err)
}
if len(ew.Errors) == 0 {
return true
}
g.P(ew.execute())
return false
}
func case2Camel(name string) string {
if !strings.Contains(name, "_") {
upperName := strings.ToUpper(name)
if upperName == name {
name = strings.ToLower(name)
}
return strings.Title(name)
}
name = strings.ToLower(name)
name = strings.Replace(name, "_", " ", -1)
name = strings.Title(name)
return strings.Replace(name, " ", "", -1)
}
// getNotModuleNum 获取注释中的模块号(如果没有会panic)
func getNotModuleNum(enum *protogen.Enum) string {
// 获取注释中的模块号
nots := strings.Split(string(enum.Comments.Leading), "\n")
for i := 0; i < len(nots); i++ {
// 匹配是否包含模块key
if strings.Contains(nots[i], enumModuleNumKey) {
moduleNum := strings.Replace(nots[i], enumModuleNumKey, "", -1)
return strings.Replace(moduleNum, " ", "", -1)
}
}
panic(fmt.Sprintf("enmu %s no %s data, please check", enum.Desc.Name(), enumModuleNumKey))
}
// buildErrorCode 组装error code
func buildErrorCode(typ, source, module string, enumNum int32) string {
return fmt.Sprintf("%s%s%s%05d", string(typ[0]), string(source[0]), module, enumNum)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。