Documentation
¶
Overview ¶
Package gobridge exposes typed Go operations through a CLI and a local multiplexed stdio protocol. Register operations before serving; handlers run concurrently and must respect context cancellation.
Index ¶
- Constants
- func Bind(r *Registry, name string, fn any, paramNames ...string) error
- func Failure(code, message string) error
- func Register[I, O any](r *Registry, name, description string, fn func(context.Context, I) (O, error)) error
- type Constraints
- type Error
- type Field
- type Memo
- type Object
- type Operation
- type Registry
- func (r *Registry) Call(ctx context.Context, name string, params json.RawMessage) (result any, err error)
- func (r *Registry) Describe(name, description string) error
- func (r *Registry) GeneratePython(w io.Writer, class, binary string) error
- func (r *Registry) GenerateTypeScript(w io.Writer, class, binary string) error
- func (r *Registry) Initialize(ctx context.Context, config json.RawMessage) (err error)
- func (r *Registry) Main()
- func (r *Registry) NeedsInit() bool
- func (r *Registry) Run(ctx context.Context, args []string, in io.Reader, out, stderr io.Writer) error
- func (r *Registry) Schema() Schema
- func (r *Registry) Serve(parent context.Context, in io.Reader, out io.Writer, maxConcurrent int) error
- type Request
- type Response
- type Schema
- type Type
Constants ¶
const MaxFrame = 1024 * 1024
Variables ¶
This section is empty.
Functions ¶
func Bind ¶
Bind exposes an ordinary Go function or bound method. Supply every parameter name explicitly, except an optional leading context.Context. Supported results are T, (T, error), error, or no results. Pointer parameters are optional. Reflection adapts calls; Register retains its direct typed invocation path.
Types ¶
type Constraints ¶
type Constraints struct {
Minimum json.Number `json:"minimum,omitempty"`
Maximum json.Number `json:"maximum,omitempty"`
MinLength *int `json:"min_length,omitempty"`
MaxLength *int `json:"max_length,omitempty"`
}
Constraints describes inclusive limits on a non-null wire field. JSON numbers preserve exact integer bounds; an empty Number means the bound is absent. Length counts Unicode code points for strings and entries for slices/maps.
type Field ¶
type Field struct {
Name string `json:"name"`
Type Type `json:"type"`
Description string `json:"description,omitempty"`
Constraints *Constraints `json:"constraints,omitempty"`
}
type Memo ¶
type Memo[K comparable, V any] struct { // contains filtered or unexported fields }
Memo is a bounded, concurrency-safe TTL/LRU cache with per-key request coalescing. Values must be immutable or copied by the caller. Only use it for pure operations; keys must include every input and identity boundary.
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
Object binds method expressions to one constructor-created receiver per daemon. Register all methods before serving; mutable receiver state must be safe for concurrent method calls. Constructors never run during schema generation.
func NewObject ¶
NewObject declares the registry's constructor. It accepts func(Config) *T or func(Config) (*T, error), optionally with a leading context.Context. Config must be a named wire struct and *T a pointer to a named struct. One constructor is supported per registry; Bind can also register ordinary functions alongside it.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry is immutable once serving starts. Registration is not concurrent.
func (*Registry) Describe ¶
Describe supplies help text for an already registered function or method.
func (*Registry) GeneratePython ¶
GeneratePython emits concrete dataclasses, constructors and keyword-only functions/methods, plus lazy module defaults and explicit scope controls.
func (*Registry) GenerateTypeScript ¶
GenerateTypeScript emits ESM bindings with concrete readonly models, exact int64 bigint types, lazy instance/module clients, and separate runtime options. The matching package runtime is gobridge-runtime (Node.js 24 and newer).
func (*Registry) Initialize ¶
Initialize creates the process-owned receiver exactly once. Even failed attempts cannot be retried: constructors may have irreversible side effects. A new client process provides a fresh initialization attempt.
func (*Registry) Main ¶
func (r *Registry) Main()
Main runs with the process streams and sets a nonzero exit status on error.
func (*Registry) NeedsInit ¶
NeedsInit reports whether a declared constructor has not successfully run.
func (*Registry) Run ¶
func (r *Registry) Run(ctx context.Context, args []string, in io.Reader, out, stderr io.Writer) error
Run exposes the same registry as a CLI, schema command, generator, or daemon. Operation flags use JSON literals, except strings which are passed as text.
func (*Registry) Serve ¶
func (r *Registry) Serve(parent context.Context, in io.Reader, out io.Writer, maxConcurrent int) error
Serve owns one session. EOF cancels all calls and returns immediately; cooperative handlers stop via their context. The hosting main must exit on return. stdout is exclusively protocol data; send logging to stderr.
type Request ¶
type Request struct {
ID string `json:"id"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
TimeoutMS int64 `json:"timeout_ms,omitempty"`
}
type Response ¶
type Response struct {
ID string `json:"id"`
Result any `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
}
func (Response) MarshalJSON ¶
A successful void operation must include result:null. Omitting it would make the response indistinguishable from an invalid envelope to strict clients.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gobridge
command
Command gobridge generates bridge adapters from annotated Go source.
|
Command gobridge generates bridge adapters from annotated Go source. |
|
examples
|
|
|
greeter
Package greeter demonstrates an ordinary Go library with generated CLI and Python adapters.
|
Package greeter demonstrates an ordinary Go library with generated CLI and Python adapters. |
|
greeter/cmd/greeter
command
Command greeter exposes the greeter library as a CLI and stdio daemon.
|
Command greeter exposes the greeter library as a CLI and stdio daemon. |
|
internal
|
|
|
fixtures/hello
command
Hello exercises typed registration and cache ownership in integration tests.
|
Hello exercises typed registration and cache ownership in integration tests. |
|
fixtures/metadata
command
|
|
|
fixtures/perf
command
perf supplies identical work to native Go and generated bridge clients.
|
perf supplies identical work to native Go and generated bridge clients. |
|
fixtures/textkit
command
|
|
|
fixtures/wiretypes
command
|
|
|
sourcegen
Package sourcegen generates bridge registration from explicitly annotated Go declarations.
|
Package sourcegen generates bridge registration from explicitly annotated Go declarations. |