1 Star 0 Fork 0

东海苍月/aithu-protoc

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
template.go 957 Bytes
一键复制 编辑 原始数据 按行查看 历史
东海苍月 提交于 2022-01-24 17:58 +08:00 . feat: 适配v2错误结构 #0000
package main
import (
"bytes"
"text/template"
)
var errorsTemplate = `
{{ range .Errors }}
func Is{{.CamelValue}}(err error) bool {
if err == nil {
return false
}
e := errors.FromError(err)
return e.Reason == {{.Name}}_{{.Value}}.String() && e.Code == "{{.Code}}"
}
{{.Deprecated}}
func {{.CamelValue}}() *errors.Error {
return errors.New("{{.Code}}", {{.Name}}_{{.Value}}.String(), "{{.Message}}")
}
{{- end }}
`
type errorInfo struct {
Name string // 枚举名称
Value string // 协议名称
Code string // code码
Message string // 错误提示
Deprecated string // 废弃标识
CamelValue string // 函数名称
}
type errorWrapper struct {
Errors []*errorInfo
}
func (e *errorWrapper) execute() string {
buf := new(bytes.Buffer)
tmpl, err := template.New("errors").Parse(errorsTemplate)
if err != nil {
panic(err)
}
if err := tmpl.Execute(buf, e); err != nil {
panic(err)
}
return buf.String()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dhcy/aithu-protoc.git
git@gitee.com:dhcy/aithu-protoc.git
dhcy
aithu-protoc
aithu-protoc
dd4f76fc9910

搜索帮助