forked from webrpc/webrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webrpc.go
49 lines (37 loc) · 1.05 KB
/
webrpc.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
44
45
46
47
48
49
package webrpc
import (
"fmt"
"os"
"path/filepath"
"github.com/webrpc/webrpc/schema"
"github.com/webrpc/webrpc/schema/ridl"
)
func ParseSchemaFile(path string) (*schema.WebRPCSchema, error) {
absolutePath, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("invalid path %q: %w", path, err)
}
ext := filepath.Ext(absolutePath)
switch ext {
case ".json":
json, err := os.ReadFile(absolutePath)
if err != nil {
return nil, fmt.Errorf("failed to read %q: %w", path, err)
}
return schema.ParseSchemaJSON(json)
case ".ridl":
// Use root FS to allow RIDL file imports from parent directories,
// ie. import ../../common.ridl.
root := "/"
// Support Windows paths. Currently only supports paths on the same volume.
if volume := filepath.VolumeName(absolutePath); volume != "" {
root = volume + "/"
}
path := filepath.ToSlash(absolutePath[len(root):])
rootFS := os.DirFS(root)
r := ridl.NewParser(rootFS, path)
return r.Parse()
default:
return nil, fmt.Errorf("invalid schema file extension %q", ext)
}
}