genCommon.go 7.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
package gengo

import (
	"io"

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

type generateKindedRejections struct{}

func (generateKindedRejections) emitNodeMethodTraverseField(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) TraverseField(string) (ipld.Node, error) {
			return nil, ipld.ErrWrongKind{MethodName: "{{ .Name }}.TraverseField", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
		}
	`, w, t)
}

func (generateKindedRejections) emitNodeMethodTraverseIndex(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) TraverseIndex(idx int) (ipld.Node, error) {
			return nil, ipld.ErrWrongKind{MethodName: "{{ .Name }}.TraverseIndex", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
		}
	`, w, t)
}

func (generateKindedRejections) emitNodeMethodMapIterator(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) MapIterator() ipld.MapIterator {
			return mapIteratorReject{ipld.ErrWrongKind{MethodName: "{{ .Name }}.MapIterator", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}}
		}
	`, w, t)
}

func (generateKindedRejections) emitNodeMethodListIterator(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) ListIterator() ipld.ListIterator {
			return listIteratorReject{ipld.ErrWrongKind{MethodName: "{{ .Name }}.ListIterator", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}}
		}
	`, w, t)
}

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

51 52 53 54 55 56 57 58
func (generateKindedRejections) emitNodeMethodIsUndefined(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) IsUndefined() bool {
			return false
		}
	`, w, t)
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
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) {
			return false, ipld.ErrWrongKind{MethodName: "{{ .Name }}.AsBool", AppropriateKind: ipld.ReprKindSet_JustBool, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
		}
	`, w, t)
}

func (generateKindedRejections) emitNodeMethodAsInt(w io.Writer, t schema.Type) {
	doTemplate(`
		func ({{ .Name }}) AsInt() (int, error) {
			return 0, ipld.ErrWrongKind{MethodName: "{{ .Name }}.AsInt", AppropriateKind: ipld.ReprKindSet_JustInt, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
		}
	`, w, t)
}

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

func (generateKindedRejections) emitNodeMethodAsString(w io.Writer, t schema.Type) {
	doTemplate(`
93 94
		func ({{ .Name }}) AsString() (string, error) {
			return "", ipld.ErrWrongKind{MethodName: "{{ .Name }}.AsString", AppropriateKind: ipld.ReprKindSet_JustString, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
		}
	`, w, t)
}

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

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

// Embeddable to do all the "nope" methods at once.
type generateKindedRejections_String struct {
117
	Type schema.Type // used so we can generate error messages with the type name.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
}

func (gk generateKindedRejections_String) EmitNodeMethodTraverseField(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodTraverseField(w, gk.Type)
}
func (gk generateKindedRejections_String) EmitNodeMethodTraverseIndex(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodTraverseIndex(w, gk.Type)
}
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)
}
135 136 137
func (gk generateKindedRejections_String) EmitNodeMethodIsUndefined(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodIsUndefined(w, gk.Type)
}
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
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)
}
156 157 158 159 160 161 162 163 164 165 166 167 168 169

// 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.
}

func (gk generateKindedRejections_Map) EmitNodeMethodTraverseIndex(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodTraverseIndex(w, gk.Type)
}
func (gk generateKindedRejections_Map) EmitNodeMethodListIterator(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodListIterator(w, gk.Type)
}
170 171 172
func (gk generateKindedRejections_Map) EmitNodeMethodIsUndefined(w io.Writer) {
	generateKindedRejections{}.emitNodeMethodIsUndefined(w, gk.Type)
}
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
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)
}