...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package spec
16
17 import (
18 "sync"
19 )
20
21
22 type ResolutionCache interface {
23 Get(string) (interface{}, bool)
24 Set(string, interface{})
25 }
26
27 type simpleCache struct {
28 lock sync.RWMutex
29 store map[string]interface{}
30 }
31
32 func (s *simpleCache) ShallowClone() ResolutionCache {
33 store := make(map[string]interface{}, len(s.store))
34 s.lock.RLock()
35 for k, v := range s.store {
36 store[k] = v
37 }
38 s.lock.RUnlock()
39
40 return &simpleCache{
41 store: store,
42 }
43 }
44
45
46 func (s *simpleCache) Get(uri string) (interface{}, bool) {
47 s.lock.RLock()
48 v, ok := s.store[uri]
49
50 s.lock.RUnlock()
51 return v, ok
52 }
53
54
55 func (s *simpleCache) Set(uri string, data interface{}) {
56 s.lock.Lock()
57 s.store[uri] = data
58 s.lock.Unlock()
59 }
60
61 var (
62
63
64
65
66
67
68
69
70
71 resCache *simpleCache
72 onceCache sync.Once
73
74 _ ResolutionCache = &simpleCache{}
75 )
76
77
78 func initResolutionCache() {
79 resCache = defaultResolutionCache()
80 }
81
82 func defaultResolutionCache() *simpleCache {
83 return &simpleCache{store: map[string]interface{}{
84 "http://swagger.io/v2/schema.json": MustLoadSwagger20Schema(),
85 "http://json-schema.org/draft-04/schema": MustLoadJSONSchemaDraft04(),
86 }}
87 }
88
89 func cacheOrDefault(cache ResolutionCache) ResolutionCache {
90 onceCache.Do(initResolutionCache)
91
92 if cache != nil {
93 return cache
94 }
95
96
97 return resCache.ShallowClone()
98 }
99
View as plain text