2

I don't understand why an optional field (like middle_name in the example bellow), is not nullable in the kotlin generated representation while in golang and rust is properly nullable (or optional).

syntax = "proto3";

message User{
  string id = 1;
  string name = 2;
  optional string middle_name = 3;
  string last_name = 4;
}

Kotlin generated code

...
public fun hasMiddleName(): kotlin.Boolean{...}
public var middleName: kotlin.String{...}
...

Rust generated code

...
pub middle_name: ::std::option::Option<::std::string::String>,
...

Golang generated code

...
MiddleName *string `protobuf:"bytes,3,opt,name=middle_name,json=middleName,proto3,oneof" json:"middle_name,omitempty"`
...

The only way that I've found is using takeIf method on every attribute access, absolutely not error-proof (forgetting the takeIf check the code still works)

myProto.takeIf{it.hasMiddleName()}?.middleName
n3wtron
  • 73
  • 6

0 Answers0