Skip to content

Commit cfd46eb

Browse files
committed
Reduce debt
* get up to date with golangci linters * factorized duplicate code in expander * split expander into more specialized bits * More UT with headers * linting: simplified returned errors * regenerated bindata with Kevin Burke's go-bindata fork Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 5b6cdde commit cfd46eb

29 files changed

+1041
-885
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ linters-settings:
44
golint:
55
min-confidence: 0
66
gocyclo:
7-
min-complexity: 25
7+
min-complexity: 45
88
maligned:
99
suggest-new: true
1010
dupl:

auth_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,24 @@ func TestSerialization_AuthSerialization(t *testing.T) {
4040
assertSerializeJSON(
4141
t,
4242
OAuth2AccessToken("http://foo.com/authorization", "http://foo.com/token"),
43-
`{"type":"oauth2","flow":"accessCode","authorizationUrl":"http://foo.com/authorization","tokenUrl":"http://foo.com/token"}`)
43+
`{"type":"oauth2","flow":"accessCode","authorizationUrl":"http://foo.com/authorization",`+
44+
`"tokenUrl":"http://foo.com/token"}`)
4445

4546
auth1 := OAuth2Implicit("http://foo.com/authorization")
4647
auth1.AddScope("email", "read your email")
4748
assertSerializeJSON(
4849
t,
4950
auth1,
50-
`{"type":"oauth2","flow":"implicit","authorizationUrl":"http://foo.com/authorization","scopes":{"email":"read your email"}}`)
51+
`{"type":"oauth2","flow":"implicit","authorizationUrl":"http://foo.com/authorization",`+
52+
`"scopes":{"email":"read your email"}}`)
5153

5254
auth2 := OAuth2Password("http://foo.com/authorization")
5355
auth2.AddScope("email", "read your email")
5456
assertSerializeJSON(
5557
t,
5658
auth2,
57-
`{"type":"oauth2","flow":"password","tokenUrl":"http://foo.com/authorization","scopes":{"email":"read your email"}}`)
59+
`{"type":"oauth2","flow":"password","tokenUrl":"http://foo.com/authorization",`+
60+
`"scopes":{"email":"read your email"}}`)
5861

5962
auth3 := OAuth2Application("http://foo.com/token")
6063
auth3.AddScope("email", "read your email")
@@ -68,7 +71,8 @@ func TestSerialization_AuthSerialization(t *testing.T) {
6871
assertSerializeJSON(
6972
t,
7073
auth4,
71-
`{"type":"oauth2","flow":"accessCode","authorizationUrl":"http://foo.com/authorization","tokenUrl":"http://foo.com/token","scopes":{"email":"read your email"}}`)
74+
`{"type":"oauth2","flow":"accessCode","authorizationUrl":"http://foo.com/authorization",`+
75+
`"tokenUrl":"http://foo.com/token","scopes":{"email":"read your email"}}`)
7276
}
7377

7478
func TestSerialization_AuthDeserialization(t *testing.T) {
@@ -97,13 +101,15 @@ func TestSerialization_AuthDeserialization(t *testing.T) {
97101

98102
assertParsesJSON(
99103
t,
100-
`{"authorizationUrl":"http://foo.com/authorization","flow":"accessCode","tokenUrl":"http://foo.com/token","type":"oauth2"}`,
104+
`{"authorizationUrl":"http://foo.com/authorization","flow":"accessCode","tokenUrl":"http://foo.com/token",`+
105+
`"type":"oauth2"}`,
101106
OAuth2AccessToken("http://foo.com/authorization", "http://foo.com/token"))
102107

103108
auth1 := OAuth2Implicit("http://foo.com/authorization")
104109
auth1.AddScope("email", "read your email")
105110
assertParsesJSON(t,
106-
`{"authorizationUrl":"http://foo.com/authorization","flow":"implicit","scopes":{"email":"read your email"},"type":"oauth2"}`,
111+
`{"authorizationUrl":"http://foo.com/authorization","flow":"implicit","scopes":{"email":"read your email"},`+
112+
`"type":"oauth2"}`,
107113
auth1)
108114

109115
auth2 := OAuth2Password("http://foo.com/token")
@@ -122,7 +128,8 @@ func TestSerialization_AuthDeserialization(t *testing.T) {
122128
auth4.AddScope("email", "read your email")
123129
assertParsesJSON(
124130
t,
125-
`{"authorizationUrl":"http://foo.com/authorization","flow":"accessCode","scopes":{"email":"read your email"},"tokenUrl":"http://foo.com/token","type":"oauth2"}`,
131+
`{"authorizationUrl":"http://foo.com/authorization","flow":"accessCode","scopes":{"email":"read your email"},`+
132+
`"tokenUrl":"http://foo.com/token","type":"oauth2"}`,
126133
auth4)
127134

128135
}

bindata.go

Lines changed: 66 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cache.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

debug.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import (
2424

2525
var (
2626
// Debug is true when the SWAGGER_DEBUG env var is not empty.
27-
// It enables a more verbose logging of validators.
27+
// It enables a more verbose logging of this package.
2828
Debug = os.Getenv("SWAGGER_DEBUG") != ""
29-
// validateLogger is a debug logger for this package
29+
// specLogger is a debug logger for this package
3030
specLogger *log.Logger
3131
)
3232

0 commit comments

Comments
 (0)