Skip to content

protoc-gen: Refactor optional field generation (KRPC-262)#644

Open
Jozott00 wants to merge 11 commits intomainfrom
pb/optional-fields
Open

protoc-gen: Refactor optional field generation (KRPC-262)#644
Jozott00 wants to merge 11 commits intomainfrom
pb/optional-fields

Conversation

@Jozott00
Copy link
Copy Markdown
Contributor

@Jozott00 Jozott00 commented Apr 3, 2026

Subsystem
Protobuf/protoc-gen

Problem Description
Currently, optional fields that lack a user-defined default value are generated as nullable properties, with the exception of sub-message fields.

This implementation differs from the standard protobuf approach, which handles all optional fields as non-nullable. This allows users to quickly access default values for non-set subfields. Furthermore, in the edition 2023+ version, where all fields are typically optional by default, managing nullable properties becomes cumbersome.

Solution
This PR aligns the implementation with official Protobuf implementations, ensuring that all fields (except one-of fields that lack a default value) are non-nullable. However, to maintain Kotlin’s idiomatic nullable handling, users can enable additional nullable getter generation, which produces

// in MyProto.ext.kt
val MyMessage.myFieldOrNull: FieldType?

Additionally, MyMessage.Builder.clear<Field>() functions are generated for all fields that are presence tracked. This enables users to clear the original set value in the copy block

val copy = message.copy {
  clearMyField()
}

@Jozott00 Jozott00 self-assigned this Apr 3, 2026
@Jozott00 Jozott00 added feature New feature or request gRPC labels Apr 3, 2026
@Jozott00
Copy link
Copy Markdown
Contributor Author

Jozott00 commented Apr 3, 2026

Not yet implemented:

  • Gradle settings to enable nullabe extension getters
  • Documentation update

@Jozott00 Jozott00 force-pushed the pb/optional-fields branch from 909b5cd to 9ebd906 Compare April 4, 2026 10:58
Comment on lines +53 to +57
internal object FirGeneratedProtoMessageBuilderFunctionKey : GeneratedDeclarationKey() {
override fun toString(): String {
return "FirGeneratedProtoMessageBuilderFunctionKey"
}
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure if this is actually needed, or if we could just use the PropertyKey

@Jozott00 Jozott00 marked this pull request as ready for review April 4, 2026 11:13
@Jozott00 Jozott00 requested a review from Mr3zee April 4, 2026 11:14
@Mr3zee Mr3zee removed the gRPC label Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants