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

Be more graceful when handing null values #3245

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class MutablePayload(

override fun toString(): String {
val result = mutableListOf<String>()
if (preamble != null) result += """preamble=${sanitize(preamble!!)}"""
Copy link
Member

Choose a reason for hiding this comment

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

If preamble is not null when would preamble ?: "" make sense?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think doing ?.let would be best, but there's that compiler bug with them...

Copy link
Contributor Author

@tikurahul tikurahul Jan 11, 2025

Choose a reason for hiding this comment

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

Was the thought that it's confusing to read ? I guess we are doing more work than we need to strictly speaking.

I considered one other thing which is to make sanitize(...) accept String? but I would have to rename the list variant to sanitizeList to disambiguate. Maybe we should do that instead ?

if (preamble != null) result += """preamble=${sanitize(preamble ?: "")}"""
if (content != null) result += """content=$content"""
if (type != null) result += """type=$type"""
if (footers.isNotEmpty()) result += """footers=${sanitize(footers)}"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,7 @@ class KotlinGenerator private constructor(
} else {
if (fieldOrOneOf.type == ProtoType.STRING) {
if (mutableTypes && !fieldOrOneOf.isRepeated) {
add("=\${%M(%N!!)}", sanitizeMember, fieldName)
add("=\${%M(%N ?: %L)}", sanitizeMember, fieldName, "\"\"")
} else {
add("=\${%M(%N)}", sanitizeMember, fieldName)
}
Expand Down
Loading