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

Avoid pointer redirection for Optional slices in Go #198

Merged
merged 9 commits into from
Sep 24, 2024
1 change: 1 addition & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub struct TypeScriptParams {
pub struct GoParams {
pub package: String,
pub uppercase_acronyms: Vec<String>,
pub no_pointer_slice: bool,
hculea marked this conversation as resolved.
Show resolved Hide resolved
pub type_mappings: HashMap<String, String>,
}

Expand Down
1 change: 1 addition & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ fn language(
package: config.go.package,
type_mappings: config.go.type_mappings,
uppercase_acronyms: config.go.uppercase_acronyms,
no_pointer_slice: config.go.no_pointer_slice,
..Default::default()
}),
#[cfg(not(feature = "go"))]
Expand Down
12 changes: 11 additions & 1 deletion core/src/language/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct Go {
/// Whether or not to exclude the version header that normally appears at the top of generated code.
/// If you aren't generating a snapshot test, this setting can just be left as a default (false)
pub no_version_header: bool,
/// Whether or not slices should be translated with a pointer redirection.
pub no_pointer_slice: bool,
}

impl Language for Go {
Expand Down Expand Up @@ -102,7 +104,15 @@ impl Language for Go {
format!("[]{}", self.format_type(rtype, generic_types)?)
}
SpecialRustType::Option(rtype) => {
format!("*{}", self.format_type(rtype, generic_types)?)
format!(
"{}{}",
if rtype.is_vec() && self.no_pointer_slice {
""
} else {
"*"
},
self.format_type(rtype, generic_types)?
)
}
SpecialRustType::HashMap(rtype1, rtype2) => format!(
"map[{}]{}",
Expand Down
Loading