feat: 添加 []byte ↔ bytes 支持(base64 透明编解码)
- Python 侧:_decode_bytes_args 根据函数注解自动解码入参,_bytes_encode 自动编码 bytes 返回值 - _cast 支持 bytes 类型(call_go[bytes] 返回值解码) - 流式输出同步支持 chan []byte(每个 chunk 独立编解码) - example/worker.py 新增 bytes_reverse / bytes_concat / bytes_chunks 示例 - example/main.go 新增对应演示用例 - README 补充类型表 []byte 行及完整使用章节
This commit is contained in:
@@ -145,6 +145,30 @@ func demoPool(ctx context.Context, pool gobridge.Pool) {
|
||||
for u := range procCh {
|
||||
fmt.Printf(" %+v\n", u)
|
||||
}
|
||||
|
||||
// ── []byte 输入输出 ───────────────────────────────────────────────────────
|
||||
rev, err := gobridge.Invoke[[]byte](ctx, pool, "bytes_reverse", []byte("hello"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("bytes_reverse(hello) = %s\n", rev) // olleh
|
||||
|
||||
cat, err := gobridge.Invoke[[]byte](ctx, pool, "bytes_concat", []byte("foo"), []byte("bar"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("bytes_concat(foo, bar) = %s\n", cat) // foobar
|
||||
|
||||
// ── []byte 流式输出:Python yield bytes → Go chan []byte ──────────────────
|
||||
bCh, err := gobridge.Invoke[chan []byte](ctx, pool, "bytes_chunks", []byte("abcdefgh"), 3)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Print("bytes_chunks(abcdefgh, 3) =")
|
||||
for chunk := range bCh {
|
||||
fmt.Printf(" %s", chunk)
|
||||
}
|
||||
fmt.Println() // abc def gh
|
||||
}
|
||||
|
||||
// goService 实现 Handler 接口,公开方法自动暴露给 Python 通过 call_go() 调用
|
||||
|
||||
@@ -98,6 +98,28 @@ def process_users(users: Iterator[dict]) -> Iterator[dict]:
|
||||
yield {"id": u["id"], "name": u["name"].upper(), "score": u["score"] * 2}
|
||||
|
||||
|
||||
# ── []byte / bytes 示例 ──────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@expose
|
||||
def bytes_reverse(data: bytes) -> bytes:
|
||||
"""接收 []byte,返回翻转后的 []byte"""
|
||||
return data[::-1]
|
||||
|
||||
|
||||
@expose
|
||||
def bytes_concat(a: bytes, b: bytes) -> bytes:
|
||||
"""接收两个 []byte 参数,返回拼接结果"""
|
||||
return a + b
|
||||
|
||||
|
||||
@expose
|
||||
def bytes_chunks(data: bytes, size: int):
|
||||
"""流式输出:将 []byte 按 size 切分,逐块 yield(对应 Go Invoke[chan []byte])"""
|
||||
for i in range(0, len(data), size):
|
||||
yield data[i:i + size]
|
||||
|
||||
|
||||
# ── Server 全双工示例 ────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user