-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
libbeat: schema.Apply() returns plain error or nil #7167
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,16 @@ func (err *Error) IsType(errorType ErrorType) bool { | |
func (err *Error) Error() string { | ||
return fmt.Sprintf("Missing field: %s, Error: %s", err.key, err.message) | ||
} | ||
|
||
type KeyNotFoundError struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported type KeyNotFoundError should have comment or be unexported |
||
Key string | ||
Err error | ||
} | ||
|
||
func (err *KeyNotFoundError) Error() string { | ||
msg := fmt.Sprintf("Key `%s` not found", err.Key) | ||
if err.Err != nil { | ||
msg += ": " + err.Err.Error() | ||
} | ||
return msg | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,50 +6,35 @@ import ( | |
"github.com/elastic/beats/libbeat/logp" | ||
) | ||
|
||
type Errors []Error | ||
type Errors []*Error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported type Errors should have comment or be unexported There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported type Errors should have comment or be unexported |
||
|
||
func NewErrors() *Errors { | ||
return &Errors{} | ||
} | ||
|
||
func (errs *Errors) AddError(err *Error) { | ||
*errs = append(*errs, *err) | ||
} | ||
|
||
func (errs *Errors) AddErrors(errors *Errors) { | ||
if errors == nil { | ||
return | ||
} | ||
*errs = append(*errs, *errors...) | ||
} | ||
|
||
func (errs *Errors) HasRequiredErrors() bool { | ||
for _, err := range *errs { | ||
func (errs Errors) HasRequiredErrors() bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported method Errors.HasRequiredErrors should have comment or be unexported There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported method Errors.HasRequiredErrors should have comment or be unexported |
||
for _, err := range errs { | ||
if err.IsType(RequiredType) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (errs *Errors) Error() string { | ||
error := "Required fields are missing: " | ||
for _, err := range *errs { | ||
func (errs Errors) Error() string { | ||
var required []string | ||
for _, err := range errs { | ||
if err.IsType(RequiredType) { | ||
error = error + "," + err.key | ||
required = append(required, err.key) | ||
} | ||
} | ||
return error | ||
return "Required fields are missing: " + strings.Join(required, ", ") | ||
} | ||
|
||
// Log logs all missing required and optional fields to the debug log. | ||
func (errs *Errors) Log() { | ||
if len(*errs) == 0 { | ||
func (errs Errors) Log() { | ||
if len(errs) == 0 { | ||
return | ||
} | ||
var optional, required []string | ||
|
||
for _, err := range *errs { | ||
for _, err := range errs { | ||
if err.IsType(RequiredType) { | ||
required = append(required, err.key) | ||
} else { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ type Schema map[string]Mapper | |
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{}) *Errors | ||
Map(key string, event common.MapStr, data map[string]interface{}) Errors | ||
|
||
HasKey(key string) bool | ||
} | ||
|
@@ -25,26 +25,21 @@ type Conv struct { | |
Optional bool // Whether to log errors if the key is not found | ||
} | ||
|
||
// Convertor function type | ||
// Converter 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{}) *Errors { | ||
func (conv Conv) Map(key string, event common.MapStr, data map[string]interface{}) Errors { | ||
value, err := conv.Func(conv.Key, data) | ||
if err != nil { | ||
err := NewError(key, err.Error()) | ||
if conv.Optional { | ||
err.SetType(OptionalType) | ||
if _, keyNotFound := err.(*KeyNotFoundError); keyNotFound && conv.Optional { | ||
return nil | ||
} | ||
return Errors{NewError(key, err.Error())} | ||
|
||
errs := NewErrors() | ||
errs.AddError(err) | ||
return errs | ||
|
||
} else { | ||
event[key] = value | ||
} | ||
event[key] = value | ||
return nil | ||
} | ||
|
||
|
@@ -55,7 +50,7 @@ func (conv Conv) HasKey(key string) bool { | |
// implements Mapper interface for structure | ||
type Object map[string]Mapper | ||
|
||
func (o Object) Map(key string, event common.MapStr, data map[string]interface{}) *Errors { | ||
func (o Object) Map(key string, event common.MapStr, data map[string]interface{}) Errors { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported method Object.Map should have comment or be unexported There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported method Object.Map should have comment or be unexported |
||
subEvent := common.MapStr{} | ||
errs := applySchemaToEvent(subEvent, data, o) | ||
event[key] = subEvent | ||
|
@@ -68,14 +63,17 @@ func (o Object) HasKey(key string) bool { | |
|
||
// 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, *Errors) { | ||
func (s Schema) ApplyTo(event common.MapStr, data map[string]interface{}) (common.MapStr, error) { | ||
errors := applySchemaToEvent(event, data, s) | ||
errors.Log() | ||
return event, errors | ||
if len(errors) > 0 { | ||
errors.Log() | ||
return event, errors | ||
} | ||
return event, nil | ||
} | ||
|
||
// Apply converts the fields extracted from data, using the schema, into a new map and reports back the errors. | ||
func (s Schema) Apply(data map[string]interface{}) (common.MapStr, *Errors) { | ||
func (s Schema) Apply(data map[string]interface{}) (common.MapStr, error) { | ||
return s.ApplyTo(common.MapStr{}, data) | ||
} | ||
|
||
|
@@ -93,11 +91,11 @@ func hasKey(key string, mappers map[string]Mapper) bool { | |
return false | ||
} | ||
|
||
func applySchemaToEvent(event common.MapStr, data map[string]interface{}, conversions map[string]Mapper) *Errors { | ||
errs := NewErrors() | ||
func applySchemaToEvent(event common.MapStr, data map[string]interface{}, conversions map[string]Mapper) Errors { | ||
var errs Errors | ||
for key, mapper := range conversions { | ||
errors := mapper.Map(key, event, data) | ||
errs.AddErrors(errors) | ||
errs = append(errs, errors...) | ||
} | ||
return errs | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exported type KeyNotFoundError should have comment or be unexported