-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonschema.go
58 lines (45 loc) · 1.46 KB
/
jsonschema.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
50
51
52
53
54
55
56
57
58
package ortfodb
import (
"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, 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) *jsonschema.Schema {
selectedReflector := jsonschema.Reflector{}
if yaml {
selectedReflector = yamlReflector
}
selectedReflector.AddGoComments("github.com/ortfo/db", "./")
schema := selectedReflector.Reflect(t)
parts := strings.Split(string(schema.ID), "/")
base := parts[len(parts)-1]
setSchemaId(schema, base)
return schema
}
func ConfigurationJSONSchema() *jsonschema.Schema {
return makeJSONSchema(&Configuration{}, true)
}
func DatabaseJSONSchema() *jsonschema.Schema {
return makeJSONSchema(&Database{}, false)
}
type tags []Tag
func TagsRepositoryJSONSchema() *jsonschema.Schema {
return makeJSONSchema(&tags{}, true)
}
type technologies []Technology
func TechnologiesRepositoryJSONSchema() *jsonschema.Schema {
return makeJSONSchema(&technologies{}, true)
}
func ExporterManifestJSONSchema() *jsonschema.Schema {
schema := makeJSONSchema(&ExporterManifest{}, true)
setSchemaId(schema, "exporter")
return schema
}