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

chore(encoding): dropped go-json direct dependency #2033

Merged
merged 2 commits into from
Oct 2, 2024
Merged

Conversation

abi87
Copy link
Collaborator

@abi87 abi87 commented Oct 2, 2024

Replaced go-json with encoding/json library.
Note that go-json is still an indirect dependency, but only an indirect dependency.

Summary by CodeRabbit

  • New Features

    • Transitioned to using the standard Go JSON encoding and decoding functionalities, enhancing compatibility and performance.
  • Bug Fixes

    • Removed unnecessary dependency on the gojson library, streamlining the module's requirements.

@abi87 abi87 self-assigned this Oct 2, 2024
Copy link
Contributor

coderabbitai bot commented Oct 2, 2024

Walkthrough

The pull request introduces changes to the go.mod and json.go files within the github.com/berachain/beacon-kit/mod/primitives module. The go.mod file has had the dependency on github.com/goccy/go-json version v0.10.3 removed. In the json.go file, references to the gojson library have been replaced with the standard encoding/json package, updating type aliases and variable assignments accordingly.

Changes

File Path Change Summary
mod/primitives/go.mod Removed dependency: github.com/goccy/go-json v0.10.3
mod/primitives/pkg/encoding/json/json.go Replaced gojson references with standard encoding/json: updated type aliases and variable wrappers.

Poem

In the meadow where bunnies play,
We've tidied up our code today! 🐇
No more gojson, we've made it right,
With encoding/json, our future's bright!
Hopping along, we cheer and sing,
For clean dependencies, oh what joy they bring! 🌼


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 8339c2c and e5aff2e.

📒 Files selected for processing (1)
  • mod/primitives/pkg/encoding/json/json.go (1 hunks)
🔇 Additional comments (6)
mod/primitives/pkg/encoding/json/json.go (6)

30-30: LGTM: Marshaler type alias updated correctly.

The change from gojson.Marshaler to json.Marshaler aligns with the PR objective of replacing the direct dependency on go-json with the standard encoding/json library. This change maintains the same interface, ensuring compatibility with existing code.


34-34: LGTM: Unmarshaler type alias updated correctly.

The change from gojson.Unmarshaler to json.Unmarshaler is consistent with the PR objective and maintains the same interface, ensuring compatibility with existing code.


30-40: Overall LGTM: Successfully replaced go-json with encoding/json.

The changes in this file consistently replace gojson with the standard encoding/json package, aligning perfectly with the PR objective of dropping the direct dependency on go-json. All individual changes look good and maintain the same interfaces, ensuring compatibility with existing code.

Key points to consider:

  1. Performance impact: As suggested in the individual comments, run benchmarks to compare the performance of encoding/json vs go-json for your specific use cases.
  2. Thorough testing: Ensure that your test suite covers various scenarios and edge cases to validate the correctness of the new implementation.
  3. Codebase consistency: Run the following script to verify that no usage of gojson-specific features remains in the codebase:
#!/bin/bash
# Description: Search for any remaining usage of gojson package

rg --type go 'gojson\.' || echo "No usage of gojson package found."
  1. Documentation: Update any documentation or comments that might reference go-json.

Given that go-json remains an indirect dependency, consider evaluating if it can be completely removed from the project in the future to further simplify the dependency structure.


40-40: LGTM: Unmarshal function updated, consider performance impact and testing.

The change from gojson.Unmarshal to json.Unmarshal is consistent with the PR objective. While the function signatures are the same, ensuring compatibility, it's important to note that there might be performance differences between the two implementations.

To assess the performance impact and ensure correctness, consider the following:

  1. Run a performance benchmark:
#!/bin/bash
# Description: Compare performance of json.Unmarshal vs gojson.Unmarshal

cat << EOF > benchmark_unmarshal.go
package main

import (
	"encoding/json"
	"testing"

	gojson "github.com/goccy/go-json"
)

type testStruct struct {
	Name string
	Age  int
	Data []byte
}

var testJSON = []byte(\`{"Name":"Test","Age":30,"Data":"SGVsbG8gV29ybGQ="}\`)

func BenchmarkStdJsonUnmarshal(b *testing.B) {
	var result testStruct
	for i := 0; i < b.N; i++ {
		_ = json.Unmarshal(testJSON, &result)
	}
}

func BenchmarkGoJsonUnmarshal(b *testing.B) {
	var result testStruct
	for i := 0; i < b.N; i++ {
		_ = gojson.Unmarshal(testJSON, &result)
	}
}
EOF

go test -bench=. benchmark_unmarshal.go
  1. Implement a comprehensive test suite:
#!/bin/bash
# Description: Search for existing JSON-related tests

rg --type go -g '*_test.go' 'func.*Test.*Json'

This command will help you identify existing JSON-related tests. Ensure that these tests cover various scenarios, including edge cases, to validate the correctness of the new json.Unmarshal implementation in your specific use cases.


36-36: LGTM: Marshal function updated, consider performance impact.

The change from gojson.Marshal to json.Marshal aligns with the PR objective. While the function signatures are the same, ensuring compatibility, it's important to note that there might be performance differences between the two implementations.

To assess the performance impact, consider running the following benchmark:

This script will help you compare the performance of json.Marshal and gojson.Marshal for a sample struct. Adjust the testStruct and testData as needed to better reflect your actual use case.


38-38: LGTM: MarshalIndent function updated, consider performance impact.

The change from gojson.MarshalIndent to json.MarshalIndent is consistent with the PR objective. While the function signatures are the same, ensuring compatibility, it's important to note that there might be performance differences between the two implementations.

To assess the performance impact, consider running the following benchmark:

This script will help you compare the performance of json.MarshalIndent and gojson.MarshalIndent for a sample struct. Adjust the testStruct and testData as needed to better reflect your actual use case.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 5bdacfc and 8339c2c.

⛔ Files ignored due to path filters (1)
  • mod/primitives/go.sum is excluded by !**/*.sum
📒 Files selected for processing (2)
  • mod/primitives/go.mod (0 hunks)
  • mod/primitives/pkg/encoding/json/json.go (1 hunks)
💤 Files with no reviewable changes (1)
  • mod/primitives/go.mod
🔇 Additional comments (5)
mod/primitives/pkg/encoding/json/json.go (5)

34-34: LGTM! Verify usage in codebase.

The change from gojson.Unmarshaler to json.Unmarshaler is correct and aligns with the PR objective. Both interfaces are identical, so this should not introduce any functional changes.

To ensure this change doesn't impact the codebase, run the following script:

#!/bin/bash
# Description: Check for any custom implementations of the Unmarshaler interface
# that might rely on gojson-specific features.

# Test: Search for struct types implementing the Unmarshaler interface
rg --type go '(type\s+\w+\s+struct.*\n)*\s*func\s*\(\w+\s*\**\w+\)\s*UnmarshalJSON\s*\(.*\)\s*error'

42-42: LGTM! Verify usage in codebase.

The change from gojson.MarshalIndent to json.MarshalIndent is correct and aligns with the PR objective. However, there might be slight differences in the output format or performance between the two implementations.

To ensure this change doesn't impact the codebase, run the following script:

#!/bin/bash
# Description: Check for any direct usage of MarshalIndent that might rely on gojson-specific features or formatting.

# Test: Search for direct usage of MarshalIndent function
rg --type go '\bMarshalIndent\s*\('

Consider reviewing the output of MarshalIndent in your tests to ensure the indentation format meets your requirements.


46-46: LGTM! Verify usage and consider performance testing.

The change from gojson.Unmarshal to json.Unmarshal is correct and aligns with the PR objective. However, there might be slight performance differences between the two implementations.

To ensure this change doesn't impact the codebase and to assess any performance implications, run the following scripts:

#!/bin/bash
# Description: Check for any direct usage of Unmarshal that might rely on gojson-specific features or performance characteristics.

# Test: Search for direct usage of Unmarshal function
rg --type go '\bUnmarshal\s*\('

# Test: Search for any performance-critical sections using Unmarshal
rg --type go -C 10 '(benchmark|performance|critical).*Unmarshal'

Consider running performance tests to compare the unmarshaling speed between gojson.Unmarshal and json.Unmarshal for your specific use cases.


38-38: LGTM! Verify usage and consider performance testing.

The change from gojson.Marshal to json.Marshal is correct and aligns with the PR objective. However, there might be slight performance differences between the two implementations.

To ensure this change doesn't impact the codebase and to assess any performance implications, run the following scripts:

Consider running performance tests to compare the marshaling speed between gojson.Marshal and json.Marshal for your specific use cases.

✅ Verification successful

Verified the change to json.Marshal. No performance-critical usages of Marshal were found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any direct usage of Marshal that might rely on gojson-specific features or performance characteristics.

# Test: Search for direct usage of Marshal function
rg --type go '\bMarshal\s*\('

# Test: Search for any performance-critical sections using Marshal
rg --type go -C 10 '(benchmark|performance|critical).*Marshal'

Length of output: 3297


Script:

#!/bin/bash
# Description: Re-check for any direct usage of Marshal and ensure the search term is correct.

# Test: Search for direct usage of Marshal function
rg --type go '\bMarshal\s*\('

# Test: Search for any performance-critical sections using Marshal
rg --type go -C 10 '(benchmark|performance|critical).*Marshal'

Length of output: 2088


Script:

#!/bin/bash
# Description: Verify if Marshal is used in performance-critical sections with corrected search patterns.

# Test: Search for any performance-critical sections using Marshal
rg --type go '(benchmark|performance|critical).*Marshal' -C 10

Length of output: 64


30-30: LGTM! Verify usage in codebase.

The change from gojson.Marshaler to json.Marshaler is correct and aligns with the PR objective. Both interfaces are identical, so this should not introduce any functional changes.

To ensure this change doesn't impact the codebase, run the following script:

✅ Verification successful

LGTM! Usage verified across the codebase. No issues found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any custom implementations of the Marshaler interface
# that might rely on gojson-specific features.

# Test: Search for struct types implementing the Marshaler interface
rg --type go '(type\s+\w+\s+struct.*\n)*\s*func\s*\(\w+\s*\**\w+\)\s*MarshalJSON\s*\(\)\s*\(.*\)'

Length of output: 3297


Script:

#!/bin/bash
# Description: Check for any custom implementations of the Marshaler interface
# that might rely on gojson-specific features.

# Test: Search for struct types implementing the MarshalJSON method
rg --type go '(type\s+\w+\s+struct.*\n)*\s*func\s*\(\w+\s*\**\w+\)\s*MarshalJSON\s*\(\)\s*\(.*\)'

Length of output: 291


Script:

#!/bin/bash
# Description: Check for any custom implementations of the Marshaler interface
# that might rely on gojson-specific features.

# Test: Search for struct types implementing the MarshalJSON method with multiline support
rg -U --type go '(type\s+\w+\s+struct.*\n)*\s*func\s*\(\w+\s*\**\w+\)\s*MarshalJSON\s*\(\)\s*\(.*\)'

Length of output: 1467

Comment on lines 30 to 46
type Marshaler = json.Marshaler

// Unmarshaler is the interface implemented by types
// that can unmarshal a JSON description of themselves.
type Unmarshaler = gojson.Unmarshaler
type Unmarshaler = json.Unmarshaler

// Marshal is a wrapper for gojson.Marshal, which provides high-performance JSON
// encoding.
var Marshal = gojson.Marshal
var Marshal = json.Marshal

// MarshalIndent is a wrapper for gojson.MarshalIndent, which provides
// high-performance JSON encoding with indentation.
var MarshalIndent = gojson.MarshalIndent
var MarshalIndent = json.MarshalIndent

// Unmarshal is a wrapper for gojson.Unmarshal, which provides high-performance
// JSON decoding.
var Unmarshal = gojson.Unmarshal
var Unmarshal = json.Unmarshal
Copy link
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

Overall LGTM! Consider performance impact and thorough testing.

The changes in this file consistently replace gojson with the standard encoding/json package, aligning well with the PR objective of dropping the direct dependency on go-json. All individual changes look good.

Recommendations:

  1. Run the verification scripts provided in the previous comments to ensure no usage of gojson-specific features in the codebase.
  2. Conduct thorough testing, especially for any performance-critical sections that use these JSON functions.
  3. Consider running benchmarks to compare the performance of encoding/json vs go-json for your specific use cases.
  4. Update any documentation or comments that might reference go-json.

Given that go-json remains an indirect dependency, consider evaluating if it can be completely removed from the project in the future to further simplify the dependency structure.

Copy link

codecov bot commented Oct 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 22.42%. Comparing base (5bdacfc) to head (e5aff2e).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2033   +/-   ##
=======================================
  Coverage   22.42%   22.42%           
=======================================
  Files         358      358           
  Lines       16012    16012           
  Branches       12       12           
=======================================
  Hits         3591     3591           
  Misses      12272    12272           
  Partials      149      149           

Copy link
Contributor

@ocnc ocnc left a comment

Choose a reason for hiding this comment

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

nice

@ocnc ocnc merged commit 973f0db into main Oct 2, 2024
16 checks passed
@ocnc ocnc deleted the drop_gojson_dep branch October 2, 2024 19:13
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.

3 participants