int.go 4.39 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
)

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

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

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

tavit ohanian's avatar
tavit ohanian committed
74 75
func (x IntAssembler) BeginMap(sizeHint int64) (ld.MapAssembler, error) {
	return nil, ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "BeginMap", AppropriateKind: ld.KindSet_JustMap, ActualKind: ld.Kind_Int}
Eric Myhre's avatar
Eric Myhre committed
76
}
tavit ohanian's avatar
tavit ohanian committed
77 78
func (x IntAssembler) BeginList(sizeHint int64) (ld.ListAssembler, error) {
	return nil, ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "BeginList", AppropriateKind: ld.KindSet_JustList, ActualKind: ld.Kind_Int}
Eric Myhre's avatar
Eric Myhre committed
79 80
}
func (x IntAssembler) 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_Int}
Eric Myhre's avatar
Eric Myhre committed
82 83
}
func (x IntAssembler) 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_Int}
Eric Myhre's avatar
Eric Myhre committed
85 86
}
func (x IntAssembler) AssignFloat(float64) error {
tavit ohanian's avatar
tavit ohanian committed
87
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignFloat", AppropriateKind: ld.KindSet_JustFloat, ActualKind: ld.Kind_Int}
Eric Myhre's avatar
Eric Myhre committed
88 89
}
func (x IntAssembler) AssignString(string) error {
tavit ohanian's avatar
tavit ohanian committed
90
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignString", AppropriateKind: ld.KindSet_JustString, ActualKind: ld.Kind_Int}
Eric Myhre's avatar
Eric Myhre committed
91 92
}
func (x IntAssembler) AssignBytes([]byte) error {
tavit ohanian's avatar
tavit ohanian committed
93
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignBytes", AppropriateKind: ld.KindSet_JustBytes, ActualKind: ld.Kind_Int}
Eric Myhre's avatar
Eric Myhre committed
94
}
tavit ohanian's avatar
tavit ohanian committed
95 96
func (x IntAssembler) AssignLink(ld.Link) error {
	return ld.ErrWrongKind{TypeName: x.TypeName, MethodName: "AssignLink", AppropriateKind: ld.KindSet_JustLink, ActualKind: ld.Kind_Int}
97
}