-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml_validator_schemas.go
73 lines (55 loc) · 1.21 KB
/
yaml_validator_schemas.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
package core
import (
"sync"
"github.com/santhosh-tekuri/jsonschema/v5"
)
type Schema struct {
Content *string
compiled *jsonschema.Schema
mu sync.Mutex
}
type SchemaList map[string]*Schema
func (s *SchemaList) FindCompiled(url string) *jsonschema.Schema {
schema, err := s.Find(url)
if err != nil {
panic(err)
}
// Prevent race condition during compilation
schema.mu.Lock()
if schema.compiled == nil {
compiled, err2 := s.Compile(url)
if err2 != nil {
panic(err2)
}
schema.compiled = compiled
}
schema.mu.Unlock()
return schema.compiled
}
func (s *SchemaList) FindContent(url string) (*string, error) {
schema, err := s.Find(url)
if err != nil {
return nil, err
}
if schema.Content == nil {
return nil, EmptySchemaError(url)
}
return schema.Content, nil
}
func (s *SchemaList) Find(url string) (*Schema, error) {
schema, ok := (*s)[url]
if !ok {
return nil, SchemaNotFoundError(url)
}
if schema == nil {
return nil, SchemaIsNilError(url)
}
return schema, nil
}
func (s *SchemaList) Compile(url string) (*jsonschema.Schema, error) {
val, err := jsonschema.Compile(url)
if err != nil {
return nil, SchemaCompilationError(url, err.Error())
}
return val, nil
}