Skip to content

Commit

Permalink
Merge pull request #483 from ipld/schema-concatenation
Browse files Browse the repository at this point in the history
Additional access to schema/dmt package; schema concatenation feature
  • Loading branch information
warpfork committed Dec 22, 2022
2 parents e33bbd7 + c68ba53 commit eda53db
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
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 {
// 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

0 comments on commit eda53db

Please sign in to comment.