Skip to content

libbeat: schema.Apply() returns plain error or nil#7167

Closed
jsoriano wants to merge 1 commit intoelastic:masterfrom
jsoriano:schema-errors
Closed

libbeat: schema.Apply() returns plain error or nil#7167
jsoriano wants to merge 1 commit intoelastic:masterfrom
jsoriano:schema-errors

Conversation

@jsoriano
Copy link
Copy Markdown
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
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 {
Copy link
Copy Markdown

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 {
Copy link
Copy Markdown

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

)

type Errors []Error
type Errors []*Error
Copy link
Copy Markdown

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

return fmt.Sprintf("Missing field: %s, Error: %s", err.key, err.message)
}

type KeyNotFoundError struct {
Copy link
Copy Markdown

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

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 {
Copy link
Copy Markdown

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 {
Copy link
Copy Markdown

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

)

type Errors []Error
type Errors []*Error
Copy link
Copy Markdown

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

return fmt.Sprintf("Missing field: %s, Error: %s", err.key, err.message)
}

type KeyNotFoundError struct {
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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