-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This stops one step shy of introducing an "include" features to th IPLD Schema syntax... but can easily be used to produce such behavior.
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 := Type.Schema.NewBuilder() | ||
if err := datamodel.Copy(bindnode.Wrap(a, Type.Schema.Type()), nb); err != nil { | ||
panic(err) | ||
} | ||
if err := datamodel.Copy(bindnode.Wrap(b, Type.Schema.Type()), nb); err != nil { | ||
panic(err) | ||
} | ||
return bindnode.Unwrap(nb.Build()).(*Schema) | ||
} |