-
Notifications
You must be signed in to change notification settings - Fork 526
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
Custom options? #256
Comments
I'm into this as well at the moment. I followed the proto3 docs to add a custom option and prost fails to read in the files when I don't import the correct defintion so I know it's doing something; but nevertheless |
So looking at a syntax = "proto3";
package test;
import "google/protobuf/descriptor.proto";
extend google.protobuf.MessageOptions {
int32 message_opt1 = 7739036;
}
message Test {
option (message_opt1) = 420000;
option no_standard_descriptor_accessor = true;
string content = 1;
} and calling {
"name": "Test",
"field": [
{
"name": "content",
"number": 1,
"label": 1,
"type": 9,
"json_name": "content"
}
],
"options": {
"no_standard_descriptor_accessor": true
}
} To me, this looks like there are no Full JSON output -- quite long |
|
Ah, I should've probably explained that. I've patched prost-build locally to also give me a |
I see. It's a longer-term goal to expose message descriptors in generated code in some way, but it's going to need some design. Having a PR to go off might be very helpful! Back to the question at hand - |
I looked into this for project-oak/oak#668, and the issue seems to be that whatever parses the Judging from docs here, no It seems to me that for this to work prost would need to support extensions, which I don't think it currently does? |
I was able to extract field options with a very small patch to the I was talking to @tiziano88 about generalizing this, and we thought perhaps a flag can be added to #[derive(Clone, PartialEq, ::prost::Message)]
struct MyProtoMessage {
// ...
#[prost(unknown_fields)]
pub unknown_fields: HashMap<u64, Bytes>,
} Note that this is slightly more generic than just extensions, but those would be the primary use case. Enabling this flag when generating the code for The invocation of Config::new().unknown_fields("MyProtoMessage").compile_protos(...) I'm planning to write a PR that implements this, happy to hear any feedback on the design above 😄. |
@wildarch I'm slightly confused (maybe I just need more coffee) -- what is MyProtoMessage here? A message or a custom type for representing the structure of proto messages? Can your approach cover what I wrote in https://github.com/danburkert/prost/issues/256#issuecomment-591901770 or would I need to special-case every single one of my messages? |
Changes to struct MessageOptions {
... (all the same)
#[prost(unknown_fields)]
pub unknown_fields: HashMap<u64, Bytes>,
} If you have a reference to the I should note that while the impl Config {
pub fn override_field(&mut self, field_descriptor: &FieldDescriptorProto) -> Option<(Name, Type)>
} We could have similar things for messages, would that cover your use case? Alternatively, once we have runtime introspection you could use that to get the option value. |
I am using prost with a
If I compile the file with protoc to a FileDescriptorSet and decode that with If I understand things correctly, the options should appear in Edit: I think this needs extension support (#100 )? |
OK, with #317 plus the changes I linked in my comment, I have working custom options for my service generator! With #317, in my build.rs, I still have to set the tags manually (because prost doesn't support extensions), and the value has to be parsed manually, but that's allright for me so far. For reference, and for others following along, this is what I arrived at to parse an uint64 "id" option on both services and methods (depending on #317): pub trait MyOptions {
fn get_unknown_fields(&self) -> &Vec<prost::UnknownField>;
fn id_tag(&self) -> u32;
fn id(&self) -> Option<u64> {
let tag = self.id_tag();
let fields = self.get_unknown_fields();
let field = fields.iter().filter(|f| f.tag == tag).nth(0)?;
let mut id = 0u64;
varinteger::decode(&field.value[..], &mut id);
Some(id)
}
}
impl MyOptions for prost_types::MethodOptions {
fn get_unknown_fields(&self) -> &Vec<prost::UnknownField> {
&self.protobuf_unknown_fields
}
fn id_tag(&self) -> u32 {
50001
}
}
impl MyOptions for prost_types::ServiceOptions {
fn get_unknown_fields(&self) -> &Vec<prost::UnknownField> {
&self.protobuf_unknown_fields
}
fn id_tag(&self) -> u32 {
50000
}
} And then, in my service generator, I can access the id on So - would be great to get #317 in 🚀 |
I'm having this problem as well - I have custom options in my .proto file and the uninterpreted options vec is always empty. I need access to the custom options when generating my service.. |
Pushing this one. Would be really nice, |
I would also like to see this resolved. I am trying to define custom options on a service method, and I ended up here whilst trying to figure out why |
The |
Are the custom options supported?
I tried options and uninterpreted_option without success.
Thanks
Laurent
The text was updated successfully, but these errors were encountered: