feat: add null-safe nullable-argument extensions for querydsl-kotlin#1846
Open
HarryJhin wants to merge 1 commit into
Open
feat: add null-safe nullable-argument extensions for querydsl-kotlin#1846HarryJhin wants to merge 1 commit into
HarryJhin wants to merge 1 commit into
Conversation
Nullable-receiver / nullable-argument infix extensions that return null so
where(...) drops them, letting optional filters skip the if-else /
BooleanBuilder / per-field helper boilerplate:
where(member.name eq name, member.age gt age) // null args skipped
Comparison / equality / string / collection helpers, each null-safe against
both a value and another expression (member-over-extension keeps existing
non-null calls intact): eq, ne, in, gt, goe, lt, loe,
between (partial range: both/lower-only/upper-only/skip), notBetween,
between(ClosedRange), like, contains, startsWith, endsWith.
Boolean operators are folded into null-safe variants (and/or with partial
application, xor/xnor/not skip on null). This changes their return type to
BooleanExpression?, a source-breaking change for raw Expression<Boolean>
callers; BooleanExpression.and/or/not stay non-null via member methods.
Each function covers the four null combinations (value and expression
overloads, plus partial application for between/and/or); existing
CollectionTest still passes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1845
Problem
Building dynamic queries in Kotlin is more verbose than in Java.
where(...)already ignoresnullpredicates (DefaultQueryMetadata.addWherereturns early onnull), but Kotlin has noternary operator, so an optional filter forces
if/else,?.let, aBooleanBuilder, or aper-field helper. The existing
ExpressionExtensions.ktprovides only non-null operator overloads,which do not cover the "skip this condition when the argument is null" case that dominates
dynamic-query code.
Approach
Null-safe infix extensions that return
null(dropped bywhere(...)) when the receiver or theargument is null:
where( member.name eq name, // name: String? -> skipped when null member.age gt age, // age: Int? -> skipped when null )SimpleExpressionExtensions,ComparableExpressionExtensions,NumberExpressionExtensions,StringExpressionExtensions.(
gt(value)andgt(expr)), covering parameter and column-to-column comparisons.between/notBetweensupport partial ranges: both bounds yields BETWEEN, one bound yieldsa one-sided comparison, neither is skipped. A
ClosedRangeoverload (age between (20..60)) isalso provided.
and/or/xor/xnor/notinExpressionExtensions.ktarefolded into null-safe variants (
and/orwith partial application, the others skip on null).Compatibility
eq/ne/in/gt/goe/lt/loe/between/like/contains/
startsWith/endsWithreuse member-backed names, so member-over-extension keeps existingnon-null calls intact.
and/or/xor/xnor/notchanges their return type toBooleanExpression?. In practiceBooleanExpression.and/or/notstay non-null via their membermethods, so only raw
Expression<Boolean>callers are affected. I'll leave the release andversion placement entirely to your judgement.
Tests
both the value and expression overloads; partial application for
between/notBetween/and/or.CollectionTeststill passes. 39 tests total, green with the JDK 17 compile target andthe JDK 25 build toolchain.