github.com/ipld/go-ipld-prime@v0.21.0/schema.go (about) 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 }