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

Change non_null option to generate non-null accessors backed by nullable properties #301

Merged

Conversation

andrewparmet
Copy link
Collaborator

@andrewparmet andrewparmet commented Dec 1, 2024

Replacement for #274. Instead of generating code with a non-null property that fails to deserialize, generate a nullable backing property (with the original name and the GeneratedProperty annotation) and a delegating non-nullable property with the prefix require.

Renames the non_null option to generate_non_null_accessor.

Code excerpts:

Basic usage of the option; declaring that a field ought to be present:

@GeneratedMessage("protokt.v1.testing.NonNullModel")
public class NonNullModel private constructor(
  @GeneratedProperty(1)
  public val nonNullStringValue: String?,
  public val unknownFields: UnknownFieldSet = UnknownFieldSet.empty()
) : AbstractMessage() {

  public val requireNonNullStringValue: String
    get() =
      requireNotNull(nonNullStringValue) {
        "nonNullStringValue is assumed non-null with (protokt.property).generate_non_null_accessor but was null"
      }

Implementing an interface by a delegate (note that the overridden property narrows the type of id to String):

@GeneratedMessage("protokt.v1.testing.ImplementsWithDelegate")
public class ImplementsWithDelegate private constructor(
  @GeneratedProperty(1)
  public val modelTwo: ImplementsModel2?,
  public val unknownFields: UnknownFieldSet = UnknownFieldSet.empty()
) : AbstractMessage(),
  IModel2 {
  override val id: String
    get() = requireModelTwo.id

  public val requireModelTwo: ImplementsModel2
    get() =
      requireNotNull(modelTwo) { "modelTwo is assumed non-null with (protokt.property).generate_non_null_accessor but was null" }

@andrewparmet andrewparmet marked this pull request as ready for review December 1, 2024 18:53
@andrewparmet andrewparmet requested a review from ogolberg December 1, 2024 18:53
@andrewparmet andrewparmet merged commit bd361c2 into open-toast:main Dec 5, 2024
19 checks passed
@andrewparmet andrewparmet deleted the initial-work-on-required-getters branch December 5, 2024 04:04
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