Commit d912ea30 authored by Eric Myhre's avatar Eric Myhre

Drop code of an incorrect attempt at Validate().

Comments added addressing some of the root issues of why, and how to
try again more correctly next time.  (At least one open question
has been found that might take a few days to resolve.)

This commit happens now because they code being removed refered to
`schema.Type.ReprKind()` a lot, and that's the real issue I'm focused
on at the moment -- that's also going away in the next commit or so.
There's significantly more subtlety to the type-vs-representation
duality than the existence of such a method could accurately describe.
parent ecfee940
package schema
import (
"fmt"
"path"
"github.com/ipld/go-ipld-prime"
)
// FUTURE: we also want something *almost* identical to this Validate method,
// but returning a `typed.Node` in the case of no error.
// (Such a method would go in the same package as `typed.Node`, presumably.)
// How do we avoid writing this method twice?
// Maybe both a Validate and Reify method belong in `typed` package,
// and Validate just returns less?
// No... Reify should probably short-circuit sooner?
// Unclear. Guess first step is that we need to decide the intended UX!
func Validate(ts TypeSystem, t Type, node ipld.Node) []error {
return validate(ts, t, node, "/")
}
// review: 'ts' param might not actually be necessary; everything relevant can be reached from t so far.
func validate(ts TypeSystem, t Type, node ipld.Node, pth string) []error {
switch t2 := t.(type) {
case TypeBool:
if node.ReprKind() != ipld.ReprKind_Bool {
return []error{fmt.Errorf("Schema match failed: expected type %q (which is kind %v) at path %q, but found kind %v", t2.Name(), t.ReprKind(), pth, node.ReprKind())}
}
return nil
case TypeString:
if node.ReprKind() != ipld.ReprKind_String {
return []error{fmt.Errorf("Schema match failed: expected type %q (which is kind %v) at path %q, but found kind %v", t2.Name(), t.ReprKind(), pth, node.ReprKind())}
}
return nil
case TypeBytes:
if node.ReprKind() != ipld.ReprKind_Bytes {
return []error{fmt.Errorf("Schema match failed: expected type %q (which is kind %v) at path %q, but found kind %v", t2.Name(), t.ReprKind(), pth, node.ReprKind())}
}
return nil
case TypeInt:
if node.ReprKind() != ipld.ReprKind_Int {
return []error{fmt.Errorf("Schema match failed: expected type %q (which is kind %v) at path %q, but found kind %v", t2.Name(), t.ReprKind(), pth, node.ReprKind())}
}
return nil
case TypeFloat:
if node.ReprKind() != ipld.ReprKind_Float {
return []error{fmt.Errorf("Schema match failed: expected type %q (which is kind %v) at path %q, but found kind %v", t2.Name(), t.ReprKind(), pth, node.ReprKind())}
}
return nil
case TypeMap:
if node.ReprKind() != ipld.ReprKind_Map {
return []error{fmt.Errorf("Schema match failed: expected type %q (which is kind %v) at path %q, but found kind %v", t2.Name(), t.ReprKind(), pth, node.ReprKind())}
}
errs := []error(nil)
for itr := node.MapIterator(); !itr.Done(); {
k, v, err := itr.Next()
if err != nil {
return []error{err}
}
// FUTURE: if KeyType is an enum rather than string, do membership check.
ks, _ := k.AsString()
if v.IsNull() {
if !t2.ValueIsNullable() {
errs = append(errs, fmt.Errorf("Schema match failed: map at path %q contains unpermitted null in key %q", pth, ks))
}
} else {
errs = append(errs, validate(ts, t2.ValueType(), v, path.Join(pth, ks))...)
}
}
return errs
case TypeList:
case TypeLink:
// TODO interesting case: would need resolver to keep checking.
case TypeUnion:
// TODO *several* interesting errors
case TypeStruct:
switch t2.tupleStyle {
case false: // as map!
if node.ReprKind() != ipld.ReprKind_Map {
return []error{fmt.Errorf("Schema match failed: expected type %q (which is kind %v) at path %q, but found kind %v", t2.Name(), t.ReprKind(), pth, node.ReprKind())}
}
// TODO loop over em
// TODO REVIEW order strictness questions?
case true: // as array!
}
case TypeEnum:
// TODO another interesting error
}
return nil
}
/*
Okay, so. There are several fun considerations for a "validate" method.
---
There's two radically different approaches to "validate"/"reify":
- Option 1: Look at the schema.Type info and check if a data node seems
to match it -- recursing on the type info.
- Option 2: Use the schema.Type{}.RepresentationNodeBuilder() to feed data
into it -- recursing on what the nodebuilder already expresses.
(Option 2 also need to take a `memStorage ipld.NodeBuilder` param, btw,
for handling all the cases where we *aren't* doing codegen.)
Option 1 provides a little more opportunity for returning multiple errors.
Option 2 will generally have a hard time with that (nodebuilers are not
necessarily in a valid state after their first error encounter).
As a result of having these two options at all, we may indeed end up with
at least two very different functions -- despite seeming to do similar
things, their interior will radically diverge.
---
We may also need to consider distinct reification paths: we may want one
that returns a new node tree which is eagerly converted to typed.Node
recursively; and another that returns a lazyNode which wraps things
with their typed node constraints only as they're requested.
(Note that the latter would have interesting implications for any code
which has expectations about pointer equality consistency.)
---
A further fun issue which needs consideration: well, I'll just save a snip
of prospective docs I wrote while trying to iterate on these functions:
// Note that using Validate on a node that's already a typed.Node is likely
// to be nonsensical. In many schemas, the typed.Node tree is actually a
// different depth than its representational tree (e.g. unions can cause this),
... and that's ... that's a fairly sizable issue that needs resolving.
There's a couple of different ways to handle some of the behaviors around
unions, and some of them make the tradeoff described above, and I'm really
unsure if all the implications have been sussed out yet. We should defer
writing code that depends on this issue until gathering some more info.
---
One more note: about returning multiple errors from a Validate function:
there's an upper bound of the utility of the thing. Going farther than the
first parse error is nice, but it will still hit limits: for example,
upon encountering a union and failing to match it, we can't generally
produce further errors from anywhere deeper in the tree without them being
combinatorial "if previous juncture X was type Y, then..." nonsense.
(This applies to all recursive kinds to some degree, but it's especially
rough with unions. For most of the others, it's flatly a missing field,
or an excessive field, or a leaf error; with unions it can be hard to tell.)
---
And finally: both "Validate" and "Reify" methods might actually belong
in the typed.Node package -- if they make *any* reference to `typed.Node`,
then they have no choice (otherwise, cyclic imports would occur).
If we make a "Validate" that works purely on the schema.Type info, and
returns *only* errors: only then we can have it in the schema package.
*/
package schema
import (
"testing"
. "github.com/warpfork/go-wish"
"github.com/ipld/go-ipld-prime/impl/free"
)
func TestSimpleTypes(t *testing.T) {
t.Run("string alone", func(t *testing.T) {
n1, _ := ipldfree.NodeBuilder().CreateString("asdf")
t1 := TypeString{
anyType{name: "Foo"},
}
Wish(t,
Validate(TypeSystem{}, t1, n1),
ShouldEqual, []error(nil))
})
}
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