Skip to content

Respect errors in cb function in ArrayEach and EachKey #53

Description

@lorehov

Suppose we have following code:

    result := []Foo{}
    err := jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
        if err != nil {
            return
        }
        bars, err := extractBars(value)
        if err != nil {
            return
        }
        append(result, Foo{FieldOne: jsonparser.GetString(value, "target"), Bars: bars})
    })

We have no options to stop iterate over data even if our function extractBars will return an error.

I think there are two possible ways of fixing this issue.

1. If we don't care about backward compatibility
Just change cb signature to cb func(value []byte, dataType ValueType, offset int, err error) error.

2. If we respect backward compatibility
Then we could add counterpart for both there functions and name these like IterArray, IterKeys or something like this.

P.S.: In both cases I could contribute with PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions