Skip to content

Commit 1687155

Browse files
committed
add LoadSchema APIs to the root package
At this point it's just wiring up dsl.Parse and dmt.Compile. We can start using it in bindnode tests as well. Fixes #188.
1 parent 41a2be5 commit 1687155

File tree

3 files changed

+70
-19
lines changed

3 files changed

+70
-19
lines changed

Diff for: examples_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"os"
66
"strings"
77

8+
"github.com/ipld/go-ipld-prime"
89
"github.com/ipld/go-ipld-prime/codec/dagjson"
910
"github.com/ipld/go-ipld-prime/node/basicnode"
11+
"github.com/ipld/go-ipld-prime/schema"
1012
)
1113

1214
// Example_createDataAndMarshal shows how you can feed data into a NodeBuilder,
@@ -51,3 +53,23 @@ func Example_unmarshalData() {
5153
// the data decoded was a map kind
5254
// the length of the node is 2
5355
}
56+
57+
func ExampleLoadSchema() {
58+
ts, err := ipld.LoadSchema("sample.ipldsch", strings.NewReader(`
59+
type Root struct {
60+
foo Int
61+
bar nullable String
62+
}
63+
`))
64+
if err != nil {
65+
panic(err)
66+
}
67+
typeRoot := ts.TypeByName("Root").(*schema.TypeStruct)
68+
for _, field := range typeRoot.Fields() {
69+
fmt.Printf("field name=%q nullable=%t type=%v\n",
70+
field.Name(), field.IsNullable(), field.Type().Name())
71+
}
72+
// Output:
73+
// field name="foo" nullable=false type=Int
74+
// field name="bar" nullable=true type=String
75+
}

Diff for: node/bindnode/infer_test.go

+5-19
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import (
1010
qt "github.com/frankban/quicktest"
1111
"github.com/ipfs/go-cid"
1212

13+
"github.com/ipld/go-ipld-prime"
1314
"github.com/ipld/go-ipld-prime/codec/dagjson"
1415
"github.com/ipld/go-ipld-prime/datamodel"
1516
"github.com/ipld/go-ipld-prime/node/bindnode"
1617
"github.com/ipld/go-ipld-prime/schema"
17-
schemadmt "github.com/ipld/go-ipld-prime/schema/dmt"
18-
schemadsl "github.com/ipld/go-ipld-prime/schema/dsl"
1918
)
2019

2120
type anyScalar struct {
@@ -144,22 +143,6 @@ var prototypeTests = []struct {
144143
},
145144
}
146145

147-
func loadSchema(t *testing.T, src string) schema.Type {
148-
t.Helper()
149-
150-
dmt, err := schemadsl.Parse("", strings.NewReader(src))
151-
qt.Assert(t, err, qt.IsNil)
152-
153-
var ts schema.TypeSystem
154-
ts.Init()
155-
err = schemadmt.Compile(&ts, dmt)
156-
qt.Assert(t, err, qt.IsNil)
157-
158-
typ := ts.TypeByName("Root")
159-
qt.Assert(t, typ, qt.Not(qt.IsNil))
160-
return typ
161-
}
162-
163146
func compactJSON(t *testing.T, pretty string) string {
164147
var buf bytes.Buffer
165148
err := json.Compact(&buf, []byte(pretty))
@@ -195,7 +178,10 @@ func TestPrototype(t *testing.T) {
195178
t.Run(test.name+suffix, func(t *testing.T) {
196179
t.Parallel()
197180

198-
schemaType := loadSchema(t, test.schemaSrc)
181+
ts, err := ipld.LoadSchemaBytes([]byte(test.schemaSrc))
182+
qt.Assert(t, err, qt.IsNil)
183+
schemaType := ts.TypeByName("Root")
184+
qt.Assert(t, schemaType, qt.Not(qt.IsNil))
199185

200186
if onlySchema {
201187
test.ptrType = nil

Diff for: schema.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ipld
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os"
7+
8+
"github.com/ipld/go-ipld-prime/schema"
9+
schemadmt "github.com/ipld/go-ipld-prime/schema/dmt"
10+
schemadsl "github.com/ipld/go-ipld-prime/schema/dsl"
11+
)
12+
13+
// LoadSchemaBytes is a shortcut for LoadSchema for the common case where
14+
// the schema is available as a buffer or a string, such as via go:embed.
15+
func LoadSchemaBytes(src []byte) (*schema.TypeSystem, error) {
16+
return LoadSchema("", bytes.NewReader(src))
17+
}
18+
19+
// LoadSchemaBytes is a shortcut for LoadSchema for the common case where
20+
// the schema is a file on disk.
21+
func LoadSchemaFile(path string) (*schema.TypeSystem, error) {
22+
f, err := os.Open(path)
23+
if err != nil {
24+
return nil, err
25+
}
26+
defer f.Close()
27+
return LoadSchema(path, f)
28+
}
29+
30+
// LoadSchema parses an IPLD Schema in its DSL form
31+
// and compiles its types into a standalone TypeSystem.
32+
func LoadSchema(name string, r io.Reader) (*schema.TypeSystem, error) {
33+
sch, err := schemadsl.Parse(name, r)
34+
if err != nil {
35+
return nil, err
36+
}
37+
ts := new(schema.TypeSystem)
38+
ts.Init()
39+
if err := schemadmt.Compile(ts, sch); err != nil {
40+
return nil, err
41+
}
42+
return ts, nil
43+
}

0 commit comments

Comments
 (0)