-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add ortfodb schemas to get list of available json schemas, fix expo…
…rter schema $id
- Loading branch information
Showing
6 changed files
with
65 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,58 @@ | ||
package ortfodb | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/invopop/jsonschema" | ||
) | ||
|
||
var AvailableJSONSchemas = []string{"configuration", "database", "tags", "technologies", "exporter"} | ||
|
||
var yamlReflector = jsonschema.Reflector{ | ||
FieldNameTag: "yaml", | ||
KeyNamer: func(key string) string { return strings.ToLower(key) }, | ||
} | ||
|
||
func setSchemaId(schema *jsonschema.Schema) { | ||
parts := strings.Split(string(schema.ID), "/") | ||
base := parts[len(parts)-1] | ||
schema.ID = jsonschema.ID(fmt.Sprintf("https://raw.githubusercontent.com/ortfo/db/v%s/schemas/%s.schema.json", Version, base)) | ||
func setSchemaId(schema *jsonschema.Schema, name string) { | ||
schema.ID = jsonschema.ID(fmt.Sprintf("https://raw.githubusercontent.com/ortfo/db/v%s/schemas/%s.schema.json", Version, name)) | ||
} | ||
|
||
func makeJSONSchema(t any, yaml bool) string { | ||
func makeJSONSchema(t any, yaml bool) *jsonschema.Schema { | ||
selectedReflector := jsonschema.Reflector{} | ||
if yaml { | ||
selectedReflector = yamlReflector | ||
} | ||
selectedReflector.AddGoComments("github.com/ortfo/db", "./") | ||
schema := selectedReflector.Reflect(t) | ||
setSchemaId(schema) | ||
out, err := json.MarshalIndent(schema, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
return string(out) | ||
parts := strings.Split(string(schema.ID), "/") | ||
base := parts[len(parts)-1] | ||
setSchemaId(schema, base) | ||
return schema | ||
} | ||
|
||
func ConfigurationJSONSchema() string { | ||
func ConfigurationJSONSchema() *jsonschema.Schema { | ||
return makeJSONSchema(&Configuration{}, true) | ||
} | ||
|
||
func DatabaseJSONSchema() string { | ||
func DatabaseJSONSchema() *jsonschema.Schema { | ||
return makeJSONSchema(&Database{}, false) | ||
} | ||
|
||
type tags []Tag | ||
|
||
func TagsRepositoryJSONSchema() string { | ||
func TagsRepositoryJSONSchema() *jsonschema.Schema { | ||
return makeJSONSchema(&tags{}, true) | ||
} | ||
|
||
type technologies []Technology | ||
|
||
func TechnologiesRepositoryJSONSchema() string { | ||
func TechnologiesRepositoryJSONSchema() *jsonschema.Schema { | ||
return makeJSONSchema(&technologies{}, true) | ||
} | ||
|
||
func ExporterManifestJSONSchema() string { | ||
return makeJSONSchema(&ExporterManifest{}, true) | ||
func ExporterManifestJSONSchema() *jsonschema.Schema { | ||
schema := makeJSONSchema(&ExporterManifest{}, true) | ||
setSchemaId(schema, "exporter") | ||
return schema | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
#!/usr/bin/env python | ||
# Assuming ortfodb is available on PATH | ||
import json | ||
from pathlib import Path | ||
from subprocess import run | ||
from subprocess import run as _run | ||
|
||
root = Path(__file__).parent.parent | ||
|
||
for thing in ["tags", "technologies", "configuration", "database", "exporter"]: | ||
out = run(["./ortfodb", "schemas", thing], capture_output=True) | ||
schema = out.stdout.decode("utf-8") | ||
(root / "schemas" / f"{thing}.schema.json").write_text(schema) | ||
|
||
def run(cmd) -> str: | ||
out = _run(cmd.split(" "), capture_output=True) | ||
return out.stdout.decode("utf-8") | ||
|
||
|
||
for thing in run("./ortfodb schemas").splitlines(): | ||
schema = run(f"./ortfodb schemas {thing.strip()}") | ||
formatted_schema = json.dumps(json.loads(schema), indent=2) | ||
(root / "schemas" / f"{thing}.schema.json").write_text(formatted_schema + "\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters