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(time): added nullable Time with better json support #44

Merged
merged 5 commits into from
Apr 26, 2024
Merged

Conversation

cnlangzi
Copy link
Member

Changes

  • added Time for better json support.

Copy link

codecov bot commented Apr 26, 2024

Codecov Report

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

Project coverage is 76.09%. Comparing base (1ade208) to head (969829f).

Files Patch % Lines
time.go 91.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #44      +/-   ##
==========================================
+ Coverage   75.88%   76.09%   +0.21%     
==========================================
  Files          42       43       +1     
  Lines        1758     1782      +24     
==========================================
+ Hits         1334     1356      +22     
- Misses        306      307       +1     
- Partials      118      119       +1     
Flag Coverage Δ
Unit-Tests 76.09% <92.30%> (+0.21%) ⬆️

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

@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: 4 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 2 issues found
  • 🟢 Complexity: 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.

Comment on lines +48 to +49
if err != nil {
return err
Copy link

Choose a reason for hiding this comment

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

suggestion (code_refinement): Consider handling specific JSON unmarshal errors for better error reporting.

Handling specific JSON unmarshal errors could provide more context and help in debugging issues related to date format errors or other JSON related issues.

Suggested change
if err != nil {
return err
if err != nil {
if _, ok := err.(*json.UnmarshalTypeError); ok {
return fmt.Errorf("time data was not in the expected format: %w", err)
}
return fmt.Errorf("error unmarshaling time data: %w", err)
}

time.go Outdated
Comment on lines 42 to 43
if data == nil {
return nil
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Returning early when 'data' is nil might mask potential bugs.

It's generally safe to let the JSON unmarshalling process handle nil or empty slices as it will correctly interpret them as zero values for the types. This early return could potentially skip necessary processing or error handling that might be added later.

Comment on lines +12 to +75
func TestTimeInSQL(t *testing.T) {

now := time.Now()
d, err := sql.Open("sqlite3", "file::memory:")
require.NoError(t, err)

_, err = d.Exec("CREATE TABLE `times` (`id` id NOT NULL,`created_at` datetime, PRIMARY KEY (`id`))")
require.NoError(t, err)

result, err := d.Exec("INSERT INTO `times`(`id`) VALUES(?)", 10)
require.NoError(t, err)

rows, err := result.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rows)

result, err = d.Exec("INSERT INTO `times`(`id`, `created_at`) VALUES(?, ?)", 20, now)
require.NoError(t, err)

rows, err = result.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rows)

var t10 Time
err = d.QueryRow("SELECT `created_at` FROM `times` WHERE id=?", 10).Scan(&t10)
require.NoError(t, err)

require.EqualValues(t, false, t10.Valid)

var t20 Time
err = d.QueryRow("SELECT `created_at` FROM `times` WHERE id=?", 20).Scan(&t20)
require.NoError(t, err)

require.EqualValues(t, true, t20.Valid)
require.EqualValues(t, now.UTC(), t20.Time().UTC())

result, err = d.Exec("INSERT INTO `times`(`id`,`created_at`) VALUES(?, ?)", 11, t10)
require.NoError(t, err)

rows, err = result.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rows)

result, err = d.Exec("INSERT INTO `times`(`id`, `created_at`) VALUES(?, ?)", 21, t20)
require.NoError(t, err)

rows, err = result.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rows)

var t11 Time
err = d.QueryRow("SELECT `created_at` FROM `times` WHERE id=?", 11).Scan(&t11)
require.NoError(t, err)

require.EqualValues(t, false, t11.Valid)

var t21 Time
err = d.QueryRow("SELECT `created_at` FROM `times` WHERE id=?", 21).Scan(&t21)
require.NoError(t, err)

require.EqualValues(t, true, t21.Valid)
require.EqualValues(t, now.UTC(), t21.Time().UTC())

}
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 tests for error conditions and edge cases.

The current tests cover the basic functionality well, but there are no tests for error conditions such as database connection failures, SQL syntax errors, or invalid time values. Additionally, edge cases like leap seconds or timezone differences are not tested.

require.EqualValues(t, true, t21.Valid)
require.EqualValues(t, now.UTC(), t21.Time().UTC())

}
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 tests for JSON serialization and deserialization of null or invalid times.

The tests for JSON handling are good for valid times, but they do not cover cases where the time is null or invalid. This is important to ensure robustness in handling different kinds of input.

Copy link

deepsource-io bot commented Apr 26, 2024

Here's the code health analysis summary for commits 1ade208..969829f. 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 a6accdf into main Apr 26, 2024
7 checks passed
@cnlangzi cnlangzi deleted the fix/time branch April 26, 2024 07:43
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