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

Add and test JSON object matcher #819

Merged
merged 2 commits into from
Apr 27, 2022
Merged

Conversation

webmaster128
Copy link
Member

@webmaster128 webmaster128 commented Apr 27, 2022

JSON matching for #817

Copy link
Member

@ethanfrey ethanfrey left a comment

Choose a reason for hiding this comment

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

@jackzampolin you really should cherry-pick in these commits to your branch.
This is great example of testing methods

(Or maybe we just merge these two commits to main?)


// isJSONObjectWithTopLevelKey checks if the given bytes are a valid JSON object
// with exactly one top level key that is contained in the list of allowed keys.
func isJSONObjectWithTopLevelKey(jsonBytes []byte, allowedKeys []string) error {
Copy link
Member

Choose a reason for hiding this comment

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

very nice function, doing what it should

x/wasm/types/json_matching.go Outdated Show resolved Hide resolved
}

if !found {
return sdkerrors.Wrapf(ErrTopKevelKeyNotAllowed, "JSON object has a top level key which is not allowed: '%s'", topLevelKey)
Copy link
Member

Choose a reason for hiding this comment

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

And return this error here. Removing the found variable completely

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, done, thanks

@@ -72,6 +72,18 @@ var (

// error if an address does not belong to a contract (just for registration)
_ = sdkErrors.Register(DefaultCodespace, 22, "no such contract")

// ErrNotAJSONObject error if given data is not a JSON object
ErrNotAJSONObject = sdkErrors.Register(DefaultCodespace, 23, "not a JSON object")
Copy link
Member

Choose a reason for hiding this comment

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

Good stuff with the unique error codes

},
"happy with excaped key": {
src: []byte(`{"event\u2468thing": {"foo":"bar"}}`),
allowedKeys: []string{"event⑨thing"},
Copy link
Member

Choose a reason for hiding this comment

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

😄

"github.com/stretchr/testify/require"
)

func TestIsJSONObjectWithTopLevelKey(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Wonderful test vectors here!

@webmaster128 webmaster128 force-pushed the isJSONObjectWithTopLevelKey branch 2 times, most recently from 1924417 to 0e723a6 Compare April 27, 2022 12:00
@webmaster128 webmaster128 marked this pull request as ready for review April 27, 2022 12:00
@webmaster128 webmaster128 requested a review from alpe as a code owner April 27, 2022 12:00
@webmaster128
Copy link
Member Author

webmaster128 commented Apr 27, 2022

Rebased onto main as this is independent of the diff in #817. Feel free to merge into main or cherry-pick into the PR once @jackzampolin and maybe @alpe had a look.

@ethanfrey
Copy link
Member

Minting fails now due to:

golangci-lint has version v1.43.0 built from 861262b7 on 2021-11-03T12:17:18Z
x/wasm/types/json_matching.go:11:6: `isJSONObjectWithTopLevelKey` is unused (deadcode)
func isJSONObjectWithTopLevelKey(jsonBytes []byte, allowedKeys []string) error {

Please make that public (IsJSONObjectWithTopLevelKey). Then you can merge this

Copy link
Member

@ethanfrey ethanfrey left a comment

Choose a reason for hiding this comment

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

Happy to merge as soon as CI is happy (commented how to fix above)

@webmaster128 webmaster128 force-pushed the isJSONObjectWithTopLevelKey branch from 0e723a6 to 1e892ac Compare April 27, 2022 15:00
@codecov
Copy link

codecov bot commented Apr 27, 2022

Codecov Report

Merging #819 (1e892ac) into main (7a25e36) will increase coverage by 0.04%.
The diff coverage is 92.85%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #819      +/-   ##
==========================================
+ Coverage   59.29%   59.34%   +0.04%     
==========================================
  Files          50       51       +1     
  Lines        5884     5898      +14     
==========================================
+ Hits         3489     3500      +11     
- Misses       2143     2145       +2     
- Partials      252      253       +1     
Impacted Files Coverage Δ
x/wasm/types/errors.go 0.00% <ø> (ø)
x/wasm/types/json_matching.go 92.85% <92.85%> (ø)
x/wasm/keeper/keeper.go 87.47% <0.00%> (-0.35%) ⬇️

@webmaster128 webmaster128 merged commit 8d3d90c into main Apr 27, 2022
@webmaster128 webmaster128 deleted the isJSONObjectWithTopLevelKey branch April 27, 2022 15:11
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.

2 participants