Skip to content

Commit 730113b

Browse files
authored
feat(go): add the ability to select a package name for Go (bytecodealliance#915)
this commit adds an extra flag on the wit-bindgen tiny-go to be able to rename the package name of the generated Go source code. it also changes the file names from kebab names to snake names which follow the Go file name conventions. Signed-off-by: Jiaxiao Zhou (Mossaka) <[email protected]>
1 parent b3d53d3 commit 730113b

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

crates/go/src/lib.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ pub struct Opts {
2121
/// Whether or not `gofmt` is executed to format generated code.
2222
#[cfg_attr(feature = "clap", arg(long))]
2323
pub gofmt: bool,
24+
25+
/// Rename the Go package in the generated source code.
26+
#[cfg_attr(feature = "clap", arg(long))]
27+
pub rename_package: Option<String>,
2428
}
2529

2630
impl Default for Opts {
2731
fn default() -> Self {
28-
Self { gofmt: true } // Set the default value of gofmt to true
32+
Self {
33+
gofmt: true,
34+
rename_package: None,
35+
} // Set the default value of gofmt to true
2936
}
3037
}
3138

@@ -147,8 +154,11 @@ impl TinyGo {
147154

148155
impl WorldGenerator for TinyGo {
149156
fn preprocess(&mut self, resolve: &Resolve, world: WorldId) {
150-
let name = &resolve.worlds[world].name;
151-
self.world = name.to_string();
157+
self.world = self
158+
.opts
159+
.rename_package
160+
.clone()
161+
.unwrap_or_else(|| resolve.worlds[world].name.clone());
152162
self.sizes.fill(resolve);
153163
self.world_id = Some(world);
154164
}
@@ -309,13 +319,10 @@ impl WorldGenerator for TinyGo {
309319
self.src.append_src(&self.preamble);
310320
}
311321
self.src.push_str("import \"C\"\n");
312-
let world = &resolve.worlds[id];
322+
let world = self.world.to_snake_case();
313323

314-
self.import_requirements.generate(
315-
snake,
316-
files,
317-
format!("{}_types.go", world.name.to_kebab_case()),
318-
);
324+
self.import_requirements
325+
.generate(snake, files, format!("{}_types.go", world));
319326
self.src.push_str(&self.import_requirements.src);
320327

321328
self.src.push_str(&src);
@@ -342,14 +349,12 @@ impl WorldGenerator for TinyGo {
342349
let status = child.wait().expect("failed to wait on gofmt");
343350
assert!(status.success());
344351
}
345-
files.push(
346-
&format!("{}.go", world.name.to_kebab_case()),
347-
self.src.as_bytes(),
348-
);
352+
files.push(&format!("{}.go", world), self.src.as_bytes());
349353

350354
let mut opts = wit_bindgen_c::Opts::default();
351355
opts.no_sig_flattening = true;
352356
opts.no_object_file = true;
357+
opts.rename_world = self.opts.rename_package.clone();
353358
opts.build()
354359
.generate(resolve, id, files)
355360
.expect("C generator should be infallible");

crates/go/tests/codegen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ macro_rules! codegen_test {
2929
test_helpers::codegen_tests!();
3030

3131
fn verify(dir: &Path, name: &str) {
32-
let name = name.to_kebab_case();
32+
let name = name.to_snake_case();
3333
let main = dir.join(format!("{name}.go"));
3434

3535
// The generated go package is named after the world's name.

0 commit comments

Comments
 (0)