Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / github.com / go-openapi / spec / cache.go
1 // Copyright 2015 go-swagger maintainers
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package spec
16
17 import "sync"
18
19 // ResolutionCache a cache for resolving urls
20 type ResolutionCache interface {
21         Get(string) (interface{}, bool)
22         Set(string, interface{})
23 }
24
25 type simpleCache struct {
26         lock  sync.RWMutex
27         store map[string]interface{}
28 }
29
30 // Get retrieves a cached URI
31 func (s *simpleCache) Get(uri string) (interface{}, bool) {
32         debugLog("getting %q from resolution cache", uri)
33         s.lock.RLock()
34         v, ok := s.store[uri]
35         debugLog("got %q from resolution cache: %t", uri, ok)
36
37         s.lock.RUnlock()
38         return v, ok
39 }
40
41 // Set caches a URI
42 func (s *simpleCache) Set(uri string, data interface{}) {
43         s.lock.Lock()
44         s.store[uri] = data
45         s.lock.Unlock()
46 }
47
48 var resCache ResolutionCache
49
50 func init() {
51         resCache = initResolutionCache()
52 }
53
54 // initResolutionCache initializes the URI resolution cache
55 func initResolutionCache() ResolutionCache {
56         return &simpleCache{store: map[string]interface{}{
57                 "http://swagger.io/v2/schema.json":       MustLoadSwagger20Schema(),
58                 "http://json-schema.org/draft-04/schema": MustLoadJSONSchemaDraft04(),
59         }}
60 }