Commit 83bbe5e1 authored by Eric Myhre's avatar Eric Myhre

Extract some error path generation.

This isn't a net negative size diff yet, but it certainly will have
that effect momentarily when we add a generator for another kind.
More importantly, it reduces the set of functions in the real
generator file to *just* the relevant ones.

We'll almost certainly extend this to cover the NodeBuilder half of
things as well, later; I just haven't gotten there yet.
parent 41465bdd
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)
}
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(`
func ({{ .Name }}) AsBytes() ([]byte, error) {
return nil, ipld.ErrWrongKind{MethodName: "{{ .Name }}.AsBytes", AppropriateKind: ipld.ReprKindSet_JustString, ActualKind: {{ .Kind.ActsLike | ReprKindConst }}}
}
`, 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 {
schema.Type // used so we can generate error messages with the type name.
}
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)
}
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)
}
...@@ -6,86 +6,20 @@ import ( ...@@ -6,86 +6,20 @@ import (
"github.com/ipld/go-ipld-prime/schema" "github.com/ipld/go-ipld-prime/schema"
) )
func NewGeneratorForKindString(t schema.Type) typeGenerator {
return generateKindString{
t,
generateKindedRejections_String{t},
}
}
type generateKindString struct { type generateKindString struct {
schema.Type schema.Type
generateKindedRejections_String
// FUTURE: probably some adjunct config data should come with here as well. // 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. // FUTURE: perhaps both a global one (e.g. output package name) and a per-type one.
} }
// FUTURE: quite a few of these "nope" methods can be reused widely.
func (gk generateKindString) EmitNodeMethodTraverseField(w io.Writer) {
doTemplate(`
func ({{ .Name }}) TraverseField(key string) (ipld.Node, error) {
return nil, ipld.ErrWrongKind{MethodName: "TraverseField", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: ipld.ReprKind_String}
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodTraverseIndex(w io.Writer) {
doTemplate(`
func ({{ .Name }}) TraverseIndex(idx int) (ipld.Node, error) {
return nil, ipld.ErrWrongKind{MethodName: "TraverseIndex", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: ipld.ReprKind_String}
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodMapIterator(w io.Writer) {
doTemplate(`
func ({{ .Name }}) MapIterator() ipld.MapIterator {
return mapIteratorReject{ipld.ErrWrongKind{MethodName: "MapIterator", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: ipld.ReprKind_String}}
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodListIterator(w io.Writer) {
doTemplate(`
func ({{ .Name }}) ListIterator() ipld.ListIterator {
return listIteratorReject{ipld.ErrWrongKind{MethodName: "ListIterator", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: ipld.ReprKind_String}}
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodLength(w io.Writer) {
doTemplate(`
func ({{ .Name }}) Length() int {
return -1
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodIsNull(w io.Writer) {
doTemplate(`
func ({{ .Name }}) IsNull() bool {
return false
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodAsBool(w io.Writer) {
doTemplate(`
func ({{ .Name }}) AsBool() (bool, error) {
return false, ipld.ErrWrongKind{MethodName: "AsBool", AppropriateKind: ipld.ReprKindSet_JustBool, ActualKind: ipld.ReprKind_String}
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodAsInt(w io.Writer) {
doTemplate(`
func ({{ .Name }}) AsInt() (int, error) {
return 0, ipld.ErrWrongKind{MethodName: "AsInt", AppropriateKind: ipld.ReprKindSet_JustInt, ActualKind: ipld.ReprKind_String}
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodAsFloat(w io.Writer) {
doTemplate(`
func ({{ .Name }}) AsFloat() (float64, error) {
return 0, ipld.ErrWrongKind{MethodName: "AsFloat", AppropriateKind: ipld.ReprKindSet_JustFloat, ActualKind: ipld.ReprKind_String}
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodAsString(w io.Writer) { func (gk generateKindString) EmitNodeMethodAsString(w io.Writer) {
doTemplate(` doTemplate(`
func (x {{ .Name }}) AsString() (string, error) { func (x {{ .Name }}) AsString() (string, error) {
...@@ -93,19 +27,3 @@ func (gk generateKindString) EmitNodeMethodAsString(w io.Writer) { ...@@ -93,19 +27,3 @@ func (gk generateKindString) EmitNodeMethodAsString(w io.Writer) {
} }
`, w, gk) `, w, gk)
} }
func (gk generateKindString) EmitNodeMethodAsBytes(w io.Writer) {
doTemplate(`
func ({{ .Name }}) AsBytes() ([]byte, error) {
return nil, ipld.ErrWrongKind{MethodName: "AsBytes", AppropriateKind: ipld.ReprKindSet_JustBytes, ActualKind: ipld.ReprKind_String}
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodAsLink(w io.Writer) {
doTemplate(`
func ({{ .Name }}) AsLink() (ipld.Link, error) {
return nil, ipld.ErrWrongKind{MethodName: "AsLink", AppropriateKind: ipld.ReprKindSet_JustLink, ActualKind: ipld.ReprKind_String}
}
`, w, gk)
}
...@@ -49,7 +49,7 @@ func TestNuevo(t *testing.T) { ...@@ -49,7 +49,7 @@ func TestNuevo(t *testing.T) {
f = openOrPanic("_test/tStrang.go") f = openOrPanic("_test/tStrang.go")
emitFileHeader(f) emitFileHeader(f)
emitType(generateKindString{tStrang}, f) emitType(NewGeneratorForKindString(tStrang), f)
_ = generateKindStruct{tStract} _ = generateKindStruct{tStract}
//f = openOrPanic("_test/tStract.go") //f = openOrPanic("_test/tStract.go")
......
...@@ -4,11 +4,20 @@ import ( ...@@ -4,11 +4,20 @@ import (
"io" "io"
"text/template" "text/template"
ipld "github.com/ipld/go-ipld-prime"
wish "github.com/warpfork/go-wish" wish "github.com/warpfork/go-wish"
) )
func doTemplate(tmplstr string, w io.Writer, data interface{}) { func doTemplate(tmplstr string, w io.Writer, data interface{}) {
tmpl := template.Must(template.New("").Parse(wish.Dedent(tmplstr))) tmpl := template.Must(template.New("").
Funcs(template.FuncMap{
// 'ReprKindConst' returns the source-string for "ipld.ReprKind_{{Kind}}".
"ReprKindConst": func(k ipld.ReprKind) string {
return "ipld.ReprKind_" + k.String() // happens to be fairly trivial.
},
}).
Parse(wish.Dedent(tmplstr)))
if err := tmpl.Execute(w, data); err != nil { if err := tmpl.Execute(w, data); err != nil {
panic(err) panic(err)
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment