-
Notifications
You must be signed in to change notification settings - Fork 56
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
refactor: Make [non] nullable struct fields easier to create #646
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #646 +/- ##
==========================================
- Coverage 83.68% 83.66% -0.02%
==========================================
Files 75 75
Lines 16955 16909 -46
Branches 16955 16909 -46
==========================================
- Hits 14188 14147 -41
+ Misses 2102 2099 -3
+ Partials 665 663 -2 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whew, (checked each line haha) LGTM!
/// Creates a new nullable field | ||
pub fn nullable(name: impl Into<String>, data_type: impl Into<DataType>) -> Self { | ||
Self::new(name, data_type, true) | ||
} | ||
|
||
/// Creates a new non-nullable field | ||
pub fn not_null(name: impl Into<String>, data_type: impl Into<DataType>) -> Self { | ||
Self::new(name, data_type, false) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe (this is a big maybe - might not be useful) we should call out in doc comments either here or in the new
constructor that these exist just as shortcuts basically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Splitting up the commits was really clever! Made reviewing a breeze. LGTM
What changes are proposed in this pull request?
A lot of code (especially in tests) calls
StructField::new
with literal values of thenullable: bool
argument. Booleans are easy to misinterpret (which value means non-null field?) -- and it's hard to read when the third arg is split to its own line in nested expressions such as:To improve readability and make the code less error-prone, define two new helper methods/constructors for
StructField
:nullable
andnot_null
, which create struct fields having the corresponding nullability.How was this change tested?
No new functionality, and all existing unit tests still pass.
To minimize the risk of unfaithful refactoring, the change was made in four steps:
StructField::new(\([^()]*\), true) → StructField::nullable(\1)
, which ignores any constructor call containing parentheses, to avoid ambiguity.StructField::new(\([^()]*\), false) → StructField::not_null(\1)
, to convert simple usenot_null
call sites (see above for details).StructField::new → StructField::nullable
, relying on IDE parentheses matching to identify calls that pass the literaltrue
(first pass). As a safety measure, the resulting code is compiled; all changed call sites fail to compile because of the (now unrecognized) third arg, which can then be deleted after verifying it is the literaltrue
.StructField::new → StructField::not_null
with literalfalse
.Each step is its own commit, for easier verification.