Skip to content

doc(go): document a few APIs #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions go/wrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,41 @@ import (
"io"
)

// Invoke is the client-side transport handle
type Invoker interface {
// Invoke invokes a function `name` within an instance `instance`.
// Initial, encoded payload must be specified in `b`.
// `paths` define the async result paths to subscribe on.
// On success, `Invoke` returns two handles used for writing and reading encoded parameters and results respectively.
// NOTE: if the returned handle is used for writing, `b` must be non-empty.
Invoke(ctx context.Context, instance string, name string, b []byte, paths ...SubscribePath) (IndexWriteCloser, IndexReadCloser, error)
}

// Server is the server-side transport handle
type Server interface {
// Serve serves a function `name` within an instance `instance`.
// `paths` define the async parameter paths to subscribe on.
// `Serve` will call `f` with two handles used for writing and reading encoded results and parameters respectively.
// On success, `Serve` returns a function, which can be called to stop serving.
Serve(instance string, name string, f func(context.Context, IndexWriteCloser, IndexReadCloser), paths ...SubscribePath) (func() error, error)
}

// Own is an owned resource handle
type Own[T any] []byte

// Borrow returns the handle as a [`Borrow`]
func (v Own[T]) Borrow() Borrow[T] {
return Borrow[T](v)
}

// Borrow is a borrowed resource handle
type Borrow[T any] []byte

// SubscribePath is the subscription path.
// `nil` represents a wildcard index used for dynamically-sized values, like `list`
type SubscribePath []*uint32

// NewSubscribePath creates a new subscription path.
func NewSubscribePath(ps ...*uint32) SubscribePath {
return SubscribePath(ps)
}
Expand All @@ -33,14 +48,18 @@ func (p SubscribePath) push(v *uint32) SubscribePath {
return SubscribePath(append(append(make(SubscribePath, 0, len(p)+1), p...), v))
}

// Index pushes a `uint32` index to the path
func (p SubscribePath) Index(i uint32) SubscribePath {
return p.push(&i)
}

// Wildcard pushes a wildcard index to the path used for dynamically-sized values, like `list`
func (p SubscribePath) Wildcard() SubscribePath {
return p.push(nil)
}

// Parent pops the last element in the path and returns the resulting path and `true` and success.
// It returns `nil`, `false` otherwise.
func (p SubscribePath) Parent() (SubscribePath, bool) {
n := len(p)
if n == 0 {
Expand All @@ -49,6 +68,7 @@ func (p SubscribePath) Parent() (SubscribePath, bool) {
return SubscribePath(p[:n-1]), true
}

// Index represents entities, which can be indexed by concrete `uint32` paths, for example transport streams.
type Index[T any] interface {
Index(path ...uint32) (T, error)
}
Expand Down
Loading