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

Support NULL value replacement for custom types #58

Merged
merged 4 commits into from
Feb 20, 2024

Conversation

zitnik
Copy link
Contributor

@zitnik zitnik commented Feb 15, 2024

This PR aims to add support for NULL value replacement in custom types. Builds upon #39 and #44.

Current issue

The issue that I came across with the current implementation is that when the pagination cursor includes null for a custom type, that value is not properly replaced with the NullReplacement value. The reason is that the Go-representation of the cursor value is not nil, but a custom type object (with an underlying value of nil).

This in turn causes the pagination WHERE clause to compare the COALESCEd values of the target column with an unreplaced NULL, e.g. WHERE COALESCE(my_attr, '') > NULL.

Implementation

The change in this PR specifically handles custom types in the isNil function so that for custom types we evaluate isNil on the underlying value retrieved by GetCustomTypeValue instead of on the object itself.

Test

In order to construct a test, I added a custom type as an alias of sql.NullString, implemented the necessary methods on this type, and added a new field to the order struct. I guess this is a bit clumsy, in case you have any ideas about how to construct such a test more compactly, please let me know :)
In the test itself, we have a primary pagination rule on "Remark" and a secondary rule on the new "Description" field so that the NULL element (ID 2) appears in the cursor causing the described problem.

Copy link
Contributor

@nikicc nikicc left a comment

Choose a reason for hiding this comment

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

Code changes make sense to me 👍 Thanks for fixing my bugs 🙈

@coveralls
Copy link

coveralls commented Feb 16, 2024

Pull Request Test Coverage Report for Build 7958280161

Details

  • 0 of 6 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.6%) to 96.533%

Totals Coverage Status
Change from base Build 5418911557: 0.6%
Covered Lines: 362
Relevant Lines: 375

💛 - Coveralls

paginator/paginator.go Outdated Show resolved Hide resolved
paginator/paginator_paginate_test.go Outdated Show resolved Hide resolved
paginator/paginator.go Outdated Show resolved Hide resolved
paginator/paginator_paginate_test.go Outdated Show resolved Hide resolved
paginator/paginator_paginate_test.go Outdated Show resolved Hide resolved
paginator/paginator_paginate_test.go Outdated Show resolved Hide resolved
paginator/paginator_paginate_test.go Outdated Show resolved Hide resolved
paginator/paginator_test.go Outdated Show resolved Hide resolved
zitnik and others added 2 commits February 19, 2024 11:13
Co-authored-by: Cyan Ho <pilagooood@gmail.com>
@pilagod pilagod merged commit 6c3eb36 into pilagod:master Feb 20, 2024
3 checks passed
@pilagod
Copy link
Owner

pilagod commented Feb 20, 2024

@zitnik @nikicc I just released the new version v2.4.2 to include this PR, you can give it a check. 😎

So grateful for your contributions. 🙌

@zitnik
Copy link
Contributor Author

zitnik commented Feb 20, 2024

Imported the new version to our project, seems to do the trick 🙂

@pilagod thank you for the review and for maintaining the library 💪

@zitnik zitnik deleted the customtype-nil-replacement branch February 20, 2024 11:00
@nikicc
Copy link
Contributor

nikicc commented Feb 20, 2024

@zitnik @nikicc I just released the new version v2.4.2 to include this PR, you can give it a check. 😎

@pilagod thank you for making a release promptly 🙏 Appreciated!

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.

4 participants