diff --git a/node/bindnode/fuzz_test.go b/node/bindnode/fuzz_test.go index 26965c1e..02554890 100644 --- a/node/bindnode/fuzz_test.go +++ b/node/bindnode/fuzz_test.go @@ -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() @@ -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) diff --git a/schema/dmt/operations.go b/schema/dmt/operations.go new file mode 100644 index 00000000..efcedc55 --- /dev/null +++ b/schema/dmt/operations.go @@ -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) +} diff --git a/schema/dmt/roundtrip_test.go b/schema/dmt/roundtrip_test.go index 896e2a61..053b0bc9 100644 --- a/schema/dmt/roundtrip_test.go +++ b/schema/dmt/roundtrip_test.go @@ -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) diff --git a/schema/dmt/schema.go b/schema/dmt/schema.go index f8001c28..72aecc15 100644 --- a/schema/dmt/schema.go +++ b/schema/dmt/schema.go @@ -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 @@ -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"), ) } diff --git a/schema/dsl/parse_test.go b/schema/dsl/parse_test.go index 56126b50..d1f82637 100644 --- a/schema/dsl/parse_test.go +++ b/schema/dsl/parse_test.go @@ -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)