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

[Bug] Swift types prefixed by module name #22

Open
charypar opened this issue Nov 8, 2022 · 1 comment
Open

[Bug] Swift types prefixed by module name #22

charypar opened this issue Nov 8, 2022 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@charypar
Copy link

charypar commented Nov 8, 2022

🐛 Bug

Generated swift code, which includes types that reference each other, prefixes local references with module name provided to CodeGeneratorConfig. The code fails to compile with an error "Cannot find type '' in scope".

The problem can be worked around by searching and replacing the module name followed by "." (e.g. "module.") with an empty string, effectively stripping the namespacing out of the generated code.

To reproduce

Types to be traced

#[derive(Serialize, Deserialize)]
pub struct Request {
    pub uuid: Vec<u8>,
    pub body: RequestBody,
}

#[derive(Serialize, Deserialize)]
pub enum RequestBody {
   // ... variants
}

Tracing and generating build.rs script

use rmm::{Request, RequestBody};
use serde_reflection::{Tracer, TracerConfig};
use std::{fs::File, io::Write, path::PathBuf};

fn main() {
    let mut tracer = Tracer::new(TracerConfig::default());
    tracer.trace_simple_type::<Request>().unwrap();
    tracer.trace_simple_type::<RequestBody>().unwrap();
    let registry = tracer.registry().unwrap();

    // Create Swift definitions.
    let mut source = Vec::new();
    let config = serde_generate::CodeGeneratorConfig::new("shared".to_string())
        .with_encodings(vec![serde_generate::Encoding::Bincode]);

    let generator = serde_generate::swift::CodeGenerator::new(&config);
    generator.output(&mut source, &registry).unwrap();

    let path = "./generated/shared_types.swift";
    let mut output = File::create(path).unwrap();
    write!(output, "{}", out).unwrap();
}

Relevant part of the generated Swift code

public struct Response: Hashable {
    @Indirect public var uuid: [UInt8]
    @Indirect public var body: shared.ResponseBody // "Error: Cannot find type 'shared' in scope"

    (...)

Removing shared. from the above resolves the problem.

Expected Behavior

Generated Swift code should compile without errors.

System information

Serde reflection v0.3.6
Serde generate v0.24.0

@charypar charypar added the bug Something isn't working label Nov 8, 2022
@charypar charypar changed the title [Bug] Swift types prefixed by package name [Bug] Swift types prefixed by module name Nov 8, 2022
@ma2bd ma2bd self-assigned this Dec 31, 2022
@ma2bd
Copy link
Contributor

ma2bd commented Dec 31, 2022

@charypar Thanks for the report. IIRC prefixing identifiers with the module name is useful. Have you tried a setup more similar to the test file swift_generation.rs ? (E.g. in this test, the module "Testing" matches the file name "Testing.swift". Your module is "shared" and the file is "shared_types.swift". Perhaps this helps?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants