加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
tag.go 1.78 KB
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"bytes"
"fmt"
"sort"
"strconv"
"strings"
)
// Tag represents a single tag.
type Tag struct {
Name string
File string
Address string
Type TagType
Fields map[TagField]string
}
// TagField represents a single field in a tag line.
type TagField string
// Tag fields.
const (
Access TagField = "access"
Signature TagField = "signature"
TypeField TagField = "type"
ReceiverType TagField = "ctype"
Line TagField = "line"
InterfaceType TagField = "ntype"
Language TagField = "language"
ExtraTags TagField = "extraTag"
)
// TagType represents the type of a tag in a tag line.
type TagType string
// Tag types.
const (
Package TagType = "p"
Import TagType = "i"
Constant TagType = "c"
Variable TagType = "v"
Type TagType = "t"
Interface TagType = "n"
Field TagType = "w"
Embedded TagType = "e"
Method TagType = "m"
Constructor TagType = "r"
Function TagType = "f"
)
// NewTag creates a new Tag.
func NewTag(name, file string, line int, tagType TagType) Tag {
l := strconv.Itoa(line)
return Tag{
Name: name,
File: file,
Address: l,
Type: tagType,
Fields: map[TagField]string{Line: l},
}
}
// The tags file format string representation of this tag.
func (t Tag) String() string {
var b bytes.Buffer
b.WriteString(t.Name)
b.WriteByte('\t')
b.WriteString(t.File)
b.WriteByte('\t')
b.WriteString(t.Address)
b.WriteString(";\"\t")
b.WriteString(string(t.Type))
b.WriteByte('\t')
fields := make([]string, 0, len(t.Fields))
i := 0
for k, v := range t.Fields {
if len(v) == 0 {
continue
}
fields = append(fields, fmt.Sprintf("%s:%s", k, v))
i++
}
sort.Sort(sort.StringSlice(fields))
b.WriteString(strings.Join(fields, "\t"))
return b.String()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化