-
Notifications
You must be signed in to change notification settings - Fork 54
/
schema_registry.go
90 lines (79 loc) · 2.23 KB
/
schema_registry.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package jsonschema
import (
"context"
"fmt"
"strings"
)
var sr *SchemaRegistry
// SchemaRegistry maintains a lookup table between schema string references
// and actual schemas
type SchemaRegistry struct {
schemaLookup map[string]*Schema
contextLookup map[string]*Schema
}
// GetSchemaRegistry provides an accessor to a globally available schema registry
func GetSchemaRegistry() *SchemaRegistry {
if sr == nil {
sr = &SchemaRegistry{
schemaLookup: map[string]*Schema{},
contextLookup: map[string]*Schema{},
}
}
return sr
}
// ResetSchemaRegistry resets the main SchemaRegistry
func ResetSchemaRegistry() {
sr = nil
}
// Get fetches a schema from the top level context registry or fetches it from a remote
func (sr *SchemaRegistry) Get(ctx context.Context, uri string) *Schema {
uri = strings.TrimRight(uri, "#")
schema := sr.schemaLookup[uri]
if schema == nil {
fetchedSchema := &Schema{}
err := FetchSchema(ctx, uri, fetchedSchema)
if err != nil {
schemaDebug(fmt.Sprintf("[SchemaRegistry] Fetch error: %s", err.Error()))
return nil
}
if fetchedSchema == nil {
return nil
}
fetchedSchema.docPath = uri
// TODO(arqu): meta validate schema
schema = fetchedSchema
sr.schemaLookup[uri] = schema
}
return schema
}
// GetKnown fetches a schema from the top level context registry
func (sr *SchemaRegistry) GetKnown(uri string) *Schema {
uri = strings.TrimRight(uri, "#")
return sr.schemaLookup[uri]
}
// GetLocal fetches a schema from the local context registry
func (sr *SchemaRegistry) GetLocal(uri string) *Schema {
uri = strings.TrimRight(uri, "#")
return sr.contextLookup[uri]
}
// Register registers a schema to the top level context
func (sr *SchemaRegistry) Register(sch *Schema) {
if sch.docPath == "" {
return
}
sr.schemaLookup[sch.docPath] = sch
}
// RegisterLocal registers a schema to a local context
func (sr *SchemaRegistry) RegisterLocal(sch *Schema) {
if sch.id != "" && IsLocalSchemaID(sch.id) {
sr.contextLookup[sch.id] = sch
}
if sch.HasKeyword("$anchor") {
anchorKeyword := sch.keywords["$anchor"].(*Anchor)
anchorURI := sch.docPath + "#" + string(*anchorKeyword)
if sr.contextLookup == nil {
sr.contextLookup = map[string]*Schema{}
}
sr.contextLookup[anchorURI] = sch
}
}