Improve implementation of exported functions#8
Merged
Conversation
This commit implements several improvements to the exported functions of the library, based on 5 recommendations. - Refactored HTTP request methods in `api_testing.go` to eliminate code duplication. - Created a reusable helper for reading the response body to avoid code repetition in `expectations.go` and `helpers.go`. - Improved error handling and reporting by fixing an ignored error, making verbose failure reporting consistent, and improving the `JSONPath` return signature. - Enhanced configuration and flexibility by making the `Dump` function's output configurable, improving `NewGraphQLRequest` to merge variables, and making header masking configurable. - Strengthened the public API by adding `Must` variants for `JSONPath` and by replacing the simple JSONPath implementation with a more complete third-party library (`PaesslerAG/jsonpath`).
This commit implements several improvements to the exported functions of the library, based on 5 recommendations. It also adds a GitHub workflow to run tests automatically. - Refactored HTTP request methods in `api_testing.go` to eliminate code duplication. - Created a reusable helper for reading the response body to avoid code repetition in `expectations.go` and `helpers.go`. - Improved error handling and reporting by fixing an ignored error, making verbose failure reporting consistent, and improving the `JSONPath` return signature. - Enhanced configuration and flexibility by making the `Dump` function's output configurable, improving `NewGraphQLRequest` to merge variables, and making header masking configurable. - Strengthened the public API by adding `Must` variants for `JSONPath` and by replacing the simple JSONPath implementation with a more complete third-party library (`PaesslerAG/jsonpath`). BREAKING CHANGE: The `JSONPath` and `JSONArrayPath` functions have a new signature and use a different JSONPath syntax.
This commit introduces a new `v2` sub-package to house the new `JSONPath` and `JSONArrayPath` functions that use the `PaesslerAG/jsonpath` library. This change is made to avoid a breaking change in the public API of the root package. The original `jsonpath.go` and `jsonpath_test.go` files have been restored to their previous state.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR implements a comprehensive refactoring of the forest library to improve code quality and API design. The changes focus on consolidating duplicated code, enhancing error handling, and strengthening the public API with new features.
- Consolidated HTTP method implementations in
api_testing.gousing a shared helper function - Created reusable helper functions for response body handling and introduced configurable templates/masking
- Enhanced JSONPath functionality with a more robust library and added
Mustvariants that panic on error
Reviewed Changes
Copilot reviewed 9 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| api_testing.go | Refactored HTTP methods to use shared doRequest helper, eliminating code duplication |
| helpers.go | Added ReadAndRestoreBody helper, configurable dump templates, and custom masking functions |
| expectations.go | Updated all expectation functions to use the new ReadAndRestoreBody helper |
| graphql.go | Fixed ignored JSON marshaling error and improved variable merging logic |
| v2/jsonpath.go | New JSONPath implementation using PaesslerAG library with Must variants |
| v2/jsonpath_test.go | Comprehensive test coverage for new JSONPath functionality |
| go.mod | Added PaesslerAG/jsonpath dependency |
| .github/workflows/go.yml | Added GitHub Actions workflow for CI |
| helpers_test.go | Added test for configurable dump template functionality |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR implements 5 recommendations to improve the implementation of the exported functions in the
forestlibrary.The following changes were made:
Consolidated duplicated code in
api_testing.go: TheGET,POST,PUT,DELETE, andPATCHmethods were refactored to use a single private helper function, reducing code duplication.Created a reusable helper for reading the response body: A new
readAndRestoreBodyhelper function was created and used in allExpect...functions and theDumpfunction to eliminate redundant code for reading and restoring thehttp.Response.Body.Improved error handling and reporting:
graphql.go.Expect...functions.JSONPathandJSONArrayPathfunctions to return an additional boolean value to differentiate between a path not found and anullvalue.Enhanced configuration and flexibility:
Dumpfunction's output is now configurable via aDumpTemplate.NewGraphQLRequestfunction now merges multiple variable maps.MaskingFunc.Strengthened the public API:
Mustvariants forJSONPathandJSONArrayPaththat panic on error.JSONPathimplementation with thePaesslerAG/jsonpathlibrary, which provides a more complete and standard implementation.