datalarkengine

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2021 License: Apache-2.0, MIT, Apache-2.0, + 1 more Imports: 10 Imported by: 0

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 (Map)
evalExample(`
		x = {"bz": "zoo"}
		print(datalark.Map(hey="hai", zonk="wot", **x))
		print(datalark.Map({datalark.String("fun"): "heeey"}))
	`, nil)
Output:

map{
	string{"hey"}: string{"hai"}
	string{"zonk"}: string{"wot"}
	string{"bz"}: string{"zoo"}
}
map{
	string{"fun"}: string{"heeey"}
}
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
}

evalExample(`
		#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"}))
	`, []schema.TypedPrototype{
	bindnode.Prototype((*FooBar)(nil), ts.TypeByName("FooBar")),
	bindnode.Prototype((*M)(nil), ts.TypeByName("Map__FooBar__String")),
})
Output:

map<Map__FooBar__String>{
	struct<FooBar>{foo: string<String>{"f"}, bar: string<String>{"b"}}: string<String>{"wot"}
}
Example (String)
evalExample(`
		print(datalark.String("yo"))
	`, nil)
Output:

string{"yo"}
Example (Structs)
ts := schema.MustTypeSystem(
	schema.SpawnString("String"),
	schema.SpawnStruct("FooBar", []schema.StructField{
		schema.SpawnStructField("foo", "String", false, false),
		schema.SpawnStructField("bar", "String", false, false),
	}, nil),
)
type FooBar struct{ Foo, Bar string }

evalExample(`
		print(mytypes.FooBar)
		print(mytypes.FooBar(foo="hai", bar="wot"))
		x = {"foo": "z"}
		x["bar"] = "å!"
		print(mytypes.FooBar(**x))
	`, []schema.TypedPrototype{
	bindnode.Prototype((*FooBar)(nil), ts.TypeByName("FooBar")),
})
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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConstructMap

func ConstructMap(np datamodel.NodePrototype, _ *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

func ConstructStruct

func ConstructStruct(npt schema.TypedPrototype, _ *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

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 Unwrap

func Unwrap(sval starlark.Value) datamodel.Node

Unwrap peeks at a starlark Value and see if it is implemented by one of the wrappers in this package; if so, it gets the ipld Node back out and returns that. Otherwise, it returns nil. (Unwrap does not attempt to coerce other starlark values _into_ ipld Nodes.)

Types

type Map

type Map struct {
	// contains filtered or unexported fields
}

func (*Map) Freeze

func (g *Map) Freeze()

func (*Map) Get

func (g *Map) Get(in starlark.Value) (out starlark.Value, found bool, err error)

Get implements part of `starlark.Mapping`.

func (*Map) Hash

func (g *Map) Hash() (uint32, error)

func (*Map) Node

func (g *Map) Node() datamodel.Node

func (*Map) String

func (g *Map) String() string

func (*Map) Truth

func (g *Map) Truth() starlark.Bool

func (*Map) Type

func (g *Map) Type() string

type Object

type Object starlark.Dict

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 NewObject

func NewObject(size int) *Object

func ObjOfConstructorsForPrimitives

func ObjOfConstructorsForPrimitives() *Object

See docs on datalark.ObjOfConstructorsForPrimitives. Typically you should prefer using functions in the datalark package, rather than their equivalents in the datalarkengine package.

func ObjOfConstructorsForPrototypes

func ObjOfConstructorsForPrototypes(prototypes ...schema.TypedPrototype) *Object

See docs on datalark.ObjOfConstructorsForPrototypes. Typically you should prefer using functions in the datalark package, rather than their equivalents in the datalarkengine package.

func (*Object) Attr

func (g *Object) Attr(name string) (starlark.Value, error)

func (*Object) AttrNames

func (g *Object) AttrNames() []string

func (*Object) Freeze

func (d *Object) Freeze()

func (*Object) Get

func (d *Object) Get(k starlark.Value) (v starlark.Value, found bool, err error)

func (*Object) Hash

func (d *Object) Hash() (uint32, error)

func (*Object) Items

func (d *Object) Items() []starlark.Tuple

func (*Object) Iterate

func (d *Object) Iterate() starlark.Iterator

func (*Object) Keys

func (d *Object) Keys() []starlark.Value

func (*Object) Len

func (d *Object) Len() int

func (*Object) SetKey

func (d *Object) SetKey(k, v starlark.Value) error

func (*Object) String

func (d *Object) String() string

func (*Object) Truth

func (d *Object) Truth() starlark.Bool

func (*Object) Type

func (d *Object) Type() string

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 (*Prototype) CallInternal

func (g *Prototype) CallInternal(thread *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

func (*Prototype) Freeze

func (g *Prototype) Freeze()

func (*Prototype) Hash

func (g *Prototype) Hash() (uint32, error)

func (*Prototype) Name

func (g *Prototype) Name() string

func (*Prototype) String

func (g *Prototype) String() string

func (*Prototype) Truth

func (g *Prototype) Truth() starlark.Bool

func (*Prototype) Type

func (g *Prototype) Type() string

type String

type String struct {
	// contains filtered or unexported fields
}

func (*String) Freeze

func (g *String) Freeze()

func (*String) Hash

func (g *String) Hash() (uint32, error)

func (*String) Node

func (g *String) Node() datamodel.Node

func (*String) String

func (g *String) String() string

func (*String) Truth

func (g *String) Truth() starlark.Bool

func (*String) Type

func (g *String) Type() string

type Struct

type Struct struct {
	// contains filtered or unexported fields
}

func WrapStruct

func WrapStruct(val datamodel.Node) (*Struct, error)

func (*Struct) Attr

func (g *Struct) Attr(name string) (starlark.Value, error)

func (*Struct) AttrNames

func (g *Struct) AttrNames() []string

func (*Struct) Freeze

func (g *Struct) Freeze()

func (*Struct) Hash

func (g *Struct) Hash() (uint32, error)

func (*Struct) Node

func (g *Struct) Node() datamodel.Node

func (*Struct) SetField

func (g *Struct) SetField(name string, val starlark.Value) error

func (*Struct) String

func (g *Struct) String() string

func (*Struct) Truth

func (g *Struct) Truth() starlark.Bool

func (*Struct) Type

func (g *Struct) Type() string

type Value

type Value interface {
	starlark.Value
	Node() datamodel.Node
}

func Wrap

func Wrap(n datamodel.Node) (Value, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL