From 39818c169a5ed23d4a69c255da4cca0708e36ed7 Mon Sep 17 00:00:00 2001 From: Eric Myhre Date: Wed, 14 Dec 2022 04:02:44 +0100 Subject: [PATCH] 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. --- schema/dmt/operations.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 schema/dmt/operations.go diff --git a/schema/dmt/operations.go b/schema/dmt/operations.go new file mode 100644 index 00000000..0f83e5da --- /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 := 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) +}