-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Implement error (and any) like interface #1212
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a9497ec
treat any and error as interface (empty type)
sapk b3f0c90
add basic test
sapk 2ab70ed
validate that interface{} = any = error result
sapk cfec0b6
:white_check_mark: add test (similar to gen) in parser package
sapk 1e6f665
...
sapk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
. "github.com/swaggo/swag/testdata/error/errors" | ||
_ "github.com/swaggo/swag/testdata/error/web" | ||
) | ||
|
||
// Upload do something | ||
// @Summary Upload file | ||
// @Description Upload file | ||
// @ID file.upload | ||
// @Accept multipart/form-data | ||
// @Produce json | ||
// @Param file formData file true "this is a test file" | ||
// @Success 200 {string} string "ok" | ||
// @Failure 400 {object} web.CrossErrors "Abort !!" | ||
// @Router /file/upload [post] | ||
func Upload(w http.ResponseWriter, r *http.Request) { | ||
//write your code | ||
_ = Errors{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package errors | ||
|
||
// CustomInterface some interface | ||
type CustomInterface interface { | ||
Error() string | ||
} | ||
|
||
// Errors errors and interfaces | ||
type Errors struct { | ||
Error error | ||
ErrorInterface CustomInterface | ||
Interface interface{} | ||
Any any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{ | ||
"swagger": "2.0", | ||
"info": { | ||
"description": "This is a sample server Petstore server.", | ||
"title": "Swagger Example API", | ||
"termsOfService": "http://swagger.io/terms/", | ||
"contact": { | ||
"name": "API Support", | ||
"url": "http://www.swagger.io/support", | ||
"email": "[email protected]" | ||
}, | ||
"license": { | ||
"name": "Apache 2.0", | ||
"url": "http://www.apache.org/licenses/LICENSE-2.0.html" | ||
}, | ||
"version": "1.0" | ||
}, | ||
"host": "petstore.swagger.io", | ||
"basePath": "/v2", | ||
"paths": { | ||
"/file/upload": { | ||
"post": { | ||
"description": "Upload file", | ||
"consumes": [ | ||
"multipart/form-data" | ||
], | ||
"produces": [ | ||
"application/json" | ||
], | ||
"summary": "Upload file", | ||
"operationId": "file.upload", | ||
"parameters": [ | ||
{ | ||
"type": "file", | ||
"description": "this is a test file", | ||
"name": "file", | ||
"in": "formData", | ||
"required": true | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "ok", | ||
"schema": { | ||
"type": "string" | ||
} | ||
}, | ||
"400": { | ||
"description": "Abort !!", | ||
"schema": { | ||
"$ref": "#/definitions/web.CrossErrors" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"definitions": { | ||
"web.CrossErrors": { | ||
"type": "object", | ||
"properties": { | ||
"any": {}, | ||
"error": {}, | ||
"errorInterface": {}, | ||
"interface": {} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/swaggo/swag/testdata/error/api" | ||
) | ||
|
||
// @title Swagger Example API | ||
// @version 1.0 | ||
// @description This is a sample server Petstore server. | ||
// @termsOfService http://swagger.io/terms/ | ||
|
||
// @contact.name API Support | ||
// @contact.url http://www.swagger.io/support | ||
// @contact.email [email protected] | ||
|
||
// @license.name Apache 2.0 | ||
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html | ||
|
||
// @host petstore.swagger.io | ||
// @BasePath /v2 | ||
|
||
func main() { | ||
http.HandleFunc("/testapi/upload", api.Upload) | ||
http.ListenAndServe(":8080", nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package web | ||
|
||
import ( | ||
"github.com/swaggo/swag/testdata/error/errors" | ||
) | ||
|
||
type CrossErrors errors.Errors |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to cover this with unit tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should already be covered by
TestGen_ErrorAndInterface
andTestIsInterfaceLike
but I can recheck.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a test similar to TestGen_ErrorAndInterface based on testdata in the root package.