pilagod / gorm-cursor-paginator

A paginator doing cursor-based pagination based on GORM
https://github.com/pilagod/gorm-cursor-paginator
MIT License
192 stars 44 forks source link

Support NULL value replacement for custom types #58

Closed zitnik closed 8 months ago

zitnik commented 9 months ago

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.

coveralls commented 9 months ago

Pull Request Test Coverage Report for Build 7958280161

Details


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

πŸ’› - Coveralls
pilagod commented 8 months ago

@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 commented 8 months ago

Imported the new version to our project, seems to do the trick :slightly_smiling_face:

@pilagod thank you for the review and for maintaining the library :muscle:

nikicc commented 8 months ago

@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!