map.go 3.84 KB
Newer Older
1 2 3
package mixins

import (
tavit ohanian's avatar
tavit ohanian committed
4
	ld "gitlab.dms3.io/ld/go-ld-prime"
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
)

// Map can be embedded in a struct to provide all the methods that
// have fixed output for any map-kinded nodes.
// (Mostly this includes all the methods which simply return ErrWrongKind.)
// Other methods will still need to be implemented to finish conforming to Node.
//
// To conserve memory and get a TypeName in errors without embedding,
// write methods on your type with a body that simply initializes this struct
// and immediately uses the relevant method;
// this is more verbose in source, but compiles to a tighter result:
// in memory, there's no embed; and in runtime, the calls will be inlined
// and thus have no cost in execution time.
type Map struct {
	TypeName string
}

tavit ohanian's avatar
tavit ohanian committed
22 23
func (Map) Kind() ld.Kind {
	return ld.Kind_Map
24
}
tavit ohanian's avatar
tavit ohanian committed
25 26
func (x Map) LookupByIndex(idx int64) (ld.Node, error) {
	return nil, ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "LookupByIndex", AppropriateKind: ld.KindSet_JustList, ActualKind: ld.Kind_Map}
27
}
tavit ohanian's avatar
tavit ohanian committed
28
func (Map) ListIterator() ld.ListIterator {
29 30
	return nil
}
31
func (Map) IsAbsent() bool {
32 33 34 35 36 37
	return false
}
func (Map) IsNull() bool {
	return false
}
func (x Map) AsBool() (bool, error) {
tavit ohanian's avatar
tavit ohanian committed
38
	return false, ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsBool", AppropriateKind: ld.KindSet_JustBool, ActualKind: ld.Kind_Map}
39
}
40
func (x Map) AsInt() (int64, error) {
tavit ohanian's avatar
tavit ohanian committed
41
	return 0, ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsInt", AppropriateKind: ld.KindSet_JustInt, ActualKind: ld.Kind_Map}
42 43
}
func (x Map) AsFloat() (float64, error) {
tavit ohanian's avatar
tavit ohanian committed
44
	return 0, ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsFloat", AppropriateKind: ld.KindSet_JustFloat, ActualKind: ld.Kind_Map}
45 46
}
func (x Map) AsString() (string, error) {
tavit ohanian's avatar
tavit ohanian committed
47
	return "", ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsString", AppropriateKind: ld.KindSet_JustString, ActualKind: ld.Kind_Map}
48 49
}
func (x Map) AsBytes() ([]byte, error) {
tavit ohanian's avatar
tavit ohanian committed
50
	return nil, ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsBytes", AppropriateKind: ld.KindSet_JustBytes, ActualKind: ld.Kind_Map}
51
}
tavit ohanian's avatar
tavit ohanian committed
52 53
func (x Map) AsLink() (ld.Link, error) {
	return nil, ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AsLink", AppropriateKind: ld.KindSet_JustLink, ActualKind: ld.Kind_Map}
54
}
Eric Myhre's avatar
Eric Myhre committed
55 56 57 58 59 60 61

// MapAssembler has similar purpose as Map, but for (you guessed it)
// the NodeAssembler interface rather than the Node interface.
type MapAssembler struct {
	TypeName string
}

tavit ohanian's avatar
tavit ohanian committed
62 63
func (x MapAssembler) BeginList(sizeHint int64) (ld.ListAssembler, error) {
	return nil, ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "BeginList", AppropriateKind: ld.KindSet_JustList, ActualKind: ld.Kind_Map}
Eric Myhre's avatar
Eric Myhre committed
64 65
}
func (x MapAssembler) AssignNull() error {
tavit ohanian's avatar
tavit ohanian committed
66
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignNull", AppropriateKind: ld.KindSet_JustNull, ActualKind: ld.Kind_Map}
Eric Myhre's avatar
Eric Myhre committed
67 68
}
func (x MapAssembler) AssignBool(bool) error {
tavit ohanian's avatar
tavit ohanian committed
69
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignBool", AppropriateKind: ld.KindSet_JustBool, ActualKind: ld.Kind_Map}
Eric Myhre's avatar
Eric Myhre committed
70
}
71
func (x MapAssembler) AssignInt(int64) error {
tavit ohanian's avatar
tavit ohanian committed
72
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignInt", AppropriateKind: ld.KindSet_JustInt, ActualKind: ld.Kind_Map}
Eric Myhre's avatar
Eric Myhre committed
73 74
}
func (x MapAssembler) AssignFloat(float64) error {
tavit ohanian's avatar
tavit ohanian committed
75
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignFloat", AppropriateKind: ld.KindSet_JustFloat, ActualKind: ld.Kind_Map}
Eric Myhre's avatar
Eric Myhre committed
76 77
}
func (x MapAssembler) AssignString(string) error {
tavit ohanian's avatar
tavit ohanian committed
78
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignString", AppropriateKind: ld.KindSet_JustString, ActualKind: ld.Kind_Map}
Eric Myhre's avatar
Eric Myhre committed
79 80
}
func (x MapAssembler) AssignBytes([]byte) error {
tavit ohanian's avatar
tavit ohanian committed
81
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignBytes", AppropriateKind: ld.KindSet_JustBytes, ActualKind: ld.Kind_Map}
Eric Myhre's avatar
Eric Myhre committed
82
}
tavit ohanian's avatar
tavit ohanian committed
83 84
func (x MapAssembler) AssignLink(ld.Link) error {
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignLink", AppropriateKind: ld.KindSet_JustLink, ActualKind: ld.Kind_Map}
85
}