list.go 4.07 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1 2 3
package mixins

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

// List can be embedded in a struct to provide all the methods that
// have fixed output for any int-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 List struct {
	TypeName string
}

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

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

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