mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-11 19:08:20 +00:00
add recursive jsonschema loader
This commit is contained in:
parent
80383847de
commit
5483f7f39d
1 changed files with 29 additions and 16 deletions
|
@ -6,35 +6,48 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/santhosh-tekuri/jsonschema/v5"
|
"github.com/santhosh-tekuri/jsonschema/v5"
|
||||||
|
"golang.org/x/exp/slog"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed schemas/*.schema.json
|
//go:embed schema/*
|
||||||
var schemasFS embed.FS
|
var schemasFS embed.FS
|
||||||
var schema *jsonschema.Schema
|
var schema *jsonschema.Schema
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
all, err := schemasFS.ReadDir("schemas")
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Errorf("Reading Schemas: %w", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
c := jsonschema.NewCompiler()
|
c := jsonschema.NewCompiler()
|
||||||
|
|
||||||
for _, f := range all {
|
addFolderResources(c, "schema")
|
||||||
data, err := schemasFS.Open(filepath.Join("schemas", f.Name()))
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Errorf("Reading Schema: %w", err))
|
|
||||||
}
|
|
||||||
err = c.AddResource("https://nfsense.net/"+f.Name(), data)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Errorf("Adding Schema: %w", err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s, err := c.Compile("https://nfsense.net/config.schema.json")
|
s, err := c.Compile("https://nfsense.net/schema/config/config.schema.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("Reading Schemas: %w", err))
|
panic(fmt.Errorf("Reading Schemas: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
schema = s
|
schema = s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addFolderResources(c *jsonschema.Compiler, path string) {
|
||||||
|
all, err := schemasFS.ReadDir(path)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("Reading Schemas: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range all {
|
||||||
|
fullpath := filepath.Join(path, f.Name())
|
||||||
|
slog.Debug("Checking Path", "fullpath", fullpath, "dir", f.IsDir())
|
||||||
|
if f.IsDir() {
|
||||||
|
addFolderResources(c, fullpath)
|
||||||
|
} else {
|
||||||
|
data, err := schemasFS.Open(fullpath)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("Reading Schema: %w", err))
|
||||||
|
}
|
||||||
|
slog.Debug("Adding Resource", "id", "https://nfsense.net/"+fullpath)
|
||||||
|
err = c.AddResource("https://nfsense.net/"+fullpath, data)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("Adding Schema: %w", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue