Documentation
¶
Overview ¶
Package contextcap provides a data-only capability adapter that resolves call-scoped context values into a script-visible hash or object.
Resolvers receive the request context.Context and return a value.Value of kind Hash or Object. Returned values are validated to be data-only (no callables, no cycles) and are deep-cloned before being handed to the script runtime so subsequent mutations cannot leak across calls.
Example ¶
Example shows how to install a contextcap.Capability through the vibes facade and read its data-only resolver value from a script.
package main
import (
"context"
"fmt"
"github.com/mgomes/vibescript/vibes"
"github.com/mgomes/vibescript/vibes/value"
)
// stubResolver returns a fixed object exposing a player id under the
// "user" key. Real embedders would pull values out of ctx.
func stubResolver(_ context.Context) (value.Value, error) {
return value.NewObject(map[string]value.Value{
"user": value.NewObject(map[string]value.Value{
"id": value.NewString("player-1"),
}),
}), nil
}
func main() {
engine := vibes.MustNewEngine(vibes.Config{})
script, err := engine.Compile(`def run()
ctx[:user][:id]
end`)
if err != nil {
fmt.Println("compile:", err)
return
}
result, err := script.Call(context.Background(), "run", nil, vibes.CallOptions{
Capabilities: []vibes.CapabilityAdapter{
vibes.MustNewContextCapability("ctx", stubResolver),
},
})
if err != nil {
fmt.Println("call:", err)
return
}
fmt.Println(result.String())
}
Output: player-1
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Capability ¶
type Capability struct {
// contains filtered or unexported fields
}
Capability is the data-only context capability implementation. Its Bind method takes a context.Context so the package stays free of vibes imports; the vibes facade wraps it to satisfy vibes.CapabilityAdapter.
func MustNewCapability ¶
func MustNewCapability(name string, resolver Resolver) *Capability
MustNewCapability constructs a capability or panics on invalid arguments.
Example ¶
package main
import (
"context"
"fmt"
"github.com/mgomes/vibescript/vibes/capability/contextcap"
"github.com/mgomes/vibescript/vibes/value"
)
// stubResolver returns a fixed object exposing a player id under the
// "user" key. Real embedders would pull values out of ctx.
func stubResolver(_ context.Context) (value.Value, error) {
return value.NewObject(map[string]value.Value{
"user": value.NewObject(map[string]value.Value{
"id": value.NewString("player-1"),
}),
}), nil
}
func main() {
cap := contextcap.MustNewCapability("ctx", stubResolver)
fmt.Println(cap.Name())
}
Output: ctx
func NewCapability ¶
func NewCapability(name string, resolver Resolver) (*Capability, error)
NewCapability constructs a data-only context capability.
Example ¶
package main
import (
"context"
"fmt"
"github.com/mgomes/vibescript/vibes/capability/contextcap"
"github.com/mgomes/vibescript/vibes/value"
)
// stubResolver returns a fixed object exposing a player id under the
// "user" key. Real embedders would pull values out of ctx.
func stubResolver(_ context.Context) (value.Value, error) {
return value.NewObject(map[string]value.Value{
"user": value.NewObject(map[string]value.Value{
"id": value.NewString("player-1"),
}),
}), nil
}
func main() {
cap, err := contextcap.NewCapability("ctx", stubResolver)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(cap.Name())
}
Output: ctx
func (*Capability) Bind ¶
Bind resolves the underlying value, validates that it is data-only and non-cyclic, and returns a deep-cloned copy keyed by the capability name.
func (*Capability) Name ¶
func (c *Capability) Name() string
Name returns the script-visible binding name.