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 settings option to easily rename produced Client struct #837

Open
mfpekala opened this issue Jun 21, 2024 · 3 comments
Open

Add settings option to easily rename produced Client struct #837

mfpekala opened this issue Jun 21, 2024 · 3 comments

Comments

@mfpekala
Copy link

There does not seem to be an easy way to change the name of the produced struct. I'm developing an application which may interface with multiple openapi servers, and it would be convenient to give Client a more descriptive name.

@ahl
Copy link
Collaborator

ahl commented Jun 22, 2024

Usually what I've seen in this situation is something like this:

mod foo {
    generate_api!(
        spec = "foo.json",
        interface = Builder,
    }l
}

mod bar {
    generate_api!(
        spec = "bar.json",
        interface = Builder,
    }l
}

use foo::Client as FooClient;
use bar::Client as BarClient;

Would something like that work for you? If not, how would you ideally imagine specifying the client type name?

@mfpekala
Copy link
Author

mfpekala commented Jun 25, 2024

I've got a project where I've put the progenitor-generated clients inside of their own crates, and am generating each client inside that crate's build.rs so it auto-updates. In the future I may put more logic around each client and would prefer to leave them as separate crates so that the main logic doesn't have to worry about generation at all.

So the build.rs inside this crate ends up looking pretty standard (I think):

let settings = progenitor::GenerationSettings::default();
let mut generator = progenitor::Generator::new(&settings);
let tokens = generator.generate_tokens(&spec).unwrap();
let ast = syn::parse2(tokens).unwrap();
let mut content = prettyplease::unparse(&ast);
// Then write this content to file...

I would love if there was something on GenerationSettings like with_client_struct_name that could edit the settings in this example to provide a custom name.

The solution you gave is definitely very clean, and I ended up at something similar for my situation, but I can't help but feel like this makes sense as something in GenerationSettings. Not a necessity at all but something that could be nice. Thanks!

@ahl
Copy link
Collaborator

ahl commented Jun 25, 2024

Not unreasonable. Let me think about it and see if I can come up with something we can commit to.

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

2 participants