-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcompiler.go
219 lines (180 loc) · 6 KB
/
compiler.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package jsonschema
import (
"context"
"encoding/base64"
"encoding/xml"
"io"
"net/http"
"time"
"github.com/goccy/go-json"
"github.com/goccy/go-yaml"
)
// Compiler is a structure that manages schema compilation and validation.
type Compiler struct {
schemas map[string]*Schema // Cache of compiled schemas.
Decoders map[string]func(string) ([]byte, error) // Decoders for various encoding formats.
MediaTypes map[string]func([]byte) (interface{}, error) // Media type handlers for unmarshalling data.
Loaders map[string]func(url string) (io.ReadCloser, error) // Functions to load schemas from URLs.
DefaultBaseURI string // Base URI used to resolve relative references.
AssertFormat bool // Flag to enforce format validation.
}
// NewCompiler creates a new Compiler instance and initializes it with default settings.
func NewCompiler() *Compiler {
compiler := &Compiler{
schemas: make(map[string]*Schema),
Decoders: make(map[string]func(string) ([]byte, error)),
MediaTypes: make(map[string]func([]byte) (interface{}, error)),
Loaders: make(map[string]func(url string) (io.ReadCloser, error)),
DefaultBaseURI: "",
AssertFormat: false,
}
compiler.initDefaults()
return compiler
}
// Compile compiles a JSON schema and caches it. If an URI is provided, it uses that as the key; otherwise, it generates a hash.
func (c *Compiler) Compile(jsonSchema []byte, uris ...string) (*Schema, error) {
schema, err := newSchema(jsonSchema)
if err != nil {
return nil, err
}
uri := schema.ID
if uri == "" && len(uris) > 0 {
uri = uris[0]
}
if uri != "" && isValidURI(uri) {
schema.uri = uri
if existingSchema, exists := c.schemas[uri]; exists {
return existingSchema, nil
}
}
schema.initializeSchema(c, nil)
if schema.uri != "" && isValidURI(schema.uri) {
c.SetSchema(schema.uri, schema)
}
return schema, nil
}
// resolveSchemaURL attempts to fetch and compile a schema from a URL.
func (c *Compiler) resolveSchemaURL(url string) (*Schema, error) {
id, anchor := splitRef(url)
if schema, exists := c.schemas[id]; exists {
return schema, nil // Return cached schema if available
}
loader, ok := c.Loaders[getURLScheme(url)]
if !ok {
return nil, ErrNoLoaderRegistered
}
body, err := loader(url)
if err != nil {
return nil, err
}
defer body.Close() //nolint:errcheck
data, err := io.ReadAll(body)
if err != nil {
return nil, ErrFailedToReadData
}
schema, err := c.Compile(data, id)
if err != nil {
return nil, err
}
if anchor != "" {
return schema.resolveAnchor(anchor)
}
return schema, nil
}
// SetSchema associates a specific schema with a URI.
func (c *Compiler) SetSchema(uri string, schema *Schema) *Compiler {
c.schemas[uri] = schema
return c
}
// GetSchema retrieves a schema by reference. If the schema is not found in the cache and the ref is a URL, it tries to resolve it.
func (c *Compiler) GetSchema(ref string) (*Schema, error) {
baseURI, anchor := splitRef(ref)
if schema, exists := c.schemas[baseURI]; exists {
if baseURI == ref {
return schema, nil
}
return schema.resolveAnchor(anchor)
}
return c.resolveSchemaURL(ref)
}
// SetDefaultBaseURI sets the default base URL for resolving relative references.
func (c *Compiler) SetDefaultBaseURI(baseURI string) *Compiler {
c.DefaultBaseURI = baseURI
return c
}
// SetAssertFormat enables or disables format assertion.
func (c *Compiler) SetAssertFormat(assert bool) *Compiler {
c.AssertFormat = assert
return c
}
// RegisterDecoder adds a new decoder function for a specific encoding.
func (c *Compiler) RegisterDecoder(encodingName string, decoderFunc func(string) ([]byte, error)) *Compiler {
c.Decoders[encodingName] = decoderFunc
return c
}
// RegisterMediaType adds a new unmarshal function for a specific media type.
func (c *Compiler) RegisterMediaType(mediaTypeName string, unmarshalFunc func([]byte) (interface{}, error)) *Compiler {
c.MediaTypes[mediaTypeName] = unmarshalFunc
return c
}
// RegisterLoader adds a new loader function for a specific URI scheme.
func (c *Compiler) RegisterLoader(scheme string, loaderFunc func(url string) (io.ReadCloser, error)) *Compiler {
c.Loaders[scheme] = loaderFunc
return c
}
// initDefaults initializes default values for decoders, media types, and loaders.
func (c *Compiler) initDefaults() {
c.Decoders["base64"] = base64.StdEncoding.DecodeString
c.setupMediaTypes()
c.setupLoaders()
}
// setupMediaTypes configures default media type handlers.
func (c *Compiler) setupMediaTypes() {
c.MediaTypes["application/json"] = func(data []byte) (interface{}, error) {
var temp interface{}
if err := json.Unmarshal(data, &temp); err != nil {
return nil, ErrJSONUnmarshalError
}
return temp, nil
}
c.MediaTypes["application/xml"] = func(data []byte) (interface{}, error) {
var temp interface{}
if err := xml.Unmarshal(data, &temp); err != nil {
return nil, ErrXMLUnmarshalError
}
return temp, nil
}
c.MediaTypes["application/yaml"] = func(data []byte) (interface{}, error) {
var temp interface{}
if err := yaml.Unmarshal(data, &temp); err != nil {
return nil, ErrYAMLUnmarshalError
}
return temp, nil
}
}
// setupLoaders configures default loaders for fetching schemas via HTTP/HTTPS.
func (c *Compiler) setupLoaders() {
client := &http.Client{
Timeout: 10 * time.Second, // Set a reasonable timeout for network requests.
}
defaultHTTPLoader := func(url string) (io.ReadCloser, error) {
req, err := http.NewRequestWithContext(context.Background(), "GET", url, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, ErrFailedToFetch
}
if resp.StatusCode != http.StatusOK {
err = resp.Body.Close()
if err != nil {
return nil, err
}
return nil, ErrInvalidHTTPStatusCode
}
return resp.Body, nil
}
c.RegisterLoader("http", defaultHTTPLoader)
c.RegisterLoader("https", defaultHTTPLoader)
}