Skip to content

Improve implementation of exported functions#8

Merged
emicklei merged 6 commits intomasterfrom
improve-exported-functions
Aug 21, 2025
Merged

Improve implementation of exported functions#8
emicklei merged 6 commits intomasterfrom
improve-exported-functions

Conversation

@google-labs-jules
Copy link
Copy Markdown
Contributor

This PR implements 5 recommendations to improve the implementation of the exported functions in the forest library.

The following changes were made:

  1. Consolidated duplicated code in api_testing.go: The GET, POST, PUT, DELETE, and PATCH methods were refactored to use a single private helper function, reducing code duplication.

  2. Created a reusable helper for reading the response body: A new readAndRestoreBody helper function was created and used in all Expect... functions and the Dump function to eliminate redundant code for reading and restoring the http.Response.Body.

  3. Improved error handling and reporting:

    • Fixed an ignored JSON marshaling error in graphql.go.
    • Made verbose failure reporting consistent across all Expect... functions.
    • Changed the JSONPath and JSONArrayPath functions to return an additional boolean value to differentiate between a path not found and a null value.
  4. Enhanced configuration and flexibility:

    • The Dump function's output is now configurable via a DumpTemplate.
    • The NewGraphQLRequest function now merges multiple variable maps.
    • Header masking is now configurable via a MaskingFunc.
  5. Strengthened the public API:

    • Added Must variants for JSONPath and JSONArrayPath that panic on error.
    • Replaced the simple JSONPath implementation with the PaesslerAG/jsonpath library, which provides a more complete and standard implementation.

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`).
@emicklei emicklei marked this pull request as ready for review August 21, 2025 11:12
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.
@emicklei emicklei requested a review from Copilot August 21, 2025 11:38
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.go using 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 Must variants 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.

emicklei and others added 3 commits August 21, 2025 13:42
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>
@emicklei emicklei merged commit 8296b12 into master Aug 21, 2025
1 check passed
@emicklei emicklei deleted the improve-exported-functions branch August 21, 2025 11:44
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