Skip to content

Commit

Permalink
refactor: since go1.18 use any instead of interface{}
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Apr 2, 2022
1 parent bbd5dcb commit 330613d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
6 changes: 6 additions & 0 deletions any.go
@@ -0,0 +1,6 @@
//go:build !go1.18
// +build !go1.18

package httpmock

type any = interface{}
6 changes: 3 additions & 3 deletions doc.go
Expand Up @@ -32,7 +32,7 @@ Advanced Example:
defer httpmock.DeactivateAndReset()
// our database of articles
articles := make([]map[string]interface{}, 0)
articles := make([]map[string]any, 0)
// mock to list out the articles
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
Expand All @@ -50,7 +50,7 @@ Advanced Example:
func(req *http.Request) (*http.Response, error) {
// Get ID from request
id := httpmock.MustGetSubmatchAsUint(req, 1) // 1=first regexp submatch
return httpmock.NewJsonResponse(200, map[string]interface{}{
return httpmock.NewJsonResponse(200, map[string]any{
"id": id,
"name": "My Great Article",
})
Expand All @@ -60,7 +60,7 @@ Advanced Example:
// mock to add a new article
httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles",
func(req *http.Request) (*http.Response, error) {
article := make(map[string]interface{})
article := make(map[string]any)
if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
Expand Down
4 changes: 3 additions & 1 deletion go.mod
@@ -1,5 +1,7 @@
module github.com/jarcoal/httpmock

go 1.9
go 1.18

require github.com/maxatome/go-testdeep v1.11.0

require github.com/davecgh/go-spew v1.1.1 // indirect
34 changes: 17 additions & 17 deletions response.go
Expand Up @@ -37,7 +37,7 @@ var suggestedKey = suggestedKeyType{}
// a mocked response.
type Responder func(*http.Request) (*http.Response, error)

func (r Responder) times(name string, n int, fn ...func(...interface{})) Responder {
func (r Responder) times(name string, n int, fn ...func(...any)) Responder {
count := 0
return func(req *http.Request) (*http.Response, error) {
count++
Expand Down Expand Up @@ -71,7 +71,7 @@ func (r Responder) times(name string, n int, fn ...func(...interface{})) Respond
// httpmock.RegisterResponder("GET", "/foo/bar",
// httpmock.NewStringResponder(200, "{}").Times(3, t.Log),
// )
func (r Responder) Times(n int, fn ...func(...interface{})) Responder {
func (r Responder) Times(n int, fn ...func(...any)) Responder {
return r.times("Times", n, fn...)
}

Expand All @@ -92,7 +92,7 @@ func (r Responder) Times(n int, fn ...func(...interface{})) Responder {
// httpmock.RegisterResponder("GET", "/foo/bar",
// httpmock.NewStringResponder(200, "{}").Once(t.Log),
// )
func (r Responder) Once(fn ...func(...interface{})) Responder {
func (r Responder) Once(fn ...func(...any)) Responder {
return r.times("Once", 1, fn...)
}

Expand All @@ -110,7 +110,7 @@ func (r Responder) Once(fn ...func(...interface{})) Responder {
// httpmock.RegisterResponder("GET", "/foo/bar",
// httpmock.NewStringResponder(200, "{}").Trace(t.Log),
// )
func (r Responder) Trace(fn func(...interface{})) Responder {
func (r Responder) Trace(fn func(...any)) Responder {
return func(req *http.Request) (*http.Response, error) {
resp, err := r(req)
return resp, internal.StackTracer{
Expand Down Expand Up @@ -272,7 +272,7 @@ func ResponderFromResponse(resp *http.Response) Responder {
// t.Log),
// )
// }
func ResponderFromMultipleResponses(responses []*http.Response, fn ...func(...interface{})) Responder {
func ResponderFromMultipleResponses(responses []*http.Response, fn ...func(...any)) Responder {
responseIndex := 0
mutex := sync.Mutex{}
return func(req *http.Request) (*http.Response, error) {
Expand Down Expand Up @@ -339,7 +339,7 @@ func NewErrorResponder(err error) Responder {
// at /go/src/testing/testing.go:865
// testing.tRunner()
// at /go/src/runtime/asm_amd64.s:1337
func NewNotFoundResponder(fn func(...interface{})) Responder {
func NewNotFoundResponder(fn func(...any)) Responder {
return func(req *http.Request) (*http.Response, error) {
var extra string
suggested, _ := req.Context().Value(suggestedKey).(*suggestedInfo)
Expand Down Expand Up @@ -401,12 +401,12 @@ func NewBytesResponder(status int, body []byte) Responder {
}

// NewJsonResponse creates an *http.Response with a body that is a
// json encoded representation of the given interface{}. Also accepts
// json encoded representation of the given any. Also accepts
// an http status code.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewJsonResponse(200, httpmock.File("body.json"))
func NewJsonResponse(status int, body interface{}) (*http.Response, error) { // nolint: revive
func NewJsonResponse(status int, body any) (*http.Response, error) { // nolint: revive
encoded, err := json.Marshal(body)
if err != nil {
return nil, err
Expand All @@ -417,11 +417,11 @@ func NewJsonResponse(status int, body interface{}) (*http.Response, error) { //
}

// NewJsonResponder creates a Responder from a given body (as an
// interface{} that is encoded to json) and status code.
// any that is encoded to json) and status code.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewJsonResponder(200, httpmock.File("body.json"))
func NewJsonResponder(status int, body interface{}) (Responder, error) { // nolint: revive
func NewJsonResponder(status int, body any) (Responder, error) { // nolint: revive
resp, err := NewJsonResponse(status, body)
if err != nil {
return nil, err
Expand All @@ -442,7 +442,7 @@ func NewJsonResponder(status int, body interface{}) (Responder, error) { // noli
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewJsonResponderOrPanic(200, httpmock.File("body.json"))
func NewJsonResponderOrPanic(status int, body interface{}) Responder { // nolint: revive
func NewJsonResponderOrPanic(status int, body any) Responder { // nolint: revive
responder, err := NewJsonResponder(status, body)
if err != nil {
panic(err)
Expand All @@ -451,12 +451,12 @@ func NewJsonResponderOrPanic(status int, body interface{}) Responder { // nolint
}

// NewXmlResponse creates an *http.Response with a body that is an xml
// encoded representation of the given interface{}. Also accepts an
// encoded representation of the given any. Also accepts an
// http status code.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewXmlResponse(200, httpmock.File("body.xml"))
func NewXmlResponse(status int, body interface{}) (*http.Response, error) { // nolint: revive
func NewXmlResponse(status int, body any) (*http.Response, error) { // nolint: revive
var (
encoded []byte
err error
Expand All @@ -475,11 +475,11 @@ func NewXmlResponse(status int, body interface{}) (*http.Response, error) { // n
}

// NewXmlResponder creates a Responder from a given body (as an
// interface{} that is encoded to xml) and status code.
// any that is encoded to xml) and status code.
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewXmlResponder(200, httpmock.File("body.xml"))
func NewXmlResponder(status int, body interface{}) (Responder, error) { // nolint: revive
func NewXmlResponder(status int, body any) (Responder, error) { // nolint: revive
resp, err := NewXmlResponse(status, body)
if err != nil {
return nil, err
Expand All @@ -500,7 +500,7 @@ func NewXmlResponder(status int, body interface{}) (Responder, error) { // nolin
//
// To pass the content of an existing file as body use httpmock.File as in:
// httpmock.NewXmlResponderOrPanic(200, httpmock.File("body.xml"))
func NewXmlResponderOrPanic(status int, body interface{}) Responder { // nolint: revive
func NewXmlResponderOrPanic(status int, body any) Responder { // nolint: revive
responder, err := NewXmlResponder(status, body)
if err != nil {
panic(err)
Expand All @@ -527,7 +527,7 @@ func NewRespBodyFromBytes(body []byte) io.ReadCloser {
}

type dummyReadCloser struct {
orig interface{} // string or []byte
orig any // string or []byte
body io.ReadSeeker // instanciated on demand from orig
}

Expand Down
20 changes: 10 additions & 10 deletions transport.go
Expand Up @@ -605,7 +605,7 @@ func (m *MockTransport) RegisterRegexpResponder(method string, urlRegexp *regexp
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting m.DontCheckMethod to
// true prior to this call.
func (m *MockTransport) RegisterResponderWithQuery(method, path string, query interface{}, responder Responder) {
func (m *MockTransport) RegisterResponderWithQuery(method, path string, query any, responder Responder) {
if isRegexpURL(path) {
panic(`path begins with "=~", RegisterResponder should be used instead of RegisterResponderWithQuery`)
}
Expand Down Expand Up @@ -1074,7 +1074,7 @@ func RegisterRegexpResponder(method string, urlRegexp *regexp.Regexp, responder
// OPTIONS, POST, PUT or TRACE, a panics occurs to notice the possible
// mistake. This panic can be disabled by setting
// DefaultTransport.DontCheckMethod to true prior to this call.
func RegisterResponderWithQuery(method, path string, query interface{}, responder Responder) {
func RegisterResponderWithQuery(method, path string, query any, responder Responder) {
DefaultTransport.RegisterResponderWithQuery(method, path, query, responder)
}

Expand Down Expand Up @@ -1109,7 +1109,7 @@ var ErrSubmatchNotFound = errors.New("submatch not found")
// if err != nil {
// return nil, err
// }
// return NewJsonResponse(200, map[string]interface{}{
// return NewJsonResponse(200, map[string]any{
// "id": 123,
// "name": name,
// })
Expand Down Expand Up @@ -1140,7 +1140,7 @@ func GetSubmatch(req *http.Request, n int) (string, error) {
// if err != nil {
// return nil, err
// }
// return NewJsonResponse(200, map[string]interface{}{
// return NewJsonResponse(200, map[string]any{
// "id": id,
// "name": "The beautiful name",
// })
Expand All @@ -1166,7 +1166,7 @@ func GetSubmatchAsInt(req *http.Request, n int) (int64, error) {
// if err != nil {
// return nil, err
// }
// return NewJsonResponse(200, map[string]interface{}{
// return NewJsonResponse(200, map[string]any{
// "id": id,
// "name": "The beautiful name",
// })
Expand All @@ -1192,7 +1192,7 @@ func GetSubmatchAsUint(req *http.Request, n int) (uint64, error) {
// if err != nil {
// return nil, err
// }
// return NewJsonResponse(200, map[string]interface{}{
// return NewJsonResponse(200, map[string]any{
// "id": id,
// "name": "The beautiful name",
// "height": height,
Expand All @@ -1217,7 +1217,7 @@ func GetSubmatchAsFloat(req *http.Request, n int) (float64, error) {
// RegisterResponder("GET", `=~^/item/name/([^/]+)\z`,
// func(req *http.Request) (*http.Response, error) {
// name := MustGetSubmatch(req, 1) // 1=first regexp submatch
// return NewJsonResponse(200, map[string]interface{}{
// return NewJsonResponse(200, map[string]any{
// "id": 123,
// "name": name,
// })
Expand All @@ -1241,7 +1241,7 @@ func MustGetSubmatch(req *http.Request, n int) string {
// RegisterResponder("GET", `=~^/item/id/(\d+)\z`,
// func(req *http.Request) (*http.Response, error) {
// id := MustGetSubmatchAsInt(req, 1) // 1=first regexp submatch
// return NewJsonResponse(200, map[string]interface{}{
// return NewJsonResponse(200, map[string]any{
// "id": id,
// "name": "The beautiful name",
// })
Expand All @@ -1265,7 +1265,7 @@ func MustGetSubmatchAsInt(req *http.Request, n int) int64 {
// RegisterResponder("GET", `=~^/item/id/(\d+)\z`,
// func(req *http.Request) (*http.Response, error) {
// id, err := MustGetSubmatchAsUint(req, 1) // 1=first regexp submatch
// return NewJsonResponse(200, map[string]interface{}{
// return NewJsonResponse(200, map[string]any{
// "id": id,
// "name": "The beautiful name",
// })
Expand All @@ -1289,7 +1289,7 @@ func MustGetSubmatchAsUint(req *http.Request, n int) uint64 {
// RegisterResponder("PATCH", `=~^/item/id/\d+\?height=(\d+(?:\.\d*)?)\z`,
// func(req *http.Request) (*http.Response, error) {
// height := MustGetSubmatchAsFloat(req, 1) // 1=first regexp submatch
// return NewJsonResponse(200, map[string]interface{}{
// return NewJsonResponse(200, map[string]any{
// "id": id,
// "name": "The beautiful name",
// "height": height,
Expand Down

0 comments on commit 330613d

Please sign in to comment.