Skip to content

Commit eda53db

Browse files
authored
Merge pull request #483 from ipld/schema-concatenation
Additional access to schema/dmt package; schema concatenation feature
2 parents e33bbd7 + c68ba53 commit eda53db

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

node/bindnode/fuzz_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func FuzzBindnodeViaDagCBOR(f *testing.F) {
149149
if err != nil {
150150
f.Fatal(err)
151151
}
152-
schemaNode := bindnode.Wrap(schemaDMT, schemadmt.Type.Schema.Type())
152+
schemaNode := bindnode.Wrap(schemaDMT, schemadmt.Prototypes.Schema.Type())
153153
schemaDagCBOR := marshalDagCBOR(f, schemaNode.Representation())
154154

155155
nodeBuilder := basicnode.Prototype.Any.NewBuilder()
@@ -178,7 +178,7 @@ func FuzzBindnodeViaDagCBOR(f *testing.F) {
178178
}
179179
}
180180
f.Fuzz(func(t *testing.T, schemaDagCBOR, nodeDagCBOR []byte) {
181-
schemaBuilder := schemadmt.Type.Schema.Representation().NewBuilder()
181+
schemaBuilder := schemadmt.Prototypes.Schema.Representation().NewBuilder()
182182

183183
if err := dagcbor.Decode(schemaBuilder, bytes.NewReader(schemaDagCBOR)); err != nil {
184184
t.Skipf("invalid schema-schema dag-cbor: %v", err)

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 := Prototypes.Schema.NewBuilder()
20+
if err := datamodel.Copy(bindnode.Wrap(a, Prototypes.Schema.Type()), nb); err != nil {
21+
panic(err)
22+
}
23+
if err := datamodel.Copy(bindnode.Wrap(b, Prototypes.Schema.Type()), nb); err != nil {
24+
panic(err)
25+
}
26+
return bindnode.Unwrap(nb.Build()).(*Schema)
27+
}

schema/dmt/roundtrip_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func testRoundtrip(t *testing.T, want string, updateFn func(string)) {
3333

3434
crre := regexp.MustCompile(`\r?\n`)
3535
want = crre.ReplaceAllString(want, "\n")
36-
nb := schemadmt.Type.Schema.Representation().NewBuilder()
36+
nb := schemadmt.Prototypes.Schema.Representation().NewBuilder()
3737
err := ipldjson.Decode(nb, strings.NewReader(want))
3838
qt.Assert(t, err, qt.IsNil)
3939
node := nb.Build().(schema.TypedNode)

schema/dmt/schema.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010

1111
// This schema follows https://ipld.io/specs/schemas/schema-schema.ipldsch.
1212

13-
var Type struct {
13+
var Prototypes struct {
1414
Schema schema.TypedPrototype
1515
}
1616

1717
//go:generate go run -tags=schemadmtgen gen.go
1818

19-
var schemaTypeSystem schema.TypeSystem
19+
var TypeSystem schema.TypeSystem
2020

2121
func init() {
2222
var ts schema.TypeSystem
@@ -433,10 +433,10 @@ func init() {
433433
panic("not happening")
434434
}
435435

436-
schemaTypeSystem = ts
436+
TypeSystem = ts
437437

438-
Type.Schema = bindnode.Prototype(
438+
Prototypes.Schema = bindnode.Prototype(
439439
(*Schema)(nil),
440-
schemaTypeSystem.TypeByName("Schema"),
440+
TypeSystem.TypeByName("Schema"),
441441
)
442442
}

schema/dsl/parse_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func testParse(t *testing.T, inSchema, inJSON string, updateFn func(string)) {
116116
// Ensure we can encode the schema as the json codec,
117117
// and that it results in the same bytes as the ipldsch.json file.
118118
{
119-
node := bindnode.Wrap(sch, schemadmt.Type.Schema.Type())
119+
node := bindnode.Wrap(sch, schemadmt.Prototypes.Schema.Type())
120120

121121
var buf bytes.Buffer
122122
err := ipldjson.Encode(node.Representation(), &buf)

0 commit comments

Comments
 (0)