link.go 4.42 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
)

// Link 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 Link struct {
	TypeName string
}

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

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

tavit ohanian's avatar
tavit ohanian committed
74 75
func (x LinkAssembler) BeginMap(sizeHint int64) (ld.MapAssembler, error) {
	return nil, ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "BeginMap", AppropriateKind: ld.KindSet_JustMap, ActualKind: ld.Kind_Link}
Eric Myhre's avatar
Eric Myhre committed
76
}
tavit ohanian's avatar
tavit ohanian committed
77 78
func (x LinkAssembler) BeginList(sizeHint int64) (ld.ListAssembler, error) {
	return nil, ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "BeginList", AppropriateKind: ld.KindSet_JustList, ActualKind: ld.Kind_Link}
Eric Myhre's avatar
Eric Myhre committed
79 80
}
func (x LinkAssembler) AssignNull() error {
tavit ohanian's avatar
tavit ohanian committed
81
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignNull", AppropriateKind: ld.KindSet_JustNull, ActualKind: ld.Kind_Link}
Eric Myhre's avatar
Eric Myhre committed
82 83
}
func (x LinkAssembler) AssignBool(bool) error {
tavit ohanian's avatar
tavit ohanian committed
84
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignBool", AppropriateKind: ld.KindSet_JustBool, ActualKind: ld.Kind_Link}
Eric Myhre's avatar
Eric Myhre committed
85
}
86
func (x LinkAssembler) AssignInt(int64) error {
tavit ohanian's avatar
tavit ohanian committed
87
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignInt", AppropriateKind: ld.KindSet_JustInt, ActualKind: ld.Kind_Link}
Eric Myhre's avatar
Eric Myhre committed
88 89
}
func (x LinkAssembler) AssignFloat(float64) error {
tavit ohanian's avatar
tavit ohanian committed
90
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignFloat", AppropriateKind: ld.KindSet_JustFloat, ActualKind: ld.Kind_Link}
Eric Myhre's avatar
Eric Myhre committed
91 92
}
func (x LinkAssembler) AssignString(string) error {
tavit ohanian's avatar
tavit ohanian committed
93
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignString", AppropriateKind: ld.KindSet_JustString, ActualKind: ld.Kind_Link}
Eric Myhre's avatar
Eric Myhre committed
94 95
}
func (x LinkAssembler) AssignBytes([]byte) error {
tavit ohanian's avatar
tavit ohanian committed
96
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignBytes", AppropriateKind: ld.KindSet_JustBytes, ActualKind: ld.Kind_Link}
Eric Myhre's avatar
Eric Myhre committed
97
}