代码拉取完成,页面将自动刷新
package schema
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
)
// Schema describes how a map[string]interface{} object can be parsed and converted into
// an event. The conversions can be described using an (optionally nested) common.MapStr
// that contains Conv objects.
type Schema map[string]Mapper
// Mapper interface represents a valid type to be used in a schema.
type Mapper interface {
// Map applies the Mapper conversion on the data and adds the result
// to the event on the key.
Map(key string, event common.MapStr, data map[string]interface{})
HasKey(key string) bool
}
// A Conv object represents a conversion mechanism from the data map to the event map.
type Conv struct {
Func Converter // Convertor function
Key string // The key in the data map
Optional bool // Whether to log errors if the key is not found
}
// Convertor function type
type Converter func(key string, data map[string]interface{}) (interface{}, error)
// Map applies the conversion on the data and adds the result
// to the event on the key.
func (conv Conv) Map(key string, event common.MapStr, data map[string]interface{}) {
value, err := conv.Func(conv.Key, data)
if err != nil {
if !conv.Optional {
logp.Err("Error on field '%s': %v", key, err)
}
} else {
event[key] = value
}
}
func (conv Conv) HasKey(key string) bool {
return conv.Key == key
}
// implements Mapper interface for structure
type Object map[string]Mapper
func (o Object) Map(key string, event common.MapStr, data map[string]interface{}) {
subEvent := common.MapStr{}
applySchemaToEvent(subEvent, data, o)
event[key] = subEvent
}
func (o Object) HasKey(key string) bool {
return hasKey(key, o)
}
// ApplyTo adds the fields extracted from data, converted using the schema, to the
// event map.
func (s Schema) ApplyTo(event common.MapStr, data map[string]interface{}) common.MapStr {
applySchemaToEvent(event, data, s)
return event
}
// Apply converts the fields extracted from data, using the schema, into a new map.
func (s Schema) Apply(data map[string]interface{}) common.MapStr {
return s.ApplyTo(common.MapStr{}, data)
}
// HasKey checks if the key is part of the schema
func (s Schema) HasKey(key string) bool {
return hasKey(key, s)
}
func hasKey(key string, mappers map[string]Mapper) bool {
for _, mapper := range mappers {
if mapper.HasKey(key) {
return true
}
}
return false
}
func applySchemaToEvent(event common.MapStr, data map[string]interface{}, conversions map[string]Mapper) {
for key, mapper := range conversions {
mapper.Map(key, event, data)
}
}
// SchemaOption is for adding optional parameters to the conversion
// functions
type SchemaOption func(c Conv) Conv
// The optional flag suppresses the error message in case the key
// doesn't exist or results in an error.
func Optional(c Conv) Conv {
c.Optional = true
return c
}
// setOptions adds the optional flags to the Conv object
func SetOptions(c Conv, opts []SchemaOption) Conv {
for _, opt := range opts {
c = opt(c)
}
return c
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。