Skip to content

Commit

Permalink
feat: get swagger instance from register (#1298)
Browse files Browse the repository at this point in the history
Signed-off-by: Eray Ates <eates23@gmail.com>

Signed-off-by: Eray Ates <eates23@gmail.com>
  • Loading branch information
rytsh committed Aug 19, 2022
1 parent 23661ef commit cf03796
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions swagger.go
Expand Up @@ -39,6 +39,15 @@ func Register(name string, swagger Swagger) {
swags[name] = swagger
}

// GetSwagger returns the swagger instance for given name.
// If not found, returns nil.
func GetSwagger(name string) Swagger {
swaggerMu.RLock()
defer swaggerMu.RUnlock()

return swags[name]
}

// ReadDoc reads swagger document. An optional name parameter can be passed to read a specific document.
// The default name is "swagger".
func ReadDoc(optionalName ...string) (string, error) {
Expand Down
11 changes: 11 additions & 0 deletions swagger_test.go
Expand Up @@ -219,3 +219,14 @@ func TestCalledTwicelRegister(t *testing.T) {
func setup() {
swags = nil
}

func TestGetSwagger(t *testing.T) {
setup()
instance := &s{}
Register(Name, instance)
swagger := GetSwagger(Name)
assert.Equal(t, instance, swagger)

swagger = GetSwagger("invalid")
assert.Nil(t, swagger)
}

0 comments on commit cf03796

Please sign in to comment.