1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fields.go 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
package template
import (
"strings"
"github.com/elastic/beats/libbeat/common"
)
var (
defaultType = "keyword"
)
type Fields []Field
func (f Fields) process(path string, esVersion common.Version) common.MapStr {
output := common.MapStr{}
for _, field := range f {
var mapping common.MapStr
field.path = path
field.esVersion = esVersion
// If not type is defined, it assumes keyword
if field.Type == "" {
field.Type = defaultType
}
switch field.Type {
case "ip":
mapping = field.ip()
case "scaled_float":
mapping = field.scaledFloat()
case "half_float":
mapping = field.halfFloat()
case "integer":
mapping = field.integer()
case "text":
mapping = field.text()
case "keyword":
mapping = field.keyword()
case "object":
mapping = field.object()
case "array":
mapping = field.array()
case "group":
var newPath string
if path == "" {
newPath = field.Name
} else {
newPath = path + "." + field.Name
}
mapping = common.MapStr{
"properties": field.Fields.process(newPath, esVersion),
}
default:
mapping = field.other()
}
if len(mapping) > 0 {
output.Put(generateKey(field.Name), mapping)
}
}
return output
}
// HasKey checks if inside fields the given key exists
// The key can be in the form of a.b.c and it will check if the nested field exist
// In case the key is `a` and there is a value `a.b` false is return as it only
// returns true if it's a leave node
func (f Fields) HasKey(key string) bool {
keys := strings.Split(key, ".")
return f.hasKey(keys)
}
func (f Fields) hasKey(keys []string) bool {
// Nothing to compare anymore
if len(keys) == 0 {
return false
}
key := keys[0]
keys = keys[1:]
for _, field := range f {
if field.Name == key {
if len(field.Fields) > 0 {
return field.Fields.hasKey(keys)
}
// Last entry in the tree but still more keys
if len(keys) > 0 {
return false
}
return true
}
}
return false
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v6.0.0-beta1

搜索帮助