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

generate classes with non-null Message types with custom options / extensions #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Dogacel
Copy link
Contributor

@Dogacel Dogacel commented Nov 18, 2022

Refers to

Overview

Allow not nullable Message types to be generated by using custom protobuf options or extensions.

  • Added tests to verify stuff are working fine.

How

  • Add a new option called required_field_extension_number.
  • Update CodeGenerator to allow nullable MESSAGE type and set the default value to be the constructor invocation (default instance)

Risks

  • Extension number clashing, I find it unlikely but could be nice to double-check.
  • Circular dependency, I left this one to the user. Circular paths with not nullable messages lead to infinite recursion.

Comment on lines +511 to +516
protected val File.Field.Numbered.allowNulls: Boolean
get() = requiredFieldNumber?.let {
// 1 means `required = true` so we can't allow nulls
this.options.unknownFields[it]?.values?.get(0)?.rawBytes?.array?.get(0)?.toInt() != 1
} ?: true
protected fun File.Field.Numbered.kotlinValueType(allowNulls: Boolean = this.allowNulls): String = when (this) {
Copy link
Contributor

Choose a reason for hiding this comment

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

what do you think about putting something like

val isRequired: Boolean
            get() = rawBytes.array.first().toInt() != 1

in the UnknownField.Value class and then having

options.unknownFields[it]?.values?.firstOrNull()?.isRequired == true

here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe not isRequired because it is just a boolean. We need to smart cast those bytes to a boolean, maybe something like asBoolean() is a better candidate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants