genKindString.go 3.4 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1
package gengo
Eric Myhre's avatar
Eric Myhre committed
2 3 4 5

import (
	"io"

6
	"github.com/ipld/go-ipld-prime/schema"
Eric Myhre's avatar
Eric Myhre committed
7 8 9
)

type generateKindString struct {
10 11
	Name schema.TypeName
	Type schema.Type
12 13
	// FUTURE: probably some adjunct config data should come with here as well.
	// FUTURE: perhaps both a global one (e.g. output package name) and a per-type one.
Eric Myhre's avatar
Eric Myhre committed
14 15
}

16 17
// FUTURE: quite a few of these "nope" methods can be reused widely.

Eric Myhre's avatar
Eric Myhre committed
18
func (gk generateKindString) EmitNodeMethodTraverseField(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
19
	doTemplate(`
Eric Myhre's avatar
Eric Myhre committed
20
		func ({{ .Name }}) TraverseField(key string) (ipld.Node, error) {
21
			return nil, ipld.ErrWrongKind{MethodName: "TraverseField", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: ipld.ReprKind_String}
Eric Myhre's avatar
Eric Myhre committed
22
		}
Eric Myhre's avatar
Eric Myhre committed
23
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
24 25 26
}

func (gk generateKindString) EmitNodeMethodTraverseIndex(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
27
	doTemplate(`
Eric Myhre's avatar
Eric Myhre committed
28
		func ({{ .Name }}) TraverseIndex(idx int) (ipld.Node, error) {
29
			return nil, ipld.ErrWrongKind{MethodName: "TraverseIndex", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: ipld.ReprKind_String}
Eric Myhre's avatar
Eric Myhre committed
30
		}
Eric Myhre's avatar
Eric Myhre committed
31
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
32 33 34
}

func (gk generateKindString) EmitNodeMethodMapIterator(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
35
	doTemplate(`
Eric Myhre's avatar
Eric Myhre committed
36
		func ({{ .Name }}) MapIterator() ipld.MapIterator {
37
			return mapIteratorReject{ipld.ErrWrongKind{MethodName: "MapIterator", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: ipld.ReprKind_String}}
Eric Myhre's avatar
Eric Myhre committed
38
		}
Eric Myhre's avatar
Eric Myhre committed
39
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
40 41 42
}

func (gk generateKindString) EmitNodeMethodListIterator(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
43
	doTemplate(`
Eric Myhre's avatar
Eric Myhre committed
44
		func ({{ .Name }}) ListIterator() ipld.ListIterator {
45
			return listIteratorReject{ipld.ErrWrongKind{MethodName: "ListIterator", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: ipld.ReprKind_String}}
Eric Myhre's avatar
Eric Myhre committed
46
		}
47
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
48 49 50
}

func (gk generateKindString) EmitNodeMethodLength(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
51
	doTemplate(`
Eric Myhre's avatar
Eric Myhre committed
52 53 54
		func ({{ .Name }}) Length() int {
			return -1
		}
Eric Myhre's avatar
Eric Myhre committed
55
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
56 57 58
}

func (gk generateKindString) EmitNodeMethodIsNull(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
59
	doTemplate(`
Eric Myhre's avatar
Eric Myhre committed
60 61 62
		func ({{ .Name }}) IsNull() bool {
			return false
		}
Eric Myhre's avatar
Eric Myhre committed
63
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
64 65 66
}

func (gk generateKindString) EmitNodeMethodAsBool(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
67
	doTemplate(`
Eric Myhre's avatar
Eric Myhre committed
68
		func ({{ .Name }}) AsBool() (bool, error) {
69
			return false, ipld.ErrWrongKind{MethodName: "AsBool", AppropriateKind: ipld.ReprKindSet_JustBool, ActualKind: ipld.ReprKind_String}
Eric Myhre's avatar
Eric Myhre committed
70
		}
Eric Myhre's avatar
Eric Myhre committed
71
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
72 73 74
}

func (gk generateKindString) EmitNodeMethodAsInt(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
75
	doTemplate(`
Eric Myhre's avatar
Eric Myhre committed
76
		func ({{ .Name }}) AsInt() (int, error) {
77
			return 0, ipld.ErrWrongKind{MethodName: "AsInt", AppropriateKind: ipld.ReprKindSet_JustInt, ActualKind: ipld.ReprKind_String}
Eric Myhre's avatar
Eric Myhre committed
78
		}
Eric Myhre's avatar
Eric Myhre committed
79
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
80 81 82
}

func (gk generateKindString) EmitNodeMethodAsFloat(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
83
	doTemplate(`
Eric Myhre's avatar
Eric Myhre committed
84
		func ({{ .Name }}) AsFloat() (float64, error) {
85
			return 0, ipld.ErrWrongKind{MethodName: "AsFloat", AppropriateKind: ipld.ReprKindSet_JustFloat, ActualKind: ipld.ReprKind_String}
Eric Myhre's avatar
Eric Myhre committed
86
		}
Eric Myhre's avatar
Eric Myhre committed
87
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
88 89 90
}

func (gk generateKindString) EmitNodeMethodAsString(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
91
	doTemplate(`
Eric Myhre's avatar
Eric Myhre committed
92 93 94
		func (x {{ .Name }}) AsString() (string, error) {
			return x.x, nil
		}
Eric Myhre's avatar
Eric Myhre committed
95
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
96 97 98
}

func (gk generateKindString) EmitNodeMethodAsBytes(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
99
	doTemplate(`
Eric Myhre's avatar
Eric Myhre committed
100
		func ({{ .Name }}) AsBytes() ([]byte, error) {
101
			return nil, ipld.ErrWrongKind{MethodName: "AsBytes", AppropriateKind: ipld.ReprKindSet_JustBytes, ActualKind: ipld.ReprKind_String}
Eric Myhre's avatar
Eric Myhre committed
102
		}
Eric Myhre's avatar
Eric Myhre committed
103
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
104 105 106
}

func (gk generateKindString) EmitNodeMethodAsLink(w io.Writer) {
Eric Myhre's avatar
Eric Myhre committed
107
	doTemplate(`
Eric Myhre's avatar
Eric Myhre committed
108
		func ({{ .Name }}) AsLink() (ipld.Link, error) {
109
			return nil, ipld.ErrWrongKind{MethodName: "AsLink", AppropriateKind: ipld.ReprKindSet_JustLink, ActualKind: ipld.ReprKind_String}
Eric Myhre's avatar
Eric Myhre committed
110
		}
Eric Myhre's avatar
Eric Myhre committed
111
	`, w, gk)
Eric Myhre's avatar
Eric Myhre committed
112
}