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

fix(shardid): use string instead of int64 in id json Marshaler/Unmarshaler #41

Merged
merged 2 commits into from
Apr 22, 2024

Conversation

cnlangzi
Copy link
Member

Fixes

  • fixed BigInt compatibility issue in browser javascript.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @cnlangzi - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Testing: 3 issues found
  • 🟢 Complexity: all looks good
  • 🟢 Docstrings: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.


bufID, err := json.Marshal(id)
bufStr, err := json.Marshal(s)
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Consider adding a test case for handling invalid string formats during JSON unmarshalling.

This test should verify the behavior when the string does not represent a valid int64, ensuring the error handling is correct.

Suggested change
bufStr, err := json.Marshal(s)
bufStr, err := json.Marshal(s)
require.NoError(t, err)
parsedInt, parseErr := strconv.ParseInt(s, 10, 64)
require.Error(t, parseErr, "Expected an error when parsing a non-int64 string")


var jsID ID
err = json.Unmarshal(bufIdInt64, &jsID)
// Unmarshal id from string json bytes
err = json.Unmarshal(bufStr, &jsID)
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Add assertions to verify the internal state of 'jsID' after unmarshalling.

It's important to ensure that all fields of 'jsID' are correctly set, not just that it equals 'id'. This helps catch any partial unmarshalling issues.

Suggested change
err = json.Unmarshal(bufStr, &jsID)
var jsID ID
err = json.Unmarshal(bufStr, &jsID)
require.NoError(t, err)
require.NotNil(t, jsID)
require.Equal(t, id, jsID)

err = json.Unmarshal(bufID, &jsIdInt64)
var jsString string
// Unmarshal string from ID json bytes
err = json.Unmarshal(bufID, &jsString)
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Add a test case to verify the behavior when 'bufID' contains unexpected JSON structure.

This test should check how the unmarshalling behaves if the JSON does not match the expected string format, ensuring robust error handling.

Suggested change
err = json.Unmarshal(bufID, &jsString)
var jsString string
err = json.Unmarshal(bufID, &jsString)
if err != nil {
t.Errorf("Failed to unmarshal JSON: %v", err)
} else if jsString != expectedString {
t.Errorf("Unmarshalled string does not match expected: got %v, want %v", jsString, expectedString)
}

Copy link

codecov bot commented Apr 21, 2024

Codecov Report

Attention: Patch coverage is 80.00000% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 75.31%. Comparing base (d16bfb3) to head (230752d).

Files Patch % Lines
shardid/id.go 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #41      +/-   ##
==========================================
+ Coverage   75.29%   75.31%   +0.01%     
==========================================
  Files          42       42              
  Lines        1757     1758       +1     
==========================================
+ Hits         1323     1324       +1     
  Misses        319      319              
  Partials      115      115              
Flag Coverage Δ
Unit-Tests 75.31% <80.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

deepsource-io bot commented Apr 21, 2024

Here's the code health analysis summary for commits d16bfb3..230752d. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Go LogoGo✅ SuccessView Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

@cnlangzi cnlangzi merged commit edb8304 into main Apr 22, 2024
7 checks passed
@cnlangzi cnlangzi deleted the fix/id_json_in_js branch April 22, 2024 09:49
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.

1 participant