Skip to content

Commit

Permalink
Reduce debt
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
fredbi committed Dec 14, 2018
1 parent 5b6cdde commit cfd46eb
Show file tree
Hide file tree
Showing 29 changed files with 1,041 additions and 885 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ linters-settings:
golint:
min-confidence: 0
gocyclo:
min-complexity: 25
min-complexity: 45
maligned:
suggest-new: true
dupl:
Expand Down
21 changes: 14 additions & 7 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,24 @@ func TestSerialization_AuthSerialization(t *testing.T) {
assertSerializeJSON(
t,
OAuth2AccessToken("http://foo.com/authorization", "http://foo.com/token"),
`{"type":"oauth2","flow":"accessCode","authorizationUrl":"http://foo.com/authorization","tokenUrl":"http://foo.com/token"}`)
`{"type":"oauth2","flow":"accessCode","authorizationUrl":"http://foo.com/authorization",`+
`"tokenUrl":"http://foo.com/token"}`)

auth1 := OAuth2Implicit("http://foo.com/authorization")
auth1.AddScope("email", "read your email")
assertSerializeJSON(
t,
auth1,
`{"type":"oauth2","flow":"implicit","authorizationUrl":"http://foo.com/authorization","scopes":{"email":"read your email"}}`)
`{"type":"oauth2","flow":"implicit","authorizationUrl":"http://foo.com/authorization",`+
`"scopes":{"email":"read your email"}}`)

auth2 := OAuth2Password("http://foo.com/authorization")
auth2.AddScope("email", "read your email")
assertSerializeJSON(
t,
auth2,
`{"type":"oauth2","flow":"password","tokenUrl":"http://foo.com/authorization","scopes":{"email":"read your email"}}`)
`{"type":"oauth2","flow":"password","tokenUrl":"http://foo.com/authorization",`+
`"scopes":{"email":"read your email"}}`)

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

func TestSerialization_AuthDeserialization(t *testing.T) {
Expand Down Expand Up @@ -97,13 +101,15 @@ func TestSerialization_AuthDeserialization(t *testing.T) {

assertParsesJSON(
t,
`{"authorizationUrl":"http://foo.com/authorization","flow":"accessCode","tokenUrl":"http://foo.com/token","type":"oauth2"}`,
`{"authorizationUrl":"http://foo.com/authorization","flow":"accessCode","tokenUrl":"http://foo.com/token",`+
`"type":"oauth2"}`,
OAuth2AccessToken("http://foo.com/authorization", "http://foo.com/token"))

auth1 := OAuth2Implicit("http://foo.com/authorization")
auth1.AddScope("email", "read your email")
assertParsesJSON(t,
`{"authorizationUrl":"http://foo.com/authorization","flow":"implicit","scopes":{"email":"read your email"},"type":"oauth2"}`,
`{"authorizationUrl":"http://foo.com/authorization","flow":"implicit","scopes":{"email":"read your email"},`+
`"type":"oauth2"}`,
auth1)

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

}
95 changes: 66 additions & 29 deletions bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package spec

import "sync"

// ResolutionCache a cache for resolving urls
type ResolutionCache interface {
Get(string) (interface{}, bool)
Set(string, interface{})
}

type simpleCache struct {
lock sync.RWMutex
store map[string]interface{}
}

// Get retrieves a cached URI
func (s *simpleCache) Get(uri string) (interface{}, bool) {
debugLog("getting %q from resolution cache", uri)
s.lock.RLock()
v, ok := s.store[uri]
debugLog("got %q from resolution cache: %t", uri, ok)

s.lock.RUnlock()
return v, ok
}

// Set caches a URI
func (s *simpleCache) Set(uri string, data interface{}) {
s.lock.Lock()
s.store[uri] = data
s.lock.Unlock()
}

var resCache ResolutionCache

func init() {
resCache = initResolutionCache()
}

// initResolutionCache initializes the URI resolution cache
func initResolutionCache() ResolutionCache {
return &simpleCache{store: map[string]interface{}{
"http://swagger.io/v2/schema.json": MustLoadSwagger20Schema(),
"http://json-schema.org/draft-04/schema": MustLoadJSONSchemaDraft04(),
}}
}
4 changes: 2 additions & 2 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (

var (
// Debug is true when the SWAGGER_DEBUG env var is not empty.
// It enables a more verbose logging of validators.
// It enables a more verbose logging of this package.
Debug = os.Getenv("SWAGGER_DEBUG") != ""
// validateLogger is a debug logger for this package
// specLogger is a debug logger for this package
specLogger *log.Logger
)

Expand Down
Loading

0 comments on commit cfd46eb

Please sign in to comment.