Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

jsoriano
Copy link
Member

@jsoriano jsoriano commented May 23, 2018

schema.Apply() returned an schema.Errors object as error, with this
object is not easy to check if there was really an error, specially when
optional fields are involved. This leads to lots of cases where these
errors are being ignored.

This change makes Apply to return an error only if a non-optional field
is missing or in any case if some field is misformatted. Otherwise it
returns plain nil, so a more idiomatic go error checking can be used.

schema.Apply() returned an schema.Errors object as error, with this
object is not easy to check if there was really an error, specially when
optional fields are involved. This leads to lots of cases where these
errors are being ignored.

This change makes Apply to return an error only if a non-optional field
is missing or in any case if some field is misformatted. Otherwise it
returns plain nil, so idiomatic go error checking can be used.
@jsoriano jsoriano added in progress Pull request is currently in progress. libbeat Metricbeat Metricbeat labels May 23, 2018
@jsoriano jsoriano requested a review from ruflin May 23, 2018 12:52
@@ -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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method Object.Map should have comment or be unexported


func (errs *Errors) HasRequiredErrors() bool {
for _, err := range *errs {
func (errs Errors) HasRequiredErrors() bool {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method Errors.HasRequiredErrors should have comment or be unexported

@@ -6,50 +6,35 @@ import (
"github.com/elastic/beats/libbeat/logp"
)

type Errors []Error
type Errors []*Error

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type Errors should have comment or be unexported

@@ -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 {

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

@@ -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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method Object.Map should have comment or be unexported


func (errs *Errors) HasRequiredErrors() bool {
for _, err := range *errs {
func (errs Errors) HasRequiredErrors() bool {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported method Errors.HasRequiredErrors should have comment or be unexported

@@ -6,50 +6,35 @@ import (
"github.com/elastic/beats/libbeat/logp"
)

type Errors []Error
type Errors []*Error

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type Errors should have comment or be unexported

@@ -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 {

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

@jsoriano
Copy link
Member Author

WIP, I'm not completely happy with the result and probably I'm missing some cases.

@jsoriano jsoriano added discuss Issue needs further discussion. refactoring labels May 23, 2018
@ruflin
Copy link
Member

ruflin commented May 24, 2018

For testing purpose it is useful to also check for non required fields if they are missing. Is this still possible?

@jsoriano
Copy link
Member Author

jsoriano commented May 24, 2018

Not with the returned error. For testing we might think in other approximations for explicitly checking that optional fields are also present, but I wouldn't make it return an error by default if an optional field is missing, this breaks a bit the nature of a non-required thing.

@ruflin
Copy link
Member

ruflin commented May 28, 2018

I agree with the statement above. The part that I worry is that our current testing in some cases relies on checking for the error, converting it and then check if there are also no optional fields missing.

We can definitively decide to move forward with this suggestion and fix this as a follow up.

@jsoriano
Copy link
Member Author

I'm following with this with other approach (in #7335), returning a "result" object instead of just an error, this result object would make easier to check the errors, and also the list of missing fields, what could be used in tests.

@jsoriano
Copy link
Member Author

Closing this one, continuing in #7335

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss Issue needs further discussion. in progress Pull request is currently in progress. libbeat Metricbeat Metricbeat refactoring
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants