Skip to content

Commit 120c258

Browse files
committed
fix(wit-bindgen-go): quickfix for resource exports with no constructor
bytecodealliance/wit-bindgen#929 Signed-off-by: Roman Volosatovs <[email protected]>
1 parent 0f99314 commit 120c258

File tree

1 file changed

+17
-74
lines changed

1 file changed

+17
-74
lines changed

crates/wit-bindgen-go/src/interface.rs

Lines changed: 17 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use heck::ToUpperCamelCase;
66
use wit_bindgen_core::wit_parser::{
77
Case, Docs, Enum, EnumCase, Field, Flag, Flags, Function, FunctionKind, Handle, Int,
88
InterfaceId, Record, Resolve, Result_, Stream, Tuple, Type, TypeDefKind, TypeId, TypeOwner,
9-
Variant, World, WorldItem, WorldKey,
9+
Variant, World, WorldKey,
1010
};
1111
use wit_bindgen_core::{uwrite, uwriteln, Source, TypeInfo};
1212
use wrpc_introspect::{async_paths_ty, is_list_of, is_ty, rpc_func_name};
@@ -2545,13 +2545,13 @@ impl InterfaceGenerator<'_> {
25452545
let mut methods = BTreeMap::new();
25462546
let mut funcs_to_export = vec![];
25472547

2548-
traits.insert(None, ("Handler".to_string(), (vec![], vec![])));
2548+
traits.insert(None, ("Handler".to_string(), vec![]));
25492549

25502550
if let Identifier::Interface(id, ..) = identifier {
25512551
for (name, id) in &self.resolve.interfaces[id].types {
25522552
if let TypeDefKind::Resource = self.resolve.types[*id].kind {
25532553
let camel = to_upper_camel_case(name);
2554-
traits.insert(Some(*id), (camel, (vec![], vec![])));
2554+
traits.insert(Some(*id), (camel, vec![]));
25552555
methods.insert(*id, vec![]);
25562556
}
25572557
}
@@ -2569,7 +2569,7 @@ impl InterfaceGenerator<'_> {
25692569
funcs_to_export.push(func);
25702570
None
25712571
};
2572-
let (_, (handler_methods, client_methods)) = traits.get_mut(&resource).unwrap();
2572+
let (_, handler_methods) = traits.get_mut(&resource).unwrap();
25732573

25742574
let prev = mem::take(&mut self.src);
25752575
self.print_docs_and_params(func, true);
@@ -2597,23 +2597,12 @@ impl InterfaceGenerator<'_> {
25972597
self.push_str("\n");
25982598
let trait_method = mem::replace(&mut self.src, prev);
25992599
handler_methods.push(trait_method);
2600-
2601-
if matches!(func.kind, FunctionKind::Method(..)) {
2602-
let prev = mem::take(&mut self.src);
2603-
self.print_docs_and_params(func, true);
2604-
self.src.push_str(" (");
2605-
for ty in func.results.iter_types() {
2606-
self.print_opt_ty(ty, true);
2607-
self.src.push_str(", ");
2608-
}
2609-
self.push_str("func() error, error)\n");
2610-
let trait_method = mem::replace(&mut self.src, prev);
2611-
client_methods.push(trait_method);
2612-
}
26132600
}
26142601

2615-
let (name, (interface_methods, _)) = traits.remove(&None).unwrap();
2616-
if interface_methods.is_empty() {
2602+
// TODO: The method serving should be propagated into the `ServeInterface`
2603+
2604+
let (name, interface_methods) = traits.remove(&None).unwrap();
2605+
if interface_methods.is_empty() && traits.is_empty() {
26172606
return false;
26182607
}
26192608

@@ -2623,17 +2612,12 @@ impl InterfaceGenerator<'_> {
26232612
}
26242613
uwriteln!(self.src, "}}");
26252614

2626-
for (trait_name, (handler_methods, client_methods)) in traits.values() {
2615+
for (trait_name, handler_methods) in traits.values() {
26272616
uwriteln!(self.src, "type Handler{trait_name} interface {{");
26282617
for method in handler_methods {
26292618
self.src.push_str(method);
26302619
}
26312620
uwriteln!(self.src, "}}");
2632-
uwriteln!(self.src, "type {trait_name} interface {{");
2633-
for method in client_methods {
2634-
self.src.push_str(method);
2635-
}
2636-
uwriteln!(self.src, "}}");
26372621
}
26382622

26392623
uwriteln!(
@@ -3017,53 +3001,15 @@ impl InterfaceGenerator<'_> {
30173001

30183002
pub fn generate_imports<'a>(
30193003
&mut self,
3020-
identifier: Identifier<'a>,
3004+
_identifier: Identifier<'a>,
30213005
instance: &str,
30223006
funcs: impl Iterator<Item = &'a Function>,
30233007
) {
3024-
let mut resources = BTreeMap::new();
3025-
match identifier {
3026-
Identifier::Interface(id, ..) => {
3027-
for (name, id) in &self.resolve.interfaces[id].types {
3028-
if let TypeDefKind::Resource = self.resolve.types[*id].kind {
3029-
let camel = to_upper_camel_case(name);
3030-
resources.insert(*id, (camel, vec![]));
3031-
}
3032-
}
3033-
}
3034-
Identifier::World(id) => {
3035-
for (wk, wi) in &self.resolve.worlds[id].imports {
3036-
if let WorldItem::Type(id) = wi {
3037-
if let TypeDefKind::Resource = self.resolve.types[*id].kind {
3038-
let WorldKey::Name(name) = wk else {
3039-
panic!("unnamed world resource")
3040-
};
3041-
let camel = to_upper_camel_case(name);
3042-
resources.insert(*id, (camel, vec![]));
3043-
}
3044-
}
3045-
}
3046-
}
3047-
}
30483008
for func in funcs {
30493009
if self.gen.skip.contains(&func.name) {
30503010
return;
30513011
}
30523012

3053-
if let FunctionKind::Method(id) = &func.kind {
3054-
let (_, methods) = resources.get_mut(id).unwrap();
3055-
let prev = mem::take(&mut self.src);
3056-
self.print_docs_and_params(func, true);
3057-
self.src.push_str(" (");
3058-
for ty in func.results.iter_types() {
3059-
self.print_opt_ty(ty, true);
3060-
self.src.push_str(", ");
3061-
}
3062-
self.push_str("func() error, error)\n");
3063-
let trait_method = mem::replace(&mut self.src, prev);
3064-
methods.push(trait_method);
3065-
}
3066-
30673013
let fmt = self.deps.fmt();
30683014
let wrpc = self.deps.wrpc();
30693015

@@ -3181,14 +3127,6 @@ impl InterfaceGenerator<'_> {
31813127
func.name
31823128
);
31833129
}
3184-
3185-
for (trait_name, methods) in resources.values() {
3186-
uwriteln!(self.src, "type {trait_name} interface {{");
3187-
for method in methods {
3188-
self.src.push_str(method);
3189-
}
3190-
uwriteln!(self.src, "}}");
3191-
}
31923130
}
31933131

31943132
pub fn finish(&mut self) -> String {
@@ -3711,8 +3649,13 @@ func (v *{name}) WriteToIndex(w {wrpc}.ByteWriter) (func({wrpc}.IndexWriter) err
37113649
}
37123650
}
37133651

3714-
fn type_resource(&mut self, _id: TypeId, _name: &str, _docs: &Docs) {
3715-
// appropriate interfaces will be generated in imports and exports
3652+
fn type_resource(&mut self, _id: TypeId, name: &str, docs: &Docs) {
3653+
self.godoc(docs);
3654+
uwriteln!(
3655+
self.src,
3656+
"type {} interface {{}}",
3657+
to_upper_camel_case(name)
3658+
);
37163659
}
37173660

37183661
fn type_tuple(&mut self, id: TypeId, _name: &str, tuple: &Tuple, docs: &Docs) {

0 commit comments

Comments
 (0)