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

Initial JSONPath scrubber #42

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bardzusny
Copy link

@bardzusny bardzusny commented Jun 10, 2022

A proof-of-concept PR implementing #38 .

Ignore the implementation details for now, focus on the public API.

If it looks right then we can work on it! (I'll also happily rebase commits to fit the preferred notation.)

Description

Adds basic support for scrubbing values by passed JSONPath. To be used with approvals.VerifyJsonStruct(...) and approvals.VerifyJSONBytes(...).

Usage examples:

opts := approvals.Options().WithJSONPathScrubber("id", "<scrubbed_id>")

jb := []byte(`{ "id": 1 }`)
approvals.VerifyJSONBytes(t, jb, opts)

Also works for scrubbing properties in lists of objects:

 opts := approvals.Options().WithJSONPathScrubber("items[*].id", "<scrubbed_id>")

jb := []byte(`{ "items": [{ "id": 1 }, { "id": 2 }] }`)
approvals.VerifyJSONBytes(t, jb, opts)

The solution

What happens during scrubbing:

  1. The JSON string is un-marshalled into a https://pkg.go.dev/github.com/bitly/go-simplejson struct.
  2. A recursive function scrubs values based on the passed path.
  3. Resulting simplejson struct is marshalled back into a pretty-printed JSON.

What's nice: it works and should be good great for most common usage:

  1. Scrubbing object properties.
  2. Scrubbing properties in lists of objects.

The things left to iterate on (in this PR or later):

  1. Would be nice to drop simplejson dependency.
  2. Doesn't look optimal perf-wise to pass JSON string to a scrubber instead of the original map.
  3. Doesn't implement full JSONPath proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant