-
Notifications
You must be signed in to change notification settings - Fork 63
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
Nullable accessors and fixed convertTo #175
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
da18b89
TODO tests, added nullable accessors for DataRow<Object?> since its p…
Jolanrensen 81f2eed
added toNullable converters for FieldType column types and added test…
Jolanrensen 046b490
fixed more tests
Jolanrensen 7d9a199
fixed more tests
Jolanrensen 20ae0d7
fixed convertTo for nullable sub-objects
Jolanrensen 8a3a396
added test that broke before
Jolanrensen 6d90246
added test that requires other fix
Jolanrensen 33fd23a
Merge branch 'master' into nullable-accessors
Jolanrensen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,58 @@ public sealed interface FieldType { | |
public class GroupFieldType(public val markerName: String) : FieldType | ||
} | ||
|
||
/** | ||
* Returns whether the column type ends with `?` or not. | ||
* NOTE: for [FieldType.FrameFieldType], the `nullable` property indicates the nullability of the frame itself, not the type of the column. | ||
*/ | ||
public fun FieldType.isNullable(): Boolean = | ||
when (this) { | ||
is FieldType.FrameFieldType -> markerName.endsWith("?") | ||
is FieldType.GroupFieldType -> markerName.endsWith("?") | ||
is FieldType.ValueFieldType -> typeFqName.endsWith("?") | ||
} | ||
|
||
/** | ||
* Returns whether the column type doesn't end with `?` or whether it does. | ||
* NOTE: for [FieldType.FrameFieldType], the `nullable` property indicates the nullability of the frame itself, not the type of the column. | ||
*/ | ||
public fun FieldType.isNotNullable(): Boolean = !isNullable() | ||
|
||
private fun String.toNullable() = if (this.last() == '?') this else "$this?" | ||
|
||
/** | ||
* Returns a new fieldType with the same type but with nullability in the column type. | ||
* NOTE: for [FieldType.FrameFieldType], the `nullable` property indicates the nullability of the frame itself, not the type of the column. | ||
*/ | ||
public fun FieldType.toNullable(): FieldType = | ||
if (isNotNullable()) { | ||
when (this) { | ||
is FieldType.FrameFieldType -> FieldType.FrameFieldType(markerName.toNullable(), nullable) | ||
is FieldType.GroupFieldType -> FieldType.GroupFieldType(markerName.toNullable()) | ||
is FieldType.ValueFieldType -> FieldType.ValueFieldType(typeFqName.toNullable()) | ||
} | ||
} else this | ||
|
||
/** | ||
* Returns a new fieldType with the same type but with nullability disabled in the column type. | ||
* NOTE: for [FieldType.FrameFieldType], the `nullable` property indicates the nullability of the frame itself, not the type of the column. | ||
*/ | ||
public fun FieldType.toNotNullable(): FieldType = | ||
if (isNullable()) { | ||
when (this) { | ||
is FieldType.FrameFieldType -> FieldType.FrameFieldType(markerName.removeSuffix("?"), nullable) | ||
is FieldType.GroupFieldType -> FieldType.GroupFieldType(markerName.removeSuffix("?")) | ||
is FieldType.ValueFieldType -> FieldType.ValueFieldType(typeFqName.removeSuffix("?")) | ||
} | ||
} else this | ||
|
||
public val FieldType.name: String | ||
get() = when (this) { | ||
is FieldType.FrameFieldType -> markerName | ||
is FieldType.GroupFieldType -> markerName | ||
is FieldType.ValueFieldType -> typeFqName | ||
} | ||
|
||
public class ValidFieldName private constructor(private val identifier: String, public val needsQuote: Boolean) { | ||
public val unquoted: String get() = identifier | ||
public val quotedIfNeeded: String get() = if (needsQuote) "`$identifier`" else identifier | ||
|
@@ -48,6 +100,22 @@ public interface BaseField { | |
public val fieldType: FieldType | ||
} | ||
|
||
public fun BaseField.toNullable(): BaseField = | ||
if (fieldType.isNullable()) this | ||
else object : BaseField { | ||
override val fieldName: ValidFieldName = [email protected] | ||
override val columnName: String = [email protected] | ||
override val fieldType: FieldType = [email protected]() | ||
} | ||
|
||
public fun BaseField.toNotNullable(): BaseField = | ||
if (fieldType.isNotNullable()) this | ||
else object : BaseField { | ||
override val fieldName: ValidFieldName = [email protected] | ||
override val columnName: String = [email protected] | ||
override val fieldType: FieldType = [email protected]() | ||
} | ||
|
||
public data class GeneratedField( | ||
override val fieldName: ValidFieldName, | ||
override val columnName: String, | ||
|
This file contains 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
This file contains 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
Oops, something went wrong.
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.
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.
Do we need it?
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, I'm not sure. I needed toNullable, but for completeness if we need it in the future I thought it best to also create a reverse option. Might save time to figure out how it works again