Commit 8c6af6f5 authored by Eric Myhre's avatar Eric Myhre

Add typed.ErrNoSuchField and ipld.ErrNotExists.

The latter isn't used yet; that's not the main goal of this branch (and
I think I'd rather do the refactor to use ipld.ErrNotExists after
moving the PathSegment types from the selector package into the root...
which in turn I'm not going to do while some of the other current work
on Selectors is in progress in other branches.  So!  Latur.); I just
wanted it around for documentation and clarity of intent.

This also adds a couple of other dummy references to the 'typed'
package.  I know keeping a set of packages-used-in-this-file is a
common step in the development of codegen systems targetting golang,
but I'm going to try to avoid it until the need is really forced.
parent 6898ed22
......@@ -29,3 +29,19 @@ type ErrWrongKind struct {
func (e ErrWrongKind) Error() string {
return fmt.Sprintf("func called on wrong kind: %s called on a %s node, but only makes sense on %s", e.MethodName, e.ActualKind, e.AppropriateKind)
}
// ErrNotExists may be returned from the traversal functions of the Node
// interface to indicate a missing value.
//
// Note that typed.ErrNoSuchField is another type of error which sometimes
// occurs in similar places as ErrNotExists. ErrNoSuchField is preferred
// when handling data with constraints provided by a schema that mean that
// a field can *never* exist (as differentiated from a map key which is
// simply absent in some data).
type ErrNotExists struct {
Segment string // REVIEW: might be better to use PathSegment, but depends on another refactor.
}
func (e ErrNotExists) Error() string {
return fmt.Sprintf("key not found: %q", e.Segment)
}
package typed
import (
"fmt"
"github.com/ipld/go-ipld-prime/schema"
)
// ErrNoSuchField may be returned from traversal functions on the Node
// interface when a field is requested which doesn't exist; it may also be
// returned during MapBuilder
type ErrNoSuchField struct {
Type schema.Type
FieldName string
}
func (e ErrNoSuchField) Error() string {
return fmt.Sprintf("no such field: %s.%s", e.Type.Name(), e.FieldName)
}
......@@ -61,6 +61,7 @@ func emitFileHeader(w io.Writer) {
fmt.Fprintf(w, "package whee\n\n")
fmt.Fprintf(w, "import (\n")
fmt.Fprintf(w, "\tipld \"github.com/ipld/go-ipld-prime\"\n")
fmt.Fprintf(w, "\t\"github.com/ipld/go-ipld-prime/impl/typed\"\n")
fmt.Fprintf(w, ")\n\n")
}
......
......@@ -7,6 +7,7 @@ import (
func (gk generateKindString) EmitNodeType(w io.Writer) {
doTemplate(`
var _ ipld.Node = {{ .Type.Name }}{}
var _ typed.Node = typed.Node(nil) // TODO
type {{ .Type.Name }} struct{ x string }
......
......@@ -25,6 +25,7 @@ func (gk generateKindStruct) EmitNodeType(w io.Writer) {
// and we get an extra bool for the second cardinality +1'er if both are true.
doTemplate(`
var _ ipld.Node = {{ .Type.Name }}{}
var _ typed.Node = typed.Node(nil) // TODO
type {{ .Type.Name }} struct{
{{- range $field := .Type.Fields }}
......@@ -73,7 +74,7 @@ func (gk generateKindStruct) EmitNodeMethodTraverseField(w io.Writer) {
return x.{{ $field.Name }}, nil
{{- end}}
default:
return nil, ipld.ErrNoSuchField{FieldName: key} /* FIXME would this belong in the typed package? or..? */
return nil, typed.ErrNoSuchField{Type: nil /*TODO*/, FieldName: key}
}
}
`, w, gk)
......
......@@ -7,6 +7,11 @@ import (
func emitMinima(f io.Writer) {
emitFileHeader(f)
// Avoid moderate annoyance keeping track of imports.
f.Write([]byte(`
var _ typed.Node = typed.Node(nil)
`))
// Iterator rejection thunks.
f.Write([]byte(`
type mapIteratorReject struct{ err error }
......
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