Documentation ¶
Overview ¶
datalarkengine contains all the low-level binding logic.
Perhaps somewhat surprisingly, it includes even wrapper types for the more primitive kinds (like string). This is important (rather than just converting them directly to starlark's values) because we may want things like IPLD type information (or even just NodePrototype) to be retained, as well as sometimes wanting the original pointer to be retained for efficiency reasons.
Example (MapWithStructKeys) ¶
ts := schema.MustTypeSystem( schema.SpawnString("String"), schema.SpawnStruct("FooBar", []schema.StructField{ schema.SpawnStructField("foo", "String", false, false), schema.SpawnStructField("bar", "String", false, false), }, schema.SpawnStructRepresentationStringjoin(":")), schema.SpawnMap("Map__FooBar__String", "FooBar", "String", false), ) type FooBar struct{ Foo, Bar string } type M struct { Keys []FooBar Values map[FooBar]string } mustExecExample(nil, []schema.TypedPrototype{ bindnode.Prototype((*FooBar)(nil), ts.TypeByName("FooBar")), bindnode.Prototype((*M)(nil), ts.TypeByName("Map__FooBar__String")), }, "mytypes", ` #print(mytypes.Map__FooBar__String({"f:b": "wot"})) # I want this to work someday, but it's not quite that magic yet. print(mytypes.Map__FooBar__String(_={mytypes.FooBar(foo="f", bar="b"): "wot"})) `)
Output: map<Map__FooBar__String>{ struct<FooBar>{foo: string<String>{"f"}, bar: string<String>{"b"}}: string<String>{"wot"} }
Example (String) ¶
mustExecExample(nil, nil, "mytypes", ` print(datalark.String('yo')) `)
Output: string{"yo"}
Example (Structs) ¶
// Start with a schema. typesystem, err := ipld.LoadSchema("<noname>", strings.NewReader(` type FooBar struct { foo String bar String } `)) if err != nil { panic(err) } // These are the golang types we'll bind it to. type FooBar struct{ Foo, Bar string } // These are the bindings we'll export to starlark. bindings := []schema.TypedPrototype{ bindnode.Prototype((*FooBar)(nil), typesystem.TypeByName("FooBar")), } // Here's a script running on them: mustExecExample(nil, bindings, "mytypes", ` print(mytypes.FooBar) print(mytypes.FooBar(foo="hai", bar="wot")) x = {"foo": "z"} x["bar"] = "å!" print(mytypes.FooBar(**x)) `)
Output: <built-in function datalark.Prototype<FooBar>> struct<FooBar>{ foo: string<String>{"hai"} bar: string<String>{"wot"} } struct<FooBar>{ foo: string<String>{"z"} bar: string<String>{"å!"} }
Index ¶
- Constants
- func InjectGlobals(globals starlark.StringDict, obj *Object)
- func NewListMethod(name string, meth listMethod, numNeed, numAllow int) *starlark.Builtin
- func NewMapMethod(name string, meth mapMethod, numNeed, numAllow int) *starlark.Builtin
- type ArgSeq
- type Mode
- type Object
- func (g *Object) Attr(name string) (starlark.Value, error)
- func (g *Object) AttrNames() []string
- func (d *Object) Freeze()
- func (d *Object) Get(k starlark.Value) (v starlark.Value, found bool, err error)
- func (d *Object) Hash() (uint32, error)
- func (d *Object) Items() []starlark.Tuple
- func (d *Object) Iterate() starlark.Iterator
- func (d *Object) Keys() []starlark.Value
- func (d *Object) Len() int
- func (d *Object) SetKey(k, v starlark.Value) error
- func (d *Object) String() string
- func (d *Object) Truth() starlark.Bool
- func (d *Object) Type() string
- type Prototype
- func (p *Prototype) Attr(name string) (starlark.Value, error)
- func (p *Prototype) AttrNames() []string
- func (p *Prototype) CallInternal(thread *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)
- func (p *Prototype) Freeze()
- func (p *Prototype) Hash() (uint32, error)
- func (p *Prototype) Name() string
- func (p *Prototype) NodePrototype() datamodel.NodePrototype
- func (p *Prototype) String() string
- func (p *Prototype) Truth() starlark.Bool
- func (p *Prototype) Type() string
- func (p *Prototype) TypeName() string
- type Value
- func NewBool(b bool) Value
- func NewBytes(d []byte) Value
- func NewFloat(f float64) Value
- func NewInt(n int64) Value
- func NewLink(x datamodel.Link) Value
- func NewList(starList *starlark.List) (Value, error)
- func NewNull() Value
- func NewString(text string) Value
- func ToValue(n datamodel.Node) (Value, error)
Examples ¶
Constants ¶
const ( AnyMode Mode = 0 TypedMode = 1 ReprMode = 2 )
Variables ¶
This section is empty.
Functions ¶
func InjectGlobals ¶
func InjectGlobals(globals starlark.StringDict, obj *Object)
See docs on datalark.InjectGlobals. Typically you should prefer using functions in the datalark package, rather than their equivalents in the datalarkengine package.
func NewListMethod ¶ added in v0.4.0
Types ¶
type ArgSeq ¶ added in v0.2.0
type ArgSeq struct {
// contains filtered or unexported fields
}
ArgSeq represents a sequence of arguments passed into a function. The sequence may or may not also have a mapping from argument names to positions, as is the case for keyword args or for restructured args
func (*ArgSeq) IsSingleString ¶ added in v0.2.0
IsSingleString returns whether the args are a single string value
type Object ¶
Object is a starlark.Value that contains arbitrary attributes (like a starlark.Dict), and also lets you access things with dot notation (e.g. as "attrs") in addition to with map key notation.
We use this for creating hierarchical namespaces. (For example, this is how we make typed constructors available without cluttering global namespaces.)
func MakeConstructors ¶ added in v0.2.0
func MakeConstructors(prototypes []schema.TypedPrototype) *Object
MakeConstructors returns the constructors for the given prototypes as an Object
func PrimitiveConstructors ¶ added in v0.2.0
func PrimitiveConstructors() *Object
PrimitiveConstructors returns the constructors for primitive types as an Object
type Prototype ¶
type Prototype struct {
// contains filtered or unexported fields
}
Prototype wraps an IPLD `datamodel.NodePrototype`, and in starlark, is a `Callable` which acts like a constructor for that NodePrototype.
There is only one Prototype type, and its behavior varies based on the `datamodel.NodePrototype` its bound to.
func NewPrototype ¶ added in v0.2.0
func NewPrototype(name string, np datamodel.NodePrototype) *Prototype
func (*Prototype) CallInternal ¶
func (*Prototype) NodePrototype ¶ added in v0.2.0
func (p *Prototype) NodePrototype() datamodel.NodePrototype