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