andrewhickman / prost-reflect

A protobuf library extending prost with reflection support and dynamic messages.
https://crates.io/crates/prost-reflect
Apache License 2.0
91 stars 21 forks source link

Generate protobuf schema from a type that derives `ReflectMessage` #48

Closed HKalbasi closed 1 year ago

HKalbasi commented 1 year ago

Similar to how schemars generates json schemas from Rust structs. It seems unlike prost::Message, the ReflectMessage crate has the necessary ingredients for implementing such a feature. Do you think it is possible? And if so, do you consider it in scope for this project?

andrewhickman commented 1 year ago

I don't think that would be in scope for this project. However I think it should be possible to implement using the public API of MessageDescriptor - that's what is used for JSON serialization internally

HKalbasi commented 1 year ago

Thanks for quick response. I will try to implement it using MessageDescriptor and publish it as a separate crate.

HKalbasi commented 1 year ago

I looked at the api and wonder why ReflectMessage::descriptor needs &self? Isn't it something that could be derived from the type itself?

andrewhickman commented 1 year ago

It needs &self so it can be implemented for DynamicMessage. All message types generated by prost-build should have a cheap Default implementation, so its possible to get the descriptor from the type by doing T::default().descriptor()