Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional access to schema/dmt package; schema concatenation feature #483

Merged
merged 3 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions node/bindnode/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func FuzzBindnodeViaDagCBOR(f *testing.F) {
if err != nil {
f.Fatal(err)
}
schemaNode := bindnode.Wrap(schemaDMT, schemadmt.Type.Schema.Type())
schemaNode := bindnode.Wrap(schemaDMT, schemadmt.Prototypes.Schema.Type())
schemaDagCBOR := marshalDagCBOR(f, schemaNode.Representation())

nodeBuilder := basicnode.Prototype.Any.NewBuilder()
Expand Down Expand Up @@ -178,7 +178,7 @@ func FuzzBindnodeViaDagCBOR(f *testing.F) {
}
}
f.Fuzz(func(t *testing.T, schemaDagCBOR, nodeDagCBOR []byte) {
schemaBuilder := schemadmt.Type.Schema.Representation().NewBuilder()
schemaBuilder := schemadmt.Prototypes.Schema.Representation().NewBuilder()

if err := dagcbor.Decode(schemaBuilder, bytes.NewReader(schemaDagCBOR)); err != nil {
t.Skipf("invalid schema-schema dag-cbor: %v", err)
Expand Down
27 changes: 27 additions & 0 deletions schema/dmt/operations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package schemadmt

import (
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/node/bindnode"
)

// ConcatenateSchemas returns a new schema DMT object containing the
// type declarations from both.
//
// As is usual for DMT form data, there is no check about the validity
// of the result yet; you'll need to apply `Compile` on the produced value
// to produce a usable compiled typesystem or to become certain that
// all references in the DMT are satisfied, etc.
func ConcatenateSchemas(a, b *Schema) *Schema {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be great to have a basic test/example so that there's an ongoing validation that concatenation of two basic schemas works

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I'm pro-testing but in this case I don't know what would provide useful coverage in exchange for the additional volume of test code. We really are just essentially conjoining two maps here.

If there were more interesting and complex logical operations here, I would certainly want to test it, but what this does is so trivial I don't know if it's worth it.

// The joy of having an intermediate form that's just regular data model:
// we can implement this by simply using data model "copy" operations,
// and the result is correct.
nb := Prototypes.Schema.NewBuilder()
if err := datamodel.Copy(bindnode.Wrap(a, Prototypes.Schema.Type()), nb); err != nil {
panic(err)
}
if err := datamodel.Copy(bindnode.Wrap(b, Prototypes.Schema.Type()), nb); err != nil {
panic(err)
}
return bindnode.Unwrap(nb.Build()).(*Schema)
}
2 changes: 1 addition & 1 deletion schema/dmt/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func testRoundtrip(t *testing.T, want string, updateFn func(string)) {

crre := regexp.MustCompile(`\r?\n`)
want = crre.ReplaceAllString(want, "\n")
nb := schemadmt.Type.Schema.Representation().NewBuilder()
nb := schemadmt.Prototypes.Schema.Representation().NewBuilder()
err := ipldjson.Decode(nb, strings.NewReader(want))
qt.Assert(t, err, qt.IsNil)
node := nb.Build().(schema.TypedNode)
Expand Down
10 changes: 5 additions & 5 deletions schema/dmt/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (

// This schema follows https://ipld.io/specs/schemas/schema-schema.ipldsch.

var Type struct {
var Prototypes struct {
Schema schema.TypedPrototype
}

//go:generate go run -tags=schemadmtgen gen.go

var schemaTypeSystem schema.TypeSystem
var TypeSystem schema.TypeSystem

func init() {
var ts schema.TypeSystem
Expand Down Expand Up @@ -433,10 +433,10 @@ func init() {
panic("not happening")
}

schemaTypeSystem = ts
TypeSystem = ts

Type.Schema = bindnode.Prototype(
Prototypes.Schema = bindnode.Prototype(
(*Schema)(nil),
schemaTypeSystem.TypeByName("Schema"),
TypeSystem.TypeByName("Schema"),
)
}
2 changes: 1 addition & 1 deletion schema/dsl/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func testParse(t *testing.T, inSchema, inJSON string, updateFn func(string)) {
// Ensure we can encode the schema as the json codec,
// and that it results in the same bytes as the ipldsch.json file.
{
node := bindnode.Wrap(sch, schemadmt.Type.Schema.Type())
node := bindnode.Wrap(sch, schemadmt.Prototypes.Schema.Type())

var buf bytes.Buffer
err := ipldjson.Encode(node.Representation(), &buf)
Expand Down