Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get swagger instance from register #1298

Merged
merged 1 commit into from Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}