Skip to content

Commit

Permalink
refactor!: remove Empty type (use any instead) (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
abemedia authored May 14, 2023
1 parent 827209b commit 72848e8
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Greet(ctx context.Context, req GreetRequest) (*GreetResponse, error) {
return res, nil
}

func Pong(context.Context, don.Empty) (string, error) {
func Pong(context.Context, any) (string, error) {
return "pong", nil
}

Expand Down
4 changes: 0 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"github.com/valyala/fasthttp"
)

// Empty is the type used when an API handler contains either no request or no
// response.
type Empty struct{}

// DefaultEncoding contains the media type of the default encoding to fall back
// on if no `Accept` or `Content-Type` header was provided.
var DefaultEncoding = "text/plain"
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func BenchmarkDon_HTTP(b *testing.B) {
api := don.New(nil)
api.Get("/:path", don.H(func(ctx context.Context, req don.Empty) (string, error) {
api.Get("/:path", don.H(func(ctx context.Context, req any) (string, error) {
return "foo", nil
}))

Expand Down
2 changes: 1 addition & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func E(err error) fasthttp.RequestHandler {
h := H(func(context.Context, Empty) (*Empty, error) { return nil, err })
h := H(func(context.Context, any) (any, error) { return nil, err })
return func(ctx *fasthttp.RequestCtx) { h(ctx, nil) }
}

Expand Down
4 changes: 2 additions & 2 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
)

// Returns 204 - No Content
func Empty(context.Context, *don.Empty) (interface{}, error) {
func Empty(context.Context, any) (any, error) {
return nil, nil
}

func Ping(context.Context, *don.Empty) (string, error) {
func Ping(context.Context, any) (string, error) {
return "pong", nil
}

Expand Down
14 changes: 7 additions & 7 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestHandlerResponse(t *testing.T) {
"Content-Type": "text/plain; charset=utf-8",
},
},
handler: don.H(func(ctx context.Context, req don.Empty) (any, error) {
handler: don.H(func(ctx context.Context, req any) (any, error) {
return nil, nil
}),
},
Expand All @@ -98,7 +98,7 @@ func TestHandlerResponse(t *testing.T) {
},
},
config: &don.Config{DefaultEncoding: "application/json", DisableNoContent: true},
handler: don.H(func(ctx context.Context, req don.Empty) (any, error) {
handler: don.H(func(ctx context.Context, req any) (any, error) {
return nil, nil
}),
},
Expand All @@ -111,7 +111,7 @@ func TestHandlerResponse(t *testing.T) {
"Foo": "bar",
},
},
handler: don.H(func(ctx context.Context, req don.Empty) (any, error) {
handler: don.H(func(ctx context.Context, req any) (any, error) {
return &headerer{}, nil
}),
},
Expand All @@ -134,7 +134,7 @@ func TestHandlerResponse(t *testing.T) {
Body: "Not Acceptable\n",
Header: map[string]string{"Content-Type": "text/plain; charset=utf-8"},
},
handler: don.H(func(ctx context.Context, req don.Empty) ([]string, error) {
handler: don.H(func(ctx context.Context, req any) ([]string, error) {
return []string{"foo", "bar"}, nil
}),
request: request{header: map[string]string{"Accept": "text/plain; charset=utf-8"}},
Expand All @@ -146,7 +146,7 @@ func TestHandlerResponse(t *testing.T) {
Body: "Not Acceptable\n",
Header: map[string]string{"Content-Type": "text/plain; charset=utf-8"},
},
handler: don.H(func(ctx context.Context, req don.Empty) (any, error) {
handler: don.H(func(ctx context.Context, req any) (any, error) {
return nil, nil
}),
request: request{header: map[string]string{"Accept": "application/msword"}},
Expand All @@ -158,7 +158,7 @@ func TestHandlerResponse(t *testing.T) {
Body: "Unsupported Media Type\n",
Header: map[string]string{"Content-Type": "text/plain; charset=utf-8"},
},
handler: don.H(func(ctx context.Context, req don.Empty) (any, error) {
handler: don.H(func(ctx context.Context, req any) (any, error) {
return nil, nil
}),
request: request{
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestHandlerResponse(t *testing.T) {
Body: "test\n",
Header: map[string]string{"Content-Type": "text/plain; charset=utf-8"},
},
handler: don.H(func(ctx context.Context, req don.Empty) (any, error) {
handler: don.H(func(ctx context.Context, req any) (any, error) {
return nil, errors.New("test")
}),
},
Expand Down
8 changes: 0 additions & 8 deletions nilcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ func makeNilCheck(zero any) func(v any) bool {
return func(v any) bool { return v == nil }
}

// Return true for don.Empty.
if _, ok := zero.(Empty); ok {
return func(v any) bool { return true }
}
if _, ok := zero.(*Empty); ok {
return func(v any) bool { return true }
}

switch reflect.TypeOf(zero).Kind() {
case reflect.String, reflect.Ptr:
// Return true for empty strings and nil pointer.
Expand Down
10 changes: 2 additions & 8 deletions nilcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func TestNilCheck(t *testing.T) {
},
{
message: "nil struct",
typ: (*don.Empty)(nil),
data: (*don.Empty)(nil),
typ: (*struct{})(nil),
data: (*struct{})(nil),
expected: true,
},
{
Expand All @@ -37,12 +37,6 @@ func TestNilCheck(t *testing.T) {
data: struct{}{},
expected: false,
},
{
message: "don.Empty",
typ: don.Empty{},
data: don.Empty{},
expected: true,
},
{
message: "nil map",
typ: (map[string]string)(nil),
Expand Down

0 comments on commit 72848e8

Please sign in to comment.