1 Star 0 Fork 0

iotas/toolkit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
inject_tag.go 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
xuthus 提交于 4年前 . fix
package proto_parser
import (
"fmt"
"github.com/emicklei/proto"
"regexp"
"strings"
)
func injectTagMessage(msg *proto.Message) {
for _, element := range msg.Elements {
if field, ok := element.(*proto.NormalField); ok {
if field.Comment == nil {
continue
}
field.Comment.Lines = mergeInjectTag(field.Comment.Lines)
}
if field, ok := element.(*proto.Message); ok {
injectTagMessage(field)
}
}
}
func mergeInjectTag(doc []string) []string {
var res []string
if len(doc) == 0 {
return res
}
// 去重map
var dupMap = make(map[string]string)
var hasTag bool
var newLineTag = "@gotags:"
for _, d := range doc {
if strings.Contains(d, "@gotags:") {
hasTag = true
var reg = regexp.MustCompile(`@gotags:\s*(.*)\s?`)
rules := reg.FindAllStringSubmatch(d, -1)
if len(rules) > 0 && len(rules[0]) == 2 {
val := rules[0][1]
valkey := strings.Split(val, ":")[0]
if _, ok := dupMap[valkey]; !ok {
newLineTag = fmt.Sprintf("%s %s", newLineTag, rules[0][1])
}
dupMap[valkey] = val
continue
}
continue
}
res = append(res, d)
}
if hasTag {
res = append(res, newLineTag)
}
return res
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/iotas/toolkit.git
git@gitee.com:iotas/toolkit.git
iotas
toolkit
toolkit
6a2c4ec13c63

搜索帮助