Skip to content

Commit

Permalink
change interface{} to any (#5)
Browse files Browse the repository at this point in the history
* change interface{} to any

* update go version
  • Loading branch information
acoshift authored Oct 19, 2022
1 parent 052c570 commit b716d97
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ Convert RPC style function into http.Handler
- context.Context
- *http.Request
- http.ResponseWriter
- interface{}
- any

## Support output types

- interface{}
- any
- error

## Usage
Expand All @@ -29,10 +29,10 @@ import "github.com/acoshift/hrpc/v3"

```go
m := hrpc.Manager{
Decoder: func(r *http.Request, dst interface{}) error {
Decoder: func(r *http.Request, dst any) error {
return json.NewDecoder(r.Body).Decode(dst)
},
Encoder: func(w http.ResponseWriter, r *http.Request, res interface{}) {
Encoder: func(w http.ResponseWriter, r *http.Request, res any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(res)
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/acoshift/hrpc/v3

go 1.12
go 1.19
22 changes: 11 additions & 11 deletions hrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

// Decoder is the request decoder
type Decoder func(*http.Request, interface{}) error
type Decoder func(*http.Request, any) error

// Encoder is the response encoder
type Encoder func(http.ResponseWriter, *http.Request, interface{})
type Encoder func(http.ResponseWriter, *http.Request, any)

// ErrorEncoder is the error response encoder
type ErrorEncoder func(http.ResponseWriter, *http.Request, error)
Expand All @@ -24,14 +24,14 @@ type Manager struct {

func (m *Manager) decoder() Decoder {
if m.Decoder == nil {
return func(*http.Request, interface{}) error { return nil }
return func(*http.Request, any) error { return nil }
}
return m.Decoder
}

func (m *Manager) encoder() Encoder {
if m.Encoder == nil {
return func(http.ResponseWriter, *http.Request, interface{}) {}
return func(http.ResponseWriter, *http.Request, any) {}
}
return m.Encoder
}
Expand All @@ -55,7 +55,7 @@ const (
miContext // context.Context
miRequest // *http.Request
miResponseWriter // http.ResponseWriter
miInterface // interface{}
miAny // any
miError // error
)

Expand All @@ -79,7 +79,7 @@ func setOrPanic(m map[mapIndex]int, k mapIndex, v int) {
// second input can be anything which will pass to RequestDecoder function.
// first output must be the result which will pass to success handler.
// second output must be an error interface which will pass to error handler if not nil.
func (m *Manager) Handler(f interface{}) http.Handler {
func (m *Manager) Handler(f any) http.Handler {
fv := reflect.ValueOf(f)
ft := fv.Type()
if ft.Kind() != reflect.Func {
Expand All @@ -106,7 +106,7 @@ func (m *Manager) Handler(f interface{}) http.Handler {
case strResponseWriter:
setOrPanic(mapIn, miResponseWriter, i)
default:
setOrPanic(mapIn, miInterface, i)
setOrPanic(mapIn, miAny, i)
}
}

Expand All @@ -118,15 +118,15 @@ func (m *Manager) Handler(f interface{}) http.Handler {
case strError:
setOrPanic(mapOut, miError, i)
default:
setOrPanic(mapOut, miInterface, i)
setOrPanic(mapOut, miAny, i)
}
}

var (
infType reflect.Type
infPtr bool
)
if i, ok := mapIn[miInterface]; ok {
if i, ok := mapIn[miAny]; ok {
infType = ft.In(i)
if infType.Kind() == reflect.Ptr {
infType = infType.Elem()
Expand All @@ -145,7 +145,7 @@ func (m *Manager) Handler(f interface{}) http.Handler {
vIn[i] = reflect.ValueOf(r.Context())
}
// inject request interface
if i, ok := mapIn[miInterface]; ok {
if i, ok := mapIn[miAny]; ok {
rfReq := reflect.New(infType)
req := rfReq.Interface()
err := decoder(r, req)
Expand Down Expand Up @@ -189,7 +189,7 @@ func (m *Manager) Handler(f interface{}) http.Handler {
}
}
// check response
if i, ok := mapOut[miInterface]; ok {
if i, ok := mapOut[miAny]; ok {
encoder(w, r, vOut[i].Interface())
}

Expand Down
22 changes: 11 additions & 11 deletions hrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"
)

func jsonDecoder(r *http.Request, dst interface{}) error {
func jsonDecoder(r *http.Request, dst any) error {
return json.NewDecoder(r.Body).Decode(dst)
}

Expand Down Expand Up @@ -50,7 +50,7 @@ func TestHandler(t *testing.T) {

m := Manager{
Decoder: jsonDecoder,
Encoder: func(w http.ResponseWriter, r *http.Request, res interface{}) {
Encoder: func(w http.ResponseWriter, r *http.Request, res any) {
callSuccess = true
},
ErrorEncoder: func(w http.ResponseWriter, r *http.Request, err error) {
Expand All @@ -59,7 +59,7 @@ func TestHandler(t *testing.T) {
Validate: true,
}

h := m.Handler(func(ctx context.Context, req *requestType) (interface{}, error) {
h := m.Handler(func(ctx context.Context, req *requestType) (any, error) {
if req.Data != 1 {
t.Fatalf("invalid data")
}
Expand Down Expand Up @@ -113,15 +113,15 @@ func TestHandler(t *testing.T) {
h.ServeHTTP(w, r)
mustError()

h = m.Handler(func(ctx context.Context, req *requestType) (interface{}, error) {
h = m.Handler(func(ctx context.Context, req *requestType) (any, error) {
return nil, errors.New("some error")
})
r = httptest.NewRequest(http.MethodPost, "http://localhost", successBody)
reset()
h.ServeHTTP(w, r)
mustError()

h = m.Handler(func(r *http.Request, req *requestType) (interface{}, error) {
h = m.Handler(func(r *http.Request, req *requestType) (any, error) {
return map[string]int{"ok": 1}, nil
})
r = httptest.NewRequest(http.MethodPost, "http://localhost", successBody)
Expand All @@ -136,7 +136,7 @@ func TestHandler(t *testing.T) {
mustNothing()

// grpc style
h = m.Handler(func(ctx context.Context, req *requestType, opts ...interface{}) (interface{}, error) {
h = m.Handler(func(ctx context.Context, req *requestType, opts ...any) (any, error) {
return map[string]string{"ok": "1"}, nil
})
r = httptest.NewRequest(http.MethodPost, "http://localhost", successBody)
Expand All @@ -163,7 +163,7 @@ func TestHandler(t *testing.T) {
func TestDefault(t *testing.T) {
m := Manager{}
i := 0
h := m.Handler(func(ctx context.Context, req *requestType) (interface{}, error) {
h := m.Handler(func(ctx context.Context, req *requestType) (any, error) {
if i == 0 {
i++
return req, nil
Expand Down Expand Up @@ -193,24 +193,24 @@ func TestInvalidF(t *testing.T) {
}()
func() {
defer p()
m.Handler(func(ctx interface{}, req interface{}) (interface{}, error) {
m.Handler(func(ctx any, req any) (any, error) {
return nil, nil
})
}()
func() {
defer p()
m.Handler(func(ctx context.Context, req interface{}) (interface{}, interface{}) {
m.Handler(func(ctx context.Context, req any) (any, any) {
return nil, nil
})
}()
}

func ExampleManager() {
m := Manager{
Decoder: func(r *http.Request, dst interface{}) error {
Decoder: func(r *http.Request, dst any) error {
return json.NewDecoder(r.Body).Decode(dst)
},
Encoder: func(w http.ResponseWriter, r *http.Request, res interface{}) {
Encoder: func(w http.ResponseWriter, r *http.Request, res any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(w).Encode(res)
},
Expand Down

0 comments on commit b716d97

Please sign in to comment.