Skip to content

Commit 49089c5

Browse files
authored
feat(go): enable user to configure the package name for generated types (#1498)
* feat(go): enable user to configure the package name for generated types Signed-off-by: Andrew Steurer <94206073+asteurer@users.noreply.github.com> * fix(go): conditionally including main function Signed-off-by: Andrew Steurer <94206073+asteurer@users.noreply.github.com> --------- Signed-off-by: Andrew Steurer <94206073+asteurer@users.noreply.github.com>
1 parent 0c39eee commit 49089c5

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

crates/go/src/lib.rs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ fn remote_pkg(name: &str) -> String {
3838
format!(r#""github.com/bytecodealliance/wit-bindgen/{name}""#)
3939
}
4040

41-
/// Adds the bindings module prefix to a package name.
42-
fn mod_pkg(name: &str) -> String {
43-
format!(r#""wit_component/{name}""#)
44-
}
45-
4641
/// This is the literal location of the Go package.
4742
const REPLACEMENT_PKG: &str = concat!(
4843
"github.com/bytecodealliance/wit-bindgen/crates/go/src/package v",
@@ -105,6 +100,11 @@ pub struct Opts {
105100
/// resources.
106101
#[cfg_attr(feature = "clap", clap(long))]
107102
pub generate_stubs: bool,
103+
104+
/// The name of the Go module housing the generated bindings
105+
/// (or "wit_component" if `None`).
106+
#[cfg_attr(feature = "clap", clap(long))]
107+
pub mod_name: Option<String>,
108108
}
109109

110110
impl Opts {
@@ -193,6 +193,12 @@ struct Go {
193193
}
194194

195195
impl Go {
196+
/// Adds the bindings module prefix to a package name.
197+
fn mod_pkg(&self, name: &str) -> String {
198+
let prefix = self.opts.mod_name.as_deref().unwrap_or("wit_component");
199+
format!(r#""{prefix}/{name}""#)
200+
}
201+
196202
fn package_for_owner(
197203
&mut self,
198204
resolve: &Resolve,
@@ -214,7 +220,7 @@ impl Go {
214220
package
215221
};
216222
let prefix = format!("{package}.");
217-
imports.insert(mod_pkg(&package));
223+
imports.insert(self.mod_pkg(&package));
218224
prefix
219225
}
220226
}
@@ -843,13 +849,31 @@ impl WorldGenerator for Go {
843849
.collect::<Vec<_>>()
844850
.join("\n");
845851

852+
// When a custom module name is provided, the generated bindings use package name
853+
// "wit_component" so they can be imported into another package. Without a custom
854+
// module name, the bindings use package "main" for standalone execution.
855+
let package_name = if self.opts.mod_name.is_some() {
856+
"wit_component"
857+
} else {
858+
"main"
859+
};
860+
861+
// If the package name isn't "main", a main function isn't required.
862+
let main_func = if self.opts.mod_name.is_some() {
863+
""
864+
} else {
865+
r#"// Unused, but present to make the compiler happy
866+
func main() {}
867+
"#
868+
};
869+
846870
files.push(
847871
"wit_bindings.go",
848872
&maybe_gofmt(
849873
self.opts.format,
850874
format!(
851875
r#"{header}
852-
package main
876+
package {package_name}
853877
854878
import (
855879
"runtime"
@@ -862,14 +886,21 @@ var {SYNC_EXPORT_PINNER} = runtime.Pinner{{}}
862886
863887
{src}
864888
865-
// Unused, but present to make the compiler happy
866-
func main() {{}}
889+
{main_func}
867890
"#
868891
)
869892
.as_bytes(),
870893
),
871894
);
872-
files.push("go.mod", format!("module wit_component\n\ngo 1.25\n\nreplace github.com/bytecodealliance/wit-bindgen => {REPLACEMENT_PKG}").as_bytes());
895+
files.push(
896+
"go.mod",
897+
format!(
898+
"module {}\n\ngo 1.25\n\nreplace github.com/bytecodealliance/wit-bindgen => {}",
899+
self.opts.mod_name.as_deref().unwrap_or("wit_component"),
900+
REPLACEMENT_PKG
901+
)
902+
.as_bytes(),
903+
);
873904

874905
for (prefix, interfaces) in [("export_", &self.export_interfaces), ("", &self.interfaces)] {
875906
for (name, data) in interfaces {
@@ -1707,7 +1738,7 @@ for index := 0; index < int({length}); index++ {{
17071738
FunctionKind::Freestanding | FunctionKind::AsyncFreestanding => {
17081739
let args = operands.join(", ");
17091740
let call = format!("{package}.{name}({args})");
1710-
self.imports.insert(mod_pkg(&package));
1741+
self.imports.insert(self.generator.mod_pkg(&package));
17111742
call
17121743
}
17131744
FunctionKind::Constructor(ty) => {
@@ -1718,7 +1749,7 @@ for index := 0; index < int({length}); index++ {{
17181749
.unwrap()
17191750
.to_upper_camel_case();
17201751
let call = format!("{package}.Make{ty}({args})");
1721-
self.imports.insert(mod_pkg(&package));
1752+
self.imports.insert(self.generator.mod_pkg(&package));
17221753
call
17231754
}
17241755
FunctionKind::Method(_) | FunctionKind::AsyncMethod(_) => {

0 commit comments

Comments
 (0)