genCommon.go 7.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package gengo

import (
	"io"

	"github.com/ipld/go-ipld-prime/schema"
)

type generateKindedRejections struct{}

11
func (generateKindedRejections) emitNodeMethodLookupString(w io.Writer, t schema.Type) {
12
	doTemplate(`
13
		func ({{ .Name }}) LookupString(string) (ipld.Node, error) {
14
			return nil, ipld.ErrWrongKind{TypeName: "{{ .Name }}", MethodName: "LookupString", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
15 16 17 18
		}
	`, w, t)
}

19 20 21
func (generateKindedRejections) emitNodeMethodLookup(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) Lookup(ipld.Node) (ipld.Node, error) {
22
			return nil, ipld.ErrWrongKind{TypeName: "{{ .Name }}", MethodName: "Lookup", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
23 24 25 26
		}
	`, w, t)
}

27
func (generateKindedRejections) emitNodeMethodLookupIndex(w io.Writer, t schema.Type) {
28
	doTemplate(`
29
		func ({{ .Name }}) LookupIndex(idx int) (ipld.Node, error) {
30
			return nil, ipld.ErrWrongKind{TypeName: "{{ .Name }}", MethodName: "LookupIndex", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
31 32 33 34 35 36 37
		}
	`, w, t)
}

func (generateKindedRejections) emitNodeMethodMapIterator(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) MapIterator() ipld.MapIterator {
38
			return mapIteratorReject{ipld.ErrWrongKind{TypeName: "{{ .Name }}", MethodName: "MapIterator", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}}
39 40 41 42 43 44 45
		}
	`, w, t)
}

func (generateKindedRejections) emitNodeMethodListIterator(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) ListIterator() ipld.ListIterator {
46
			return listIteratorReject{ipld.ErrWrongKind{TypeName: "{{ .Name }}", MethodName: "ListIterator", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}}
47 48 49 50 51 52 53 54 55 56 57 58
		}
	`, w, t)
}

func (generateKindedRejections) emitNodeMethodLength(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) Length() int {
			return -1
		}
	`, w, t)
}

59 60 61 62 63 64 65 66
func (generateKindedRejections) emitNodeMethodIsUndefined(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) IsUndefined() bool {
			return false
		}
	`, w, t)
}

67 68 69 70 71 72 73 74 75 76 77
func (generateKindedRejections) emitNodeMethodIsNull(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) IsNull() bool {
			return false
		}
	`, w, t)
}

func (generateKindedRejections) emitNodeMethodAsBool(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) AsBool() (bool, error) {
78
			return false, ipld.ErrWrongKind{TypeName: "{{ .Name }}", MethodName: "AsBool", AppropriateKind: ipld.ReprKindSet_JustBool, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
79 80 81 82 83 84 85
		}
	`, w, t)
}

func (generateKindedRejections) emitNodeMethodAsInt(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) AsInt() (int, error) {
86
			return 0, ipld.ErrWrongKind{TypeName: "{{ .Name }}", MethodName: "AsInt", AppropriateKind: ipld.ReprKindSet_JustInt, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
87 88 89 90 91 92 93
		}
	`, w, t)
}

func (generateKindedRejections) emitNodeMethodAsFloat(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) AsFloat() (float64, error) {
94
			return 0, ipld.ErrWrongKind{TypeName: "{{ .Name }}", MethodName: "AsFloat", AppropriateKind: ipld.ReprKindSet_JustFloat, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
95 96 97 98 99 100
		}
	`, w, t)
}

func (generateKindedRejections) emitNodeMethodAsString(w io.Writer, t schema.Type) {
	doTemplate(`
101
		func ({{ .Name }}) AsString() (string, error) {
102
			return "", ipld.ErrWrongKind{TypeName: "{{ .Name }}", MethodName: "AsString", AppropriateKind: ipld.ReprKindSet_JustString, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
103 104 105 106 107 108 109
		}
	`, w, t)
}

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

func (generateKindedRejections) emitNodeMethodAsLink(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) AsLink() (ipld.Link, error) {
118
			return nil, ipld.ErrWrongKind{TypeName: "{{ .Name }}", MethodName: "AsLink", AppropriateKind: ipld.ReprKindSet_JustLink, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
119 120 121 122 123 124
		}
	`, w, t)
}

// Embeddable to do all the "nope" methods at once.
type generateKindedRejections_String struct {
125
	Type schema.Type // used so we can generate error messages with the type name.
126 127
}

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

// Embeddable to do all the "nope" methods at once.
//
// Used for anything that "acts like" map (so, also struct).
type generateKindedRejections_Map struct {
	Type schema.Type // used so we can generate error messages with the type name.
}

175 176
func (gk generateKindedRejections_Map) EmitNodeMethodLookupIndex(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodLookupIndex(w, gk.Type)
177 178 179 180
}
func (gk generateKindedRejections_Map) EmitNodeMethodListIterator(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodListIterator(w, gk.Type)
}
181 182 183
func (gk generateKindedRejections_Map) EmitNodeMethodIsUndefined(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodIsUndefined(w, gk.Type)
}
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
func (gk generateKindedRejections_Map) EmitNodeMethodIsNull(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodIsNull(w, gk.Type)
}
func (gk generateKindedRejections_Map) EmitNodeMethodAsBool(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodAsBool(w, gk.Type)
}
func (gk generateKindedRejections_Map) EmitNodeMethodAsInt(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodAsInt(w, gk.Type)
}
func (gk generateKindedRejections_Map) EmitNodeMethodAsFloat(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodAsFloat(w, gk.Type)
}
func (gk generateKindedRejections_Map) EmitNodeMethodAsString(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodAsString(w, gk.Type)
}
func (gk generateKindedRejections_Map) EmitNodeMethodAsBytes(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodAsBytes(w, gk.Type)
}
func (gk generateKindedRejections_Map) EmitNodeMethodAsLink(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodAsLink(w, gk.Type)
}