Skip to content

Commit 39818c1

Browse files
committed
Add a SchemaConcatenate operation.
This stops one step shy of introducing an "include" features to th IPLD Schema syntax... but can easily be used to produce such behavior.
1 parent db9d8a7 commit 39818c1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

schema/dmt/operations.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package schemadmt
2+
3+
import (
4+
"github.com/ipld/go-ipld-prime/datamodel"
5+
"github.com/ipld/go-ipld-prime/node/bindnode"
6+
)
7+
8+
// ConcatenateSchemas returns a new schema DMT object containing the
9+
// type declarations from both.
10+
//
11+
// As is usual for DMT form data, there is no check about the validity
12+
// of the result yet; you'll need to apply `Compile` on the produced value
13+
// to produce a usable compiled typesystem or to become certain that
14+
// all references in the DMT are satisfied, etc.
15+
func ConcatenateSchemas(a, b *Schema) *Schema {
16+
// The joy of having an intermediate form that's just regular data model:
17+
// we can implement this by simply using data model "copy" operations,
18+
// and the result is correct.
19+
nb := Type.Schema.NewBuilder()
20+
if err := datamodel.Copy(bindnode.Wrap(a, Type.Schema.Type()), nb); err != nil {
21+
panic(err)
22+
}
23+
if err := datamodel.Copy(bindnode.Wrap(b, Type.Schema.Type()), nb); err != nil {
24+
panic(err)
25+
}
26+
return bindnode.Unwrap(nb.Build()).(*Schema)
27+
}

0 commit comments

Comments
 (0)