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

Implement the new proto3 field presence feature #34

Closed
garyp opened this issue Apr 24, 2020 · 1 comment · Fixed by #200
Closed

Implement the new proto3 field presence feature #34

garyp opened this issue Apr 24, 2020 · 1 comment · Fixed by #200
Labels
enhancement New feature or request
Milestone

Comments

@garyp
Copy link
Collaborator

garyp commented Apr 24, 2020

See protocolbuffers/protobuf#1606 (comment) and https://github.com/protocolbuffers/protobuf/blob/master/docs/implementing_proto3_presence.md for details.

When this feature is in use, pbandk should generate code for primitive fields using a nullable type that defaults to null, rather than a non-null type that defaults to empty string, zero, false, etc.

E.g., current code for this protobuf definition:

syntax = "proto3";

message Foo {
  int32 foo = 1;
}

will look like:

data class Foo(val foo: Int = 0)

Using the new optional presence feature, this protobuf definition:

syntax = "proto3";

message Foo {
  optional int32 foo = 1;
}

should look like:

data class Foo(val foo: Int? = null)
@garyp garyp added the enhancement New feature or request label Apr 24, 2020
@garyp garyp added this to the 1.0 milestone Apr 29, 2020
NiematojakTomasz added a commit to NiematojakTomasz/pbandk that referenced this issue Dec 16, 2021
NiematojakTomasz added a commit to NiematojakTomasz/pbandk that referenced this issue Dec 17, 2021
NiematojakTomasz added a commit to NiematojakTomasz/pbandk that referenced this issue Dec 18, 2021
NiematojakTomasz added a commit to NiematojakTomasz/pbandk that referenced this issue Dec 18, 2021
NiematojakTomasz added a commit to NiematojakTomasz/pbandk that referenced this issue Jan 20, 2022
NiematojakTomasz added a commit to NiematojakTomasz/pbandk that referenced this issue Jan 20, 2022
NiematojakTomasz added a commit to NiematojakTomasz/pbandk that referenced this issue Jan 20, 2022
NiematojakTomasz added a commit to NiematojakTomasz/pbandk that referenced this issue Jan 20, 2022
NiematojakTomasz added a commit to NiematojakTomasz/pbandk that referenced this issue Jan 20, 2022
garyp pushed a commit that referenced this issue Mar 29, 2022
Fields marked with the `optional` keyword in `proto3` files will now result in nullable Kotlin types in the generated code.

Fixes #34.
@garyp garyp modified the milestones: 1.0, 0.14.0 Mar 29, 2022
@Maragues
Copy link

Maragues commented Jun 25, 2024

Hi, I'm experiencing a problem with nullability generation. My scenario is a bit different in that I use .ptbtxt as input, not .proto, and protobuf-gradle-plugin.

Given this input

file {
  name: "data_models.proto"
  package: "my.package"
  message_type {
    name: "User"
    field {
      name: "name"
      number: 2
      label: LABEL_OPTIONAL
      type: TYPE_STRING
    }

I get this kotlin class

@pbandk.Export
data class User(
    val name: String = "",

I need name to be of type String?, not String.

I'm using pbandk 0.14.3. This is the protoc command

/.../protoc-3.21.9-osx-x86_64.exe
--plugin=protoc-gen-pbandk=<some-module-path>>/scripts/protoc-gen-pbandk-jvm-0.14.3-jvm8-generateProto-trampoline.sh
--pbandk_out=log=debug:<some-module-path>>/generated/source/proto/main/pbandk
--descriptor_set_in=/tmp/data_models.pb
data_models.proto

Would you expect this to generate null types, or do .proto files produce something different than text format?


Technical details, in case it's relevant

I parse pbtxt to a FileDescriptorSet, and write it to a temporary file. I checked that the FieldDescriptorProto.label is still LABEL_OPTIONAL

file.inputStream()
  .use { inputStream: FileInputStream ->
    FileDescriptorSet.parseFrom(inputStream.readAllBytes()).also {
      it.getFile(0).messageTypeList.forEach { message ->
        println("Message ${message.name}")

        message.fieldList.forEach { field: FieldDescriptorProto ->
            println("\t${field.name}, label ${field.label}")
        }
      }
    }
  }

prints

Message User
        name, label LABEL_OPTIONAL

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

Successfully merging a pull request may close this issue.

2 participants