Skip to content

Commit 7c6c21b

Browse files
committed
feat(go-bindgen): flatten result tuples
Signed-off-by: Roman Volosatovs <[email protected]>
1 parent ccf6844 commit 7c6c21b

File tree

21 files changed

+108
-40
lines changed

21 files changed

+108
-40
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ wasmtime-wasi = { version = "25", default-features = false }
131131
wit-bindgen = { version = "0.30", default-features = false }
132132
wit-bindgen-core = { version = "0.30", default-features = false }
133133
wit-bindgen-wrpc = { version = "0.6.5", default-features = false, path = "./crates/wit-bindgen" }
134-
wit-bindgen-wrpc-go = { version = "0.8.1", default-features = false, path = "./crates/wit-bindgen-go" }
134+
wit-bindgen-wrpc-go = { version = "0.9", default-features = false, path = "./crates/wit-bindgen-go" }
135135
wit-bindgen-wrpc-rust = { version = "0.6.5", default-features = false, path = "./crates/wit-bindgen-rust" }
136136
wit-bindgen-wrpc-rust-macro = { version = "0.6.5", default-features = false, path = "./crates/wit-bindgen-rust-macro" }
137137
wit-component = { version = "0.217", default-features = false }

crates/wit-bindgen-go/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wit-bindgen-wrpc-go"
3-
version = "0.8.1"
3+
version = "0.9.0"
44
description = """
55
Go bindings generator for wRPC
66
"""

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

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::iter;
2+
13
use std::fmt::Write as _;
24
use std::mem;
35

@@ -49,6 +51,26 @@ fn go_func_name(func: &Function) -> String {
4951
}
5052
}
5153

54+
pub fn flatten_ty<'a>(resolve: &'a Resolve, ty: &Type) -> impl Iterator<Item = Type> + 'a {
55+
let mut ty = *ty;
56+
loop {
57+
if let Type::Id(id) = ty {
58+
match resolve.types[id].kind {
59+
TypeDefKind::Type(t) => {
60+
ty = t;
61+
continue;
62+
}
63+
TypeDefKind::Tuple(ref t) => {
64+
return Box::new(t.types.iter().flat_map(|ty| flatten_ty(resolve, ty)))
65+
as Box<dyn Iterator<Item = Type>>
66+
}
67+
_ => {}
68+
}
69+
}
70+
return Box::new(iter::once(ty)) as Box<dyn Iterator<Item = Type>>;
71+
}
72+
}
73+
5274
pub struct InterfaceGenerator<'a> {
5375
pub src: Source,
5476
pub(super) identifier: Identifier<'a>,
@@ -2401,8 +2423,12 @@ impl InterfaceGenerator<'_> {
24012423
let prev = mem::take(&mut self.src);
24022424
self.print_docs_and_params(func);
24032425
self.src.push_str(" (");
2404-
for ty in func.results.iter_types() {
2405-
self.print_opt_ty(ty, true);
2426+
for ty in func
2427+
.results
2428+
.iter_types()
2429+
.flat_map(|ty| flatten_ty(self.resolve, ty))
2430+
{
2431+
self.print_opt_ty(&ty, true);
24062432
self.src.push_str(", ");
24072433
}
24082434
self.push_str("error)\n");
@@ -2508,11 +2534,16 @@ func ServeInterface(s {wrpc}.Server, h Handler) (stop func() error, err error) {
25082534
r#"
25092535
{slog}.DebugContext(ctx, "calling `{instance}.{name}` handler")"#,
25102536
);
2511-
for (i, _) in func.results.iter_types().enumerate() {
2537+
let results: Box<[Type]> = func
2538+
.results
2539+
.iter_types()
2540+
.flat_map(|ty| flatten_ty(self.resolve, ty))
2541+
.collect();
2542+
for (i, _) in results.iter().enumerate() {
25122543
uwrite!(self.src, "r{i}, ");
25132544
}
25142545
self.push_str("err ");
2515-
if func.results.len() > 0 {
2546+
if results.len() > 0 {
25162547
self.push_str(":");
25172548
}
25182549
self.push_str("= h.");
@@ -2534,9 +2565,9 @@ func ServeInterface(s {wrpc}.Server, h Handler) (stop func() error, err error) {
25342565
25352566
var buf {bytes}.Buffer
25362567
writes := make(map[uint32]func({wrpc}.IndexWriter) error, {})"#,
2537-
func.results.len()
2568+
results.len()
25382569
);
2539-
for (i, ty) in func.results.iter_types().enumerate() {
2570+
for (i, ty) in results.iter().enumerate() {
25402571
uwrite!(
25412572
self.src,
25422573
r#"
@@ -2632,7 +2663,12 @@ func ServeInterface(s {wrpc}.Server, h Handler) (stop func() error, err error) {
26322663
self.print_docs_and_params(func);
26332664

26342665
self.src.push_str(" (");
2635-
for (i, ty) in func.results.iter_types().enumerate() {
2666+
let results: Box<[Type]> = func
2667+
.results
2668+
.iter_types()
2669+
.flat_map(|ty| flatten_ty(self.resolve, ty))
2670+
.collect();
2671+
for (i, ty) in results.iter().enumerate() {
26362672
uwrite!(self.src, "r{i}__ ");
26372673
self.print_opt_ty(ty, true);
26382674
self.src.push_str(", ");
@@ -2735,7 +2771,7 @@ func ServeInterface(s {wrpc}.Server, h Handler) (stop func() error, err error) {
27352771
self.src.push_str("nil");
27362772
}
27372773
self.src.push_str(",\n");
2738-
for (i, ty) in func.results.iter_types().enumerate() {
2774+
for (i, ty) in results.iter().enumerate() {
27392775
let (nested, fut) = async_paths_ty(self.resolve, ty);
27402776
for path in nested {
27412777
uwrite!(self.src, "{wrpc}.NewSubscribePath().Index({i})");
@@ -2812,7 +2848,7 @@ func ServeInterface(s {wrpc}.Server, h Handler) (stop func() error, err error) {
28122848
func.name,
28132849
);
28142850

2815-
for (i, ty) in func.results.iter_types().enumerate() {
2851+
for (i, ty) in results.iter().enumerate() {
28162852
uwrite!(
28172853
self.src,
28182854
"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// Generated by `wit-bindgen-wrpc-go` 0.8.1. DO NOT EDIT!
1+
// Generated by `wit-bindgen-wrpc-go` 0.9.0. DO NOT EDIT!
22
// client package contains wRPC bindings for `client` world
33
package client

examples/go/hello-client/bindings/wrpc_examples/hello/handler/bindings.wrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by `wit-bindgen-wrpc-go` 0.8.1. DO NOT EDIT!
1+
// Generated by `wit-bindgen-wrpc-go` 0.9.0. DO NOT EDIT!
22
package handler
33

44
import (

examples/go/hello-server/bindings/exports/wrpc_examples/hello/handler/bindings.wrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by `wit-bindgen-wrpc-go` 0.8.1. DO NOT EDIT!
1+
// Generated by `wit-bindgen-wrpc-go` 0.9.0. DO NOT EDIT!
22
package handler
33

44
import (

examples/go/hello-server/bindings/server.wrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by `wit-bindgen-wrpc-go` 0.8.1. DO NOT EDIT!
1+
// Generated by `wit-bindgen-wrpc-go` 0.9.0. DO NOT EDIT!
22
// server package contains wRPC bindings for `server` world
33
package server
44

examples/go/resources-server/bindings/exports/wrpc_examples/resources/resources/bindings.wrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by `wit-bindgen-wrpc-go` 0.8.1. DO NOT EDIT!
1+
// Generated by `wit-bindgen-wrpc-go` 0.9.0. DO NOT EDIT!
22
package resources
33

44
import (

examples/go/resources-server/bindings/server.wrpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated by `wit-bindgen-wrpc-go` 0.8.1. DO NOT EDIT!
1+
// Generated by `wit-bindgen-wrpc-go` 0.9.0. DO NOT EDIT!
22
// server package contains wRPC bindings for `server` world
33
package server
44

0 commit comments

Comments
 (0)