genCommon.go 9.05 KB
Newer Older
1 2 3 4 5
package gengo

import (
	"io"

6
	ipld "github.com/ipld/go-ipld-prime"
7 8
)

9 10 11 12 13
type generateKindedRejections struct {
	TypeIdent string // the identifier in code (sometimes is munged internals like "_Thing__Repr" corresponding to no publicly admitted schema.Type.Name).
	TypeProse string // as will be printed in messages (e.g. can be goosed up a bit, like "Thing.Repr" instead of "_Thing__Repr").
	Kind      ipld.ReprKind
}
14

15
func (d generateKindedRejections) emitNodeMethodLookupString(w io.Writer) {
16
	doTemplate(`
17 18
		func ({{ .TypeIdent }}) LookupString(string) (ipld.Node, error) {
			return nil, ipld.ErrWrongKind{TypeName: "{{ .TypeProse }}", MethodName: "LookupString", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: {{ .Kind | ReprKindConst }}}
19
		}
20
	`, w, d)
21 22
}

23
func (d generateKindedRejections) emitNodeMethodLookup(w io.Writer) {
24
	doTemplate(`
25 26
		func ({{ .TypeIdent }}) Lookup(ipld.Node) (ipld.Node, error) {
			return nil, ipld.ErrWrongKind{TypeName: "{{ .TypeProse }}", MethodName: "Lookup", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: {{ .Kind | ReprKindConst }}}
27
		}
28
	`, w, d)
29 30
}

31
func (d generateKindedRejections) emitNodeMethodLookupIndex(w io.Writer) {
32
	doTemplate(`
33 34
		func ({{ .TypeIdent }}) LookupIndex(idx int) (ipld.Node, error) {
			return nil, ipld.ErrWrongKind{TypeName: "{{ .TypeProse }}", MethodName: "LookupIndex", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: {{ .Kind | ReprKindConst }}}
35
		}
36
	`, w, d)
37 38
}

39
func (d generateKindedRejections) emitNodeMethodMapIterator(w io.Writer) {
40
	doTemplate(`
41 42
		func ({{ .TypeIdent }}) MapIterator() ipld.MapIterator {
			return mapIteratorReject{ipld.ErrWrongKind{TypeName: "{{ .TypeProse }}", MethodName: "MapIterator", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: {{ .Kind | ReprKindConst }}}}
43
		}
44
	`, w, d)
45 46
}

47
func (d generateKindedRejections) emitNodeMethodListIterator(w io.Writer) {
48
	doTemplate(`
49 50
		func ({{ .TypeIdent }}) ListIterator() ipld.ListIterator {
			return listIteratorReject{ipld.ErrWrongKind{TypeName: "{{ .TypeProse }}", MethodName: "ListIterator", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: {{ .Kind | ReprKindConst }}}}
51
		}
52
	`, w, d)
53 54
}

55
func (d generateKindedRejections) emitNodeMethodLength(w io.Writer) {
56
	doTemplate(`
57
		func ({{ .TypeIdent }}) Length() int {
58 59
			return -1
		}
60
	`, w, d)
61 62
}

63
func (d generateKindedRejections) emitNodeMethodIsUndefined(w io.Writer) {
64
	doTemplate(`
65
		func ({{ .TypeIdent }}) IsUndefined() bool {
66 67
			return false
		}
68
	`, w, d)
69 70
}

71
func (d generateKindedRejections) emitNodeMethodIsNull(w io.Writer) {
72
	doTemplate(`
73
		func ({{ .TypeIdent }}) IsNull() bool {
74 75
			return false
		}
76
	`, w, d)
77 78
}

79
func (d generateKindedRejections) emitNodeMethodAsBool(w io.Writer) {
80
	doTemplate(`
81 82
		func ({{ .TypeIdent }}) AsBool() (bool, error) {
			return false, ipld.ErrWrongKind{TypeName: "{{ .TypeProse }}", MethodName: "AsBool", AppropriateKind: ipld.ReprKindSet_JustBool, ActualKind: {{ .Kind | ReprKindConst }}}
83
		}
84
	`, w, d)
85 86
}

87
func (d generateKindedRejections) emitNodeMethodAsInt(w io.Writer) {
88
	doTemplate(`
89 90
		func ({{ .TypeIdent }}) AsInt() (int, error) {
			return 0, ipld.ErrWrongKind{TypeName: "{{ .TypeProse }}", MethodName: "AsInt", AppropriateKind: ipld.ReprKindSet_JustInt, ActualKind: {{ .Kind | ReprKindConst }}}
91
		}
92
	`, w, d)
93 94
}

95
func (d generateKindedRejections) emitNodeMethodAsFloat(w io.Writer) {
96
	doTemplate(`
97 98
		func ({{ .TypeIdent }}) AsFloat() (float64, error) {
			return 0, ipld.ErrWrongKind{TypeName: "{{ .TypeProse }}", MethodName: "AsFloat", AppropriateKind: ipld.ReprKindSet_JustFloat, ActualKind: {{ .Kind | ReprKindConst }}}
99
		}
100
	`, w, d)
101 102
}

103
func (d generateKindedRejections) emitNodeMethodAsString(w io.Writer) {
104
	doTemplate(`
105 106
		func ({{ .TypeIdent }}) AsString() (string, error) {
			return "", ipld.ErrWrongKind{TypeName: "{{ .TypeProse }}", MethodName: "AsString", AppropriateKind: ipld.ReprKindSet_JustString, ActualKind: {{ .Kind | ReprKindConst }}}
107
		}
108
	`, w, d)
109 110
}

111
func (d generateKindedRejections) emitNodeMethodAsBytes(w io.Writer) {
112
	doTemplate(`
113 114
		func ({{ .TypeIdent }}) AsBytes() ([]byte, error) {
			return nil, ipld.ErrWrongKind{TypeName: "{{ .TypeProse }}", MethodName: "AsBytes", AppropriateKind: ipld.ReprKindSet_JustBytes, ActualKind: {{ .Kind | ReprKindConst }}}
115
		}
116
	`, w, d)
117 118
}

119
func (d generateKindedRejections) emitNodeMethodAsLink(w io.Writer) {
120
	doTemplate(`
121 122
		func ({{ .TypeIdent }}) AsLink() (ipld.Link, error) {
			return nil, ipld.ErrWrongKind{TypeName: "{{ .TypeProse }}", MethodName: "AsLink", AppropriateKind: ipld.ReprKindSet_JustLink, ActualKind: {{ .Kind | ReprKindConst }}}
123
		}
124
	`, w, d)
125 126 127 128
}

// Embeddable to do all the "nope" methods at once.
type generateKindedRejections_String struct {
129 130
	TypeIdent string // see doc in generateKindedRejections
	TypeProse string // see doc in generateKindedRejections
131 132
}

133
func (gk generateKindedRejections_String) EmitNodeMethodLookupString(w io.Writer) {
134
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_String}.emitNodeMethodLookupString(w)
135
}
136
func (gk generateKindedRejections_String) EmitNodeMethodLookup(w io.Writer) {
137
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_String}.emitNodeMethodLookup(w)
138
}
139
func (gk generateKindedRejections_String) EmitNodeMethodLookupIndex(w io.Writer) {
140
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_String}.emitNodeMethodLookupIndex(w)
141 142
}
func (gk generateKindedRejections_String) EmitNodeMethodMapIterator(w io.Writer) {
143
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_String}.emitNodeMethodMapIterator(w)
144 145
}
func (gk generateKindedRejections_String) EmitNodeMethodListIterator(w io.Writer) {
146
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_String}.emitNodeMethodListIterator(w)
147 148
}
func (gk generateKindedRejections_String) EmitNodeMethodLength(w io.Writer) {
149
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_String}.emitNodeMethodLength(w)
150
}
151
func (gk generateKindedRejections_String) EmitNodeMethodIsUndefined(w io.Writer) {
152
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_String}.emitNodeMethodIsUndefined(w)
153
}
154
func (gk generateKindedRejections_String) EmitNodeMethodIsNull(w io.Writer) {
155
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_String}.emitNodeMethodIsNull(w)
156 157
}
func (gk generateKindedRejections_String) EmitNodeMethodAsBool(w io.Writer) {
158
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_String}.emitNodeMethodAsBool(w)
159 160
}
func (gk generateKindedRejections_String) EmitNodeMethodAsInt(w io.Writer) {
161
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_String}.emitNodeMethodAsInt(w)
162 163
}
func (gk generateKindedRejections_String) EmitNodeMethodAsFloat(w io.Writer) {
164
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_String}.emitNodeMethodAsFloat(w)
165 166
}
func (gk generateKindedRejections_String) EmitNodeMethodAsBytes(w io.Writer) {
167
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_String}.emitNodeMethodAsBytes(w)
168 169
}
func (gk generateKindedRejections_String) EmitNodeMethodAsLink(w io.Writer) {
170
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_String}.emitNodeMethodAsLink(w)
171
}
172 173 174 175 176

// Embeddable to do all the "nope" methods at once.
//
// Used for anything that "acts like" map (so, also struct).
type generateKindedRejections_Map struct {
177 178
	TypeIdent string // see doc in generateKindedRejections
	TypeProse string // see doc in generateKindedRejections
179 180
}

181
func (gk generateKindedRejections_Map) EmitNodeMethodLookupIndex(w io.Writer) {
182
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_Map}.emitNodeMethodLookupIndex(w)
183 184
}
func (gk generateKindedRejections_Map) EmitNodeMethodListIterator(w io.Writer) {
185
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_Map}.emitNodeMethodListIterator(w)
186
}
187
func (gk generateKindedRejections_Map) EmitNodeMethodIsUndefined(w io.Writer) {
188
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_Map}.emitNodeMethodIsUndefined(w)
189
}
190
func (gk generateKindedRejections_Map) EmitNodeMethodIsNull(w io.Writer) {
191
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_Map}.emitNodeMethodIsNull(w)
192 193
}
func (gk generateKindedRejections_Map) EmitNodeMethodAsBool(w io.Writer) {
194
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_Map}.emitNodeMethodAsBool(w)
195 196
}
func (gk generateKindedRejections_Map) EmitNodeMethodAsInt(w io.Writer) {
197
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_Map}.emitNodeMethodAsInt(w)
198 199
}
func (gk generateKindedRejections_Map) EmitNodeMethodAsFloat(w io.Writer) {
200
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_Map}.emitNodeMethodAsFloat(w)
201 202
}
func (gk generateKindedRejections_Map) EmitNodeMethodAsString(w io.Writer) {
203
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_Map}.emitNodeMethodAsString(w)
204 205
}
func (gk generateKindedRejections_Map) EmitNodeMethodAsBytes(w io.Writer) {
206
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_Map}.emitNodeMethodAsBytes(w)
207 208
}
func (gk generateKindedRejections_Map) EmitNodeMethodAsLink(w io.Writer) {
209
	generateKindedRejections{gk.TypeIdent, gk.TypeProse, ipld.ReprKind_Map}.emitNodeMethodAsLink(w)
210
}