Skip to content

Commit

Permalink
Add a SchemaConcatenate operation.
Browse files Browse the repository at this point in the history
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
warpfork committed Dec 14, 2022
1 parent db9d8a7 commit 39818c1
Showing 1 changed file with 27 additions and 0 deletions.
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 := 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)
}

0 comments on commit 39818c1

Please sign in to comment.