cretz / pb-and-k

Kotlin Code Generator and Runtime for Protocol Buffers
MIT License
138 stars 15 forks source link

Is it possible to support generate .proto file from a .kt model file? #9

Open zhenghuiy opened 5 years ago

zhenghuiy commented 5 years ago

I already have a .kt file and want generate .proto file so that using pb.

cretz commented 5 years ago

That's not a feature of this library. In general I don't suggest this approach. If you have the choice to write one and generate the other, write the .proto and generate the .kt. If you already have the Kotlin type and want to create a protobuf type from it, I suggest instead manually creating a .proto to do what you need and manually write the translation to/from your Kotlin type to/from the proto Kotlin type.

There is too much impedance mismatch to expect protobuf will be able to support enough of the Kotlin type requirements to make this effort worthwhile.

zhenghuiy commented 5 years ago

That's not a feature of this library. In general I don't suggest this approach. If you have the choice to write one and generate the other, write the .proto and generate the .kt. If you already have the Kotlin type and want to create a protobuf type from it, I suggest instead manually creating a .proto to do what you need and manually write the translation to/from your Kotlin type to/from the proto Kotlin type.

There is too much impedance mismatch to expect protobuf will be able to support enough of the Kotlin type requirements to make this effort worthwhile.

Thanks for suggestion. I have a complicated .kt data class file and I use json to do Serialization and deserialization. However this takes about 100 - 200 ms per model. So I want improve it.

I think pb may help, but it is not easy to write .proto file manually. Because the fileds in model is changing and the model structure is complicated.

I think I should find an Android studio plugin to check if model is changed and auto generate pb.

cretz commented 5 years ago

Protobuf is just better to write and generate from than the opposite. I doubt you'll find any good code-to-proto generators in any language, they'd have limitations, especially for a JVM language. You really should manually write the proto files, there's not another option that I'm aware of.

phcoder commented 5 years ago

I think kotlinx-io-serialization is what you're looking for