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

add a use super::* from modules for enum implementations #244

Closed
rrichardson opened this issue Nov 27, 2019 · 6 comments
Closed

add a use super::* from modules for enum implementations #244

rrichardson opened this issue Nov 27, 2019 · 6 comments

Comments

@rrichardson
Copy link

Hey all. I'm trying to take a stab at enabling the JSON portion of conformance tests as per @danburkert 's suggestion in #75

I have attempted to add the use serde::Serialize and include it in the Cargo.toml accordingly. This so that when it is included here it should have the appropriate dependencies.

However, I am running into the error below, the problem is that there are modules declared in within the scope of the generated struct files. We need some way to instruct prost to inject some statements such as use ... at the top of each module that it creates.

Would it be reasonable to add an use super::* at the top of every module that is created for protobuf enums? This would allow any dependencies to be retrieved and would hopefully work as intended. If that won't work.. we might need a Config method that would allow us to inject arbitrary lines.

...
error: cannot find derive macro `Serialize` in this scope
   --> /home/rick/Projects/prost/target/debug/build/protobuf-25dcd2b80c599a45/out/conformance.rs:164:10
    |
164 | #[derive(Serialize)]
    |          ^^^^^^^^^

error: cannot find attribute `serde` in this scope
   --> /home/rick/Projects/prost/target/debug/build/protobuf-25dcd2b80c599a45/out/conformance.rs:165:3
    |
165 | #[serde(rename_all = "camelCase")]
    |   ^^^^^
...
@rrichardson
Copy link
Author

just hardcoding a use super::*; at the beginning of a mod does work.

However, in cases where there is nothing to import (which is most cases) it throws an unused import error. So the solution might have to be a bit more sophisticated.

@vincentdephily
Copy link

I've solved that particular issue by using the full path: #[derive(serde_derive::Serialize)].

I wonder how @adeschamps didn't need to do that, maybe something to do with rust2015 module system.

@rrichardson
Copy link
Author

Well that is certainly the right solution. It never occurred to me to use the "absolute" path of the module. I guess I just assumed that it wouldn't be available to the submodule either.

@danburkert
Copy link
Collaborator

I'm going to close this out, @vincentdephily's solution is the correct one. Please feel free to re-open if there's something more prost can do.

@yiv
Copy link

yiv commented Apr 15, 2020

@vincentdephily
how can i solve the problem of "use of undeclared type or module serde_derive"

fn main() -> Result<(), Box<dyn std::error::Error>> {
    tonic_build::configure()
        .build_server(true)
        .type_attribute(".", "#[derive(serde_derive::Serialize,serde_derive::Deserialize)]")
        .compile(
            &["./proto/user.proto","./proto/dispatch.proto"],
            &["./proto"],
        )?;
    Ok(())
}
 #[derive(Clone, PartialEq, ::prost::Message, serde_derive::Serialize, serde_derive::Deserialize)]
   |                                              ^^^^^^^^^^^^ use of undeclared type or module `serde_derive`

@vincentdephily
Copy link

@yiv I'm guessing you're missing serde_derive in your Cargo.toml ? I'm not sure what the relationship between tonic and prost is, please use tonic's bugtracker or convert your example to prost.

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

No branches or pull requests

4 participants