Skip to content

Commit b0ccd57

Browse files
committed
add a new _proposals folder and a silly idea
1 parent 60bf26e commit b0ccd57

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

.deepsource.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ name = "go"
1313
enabled = true
1414

1515
[analyzers.meta]
16-
import_paths = ["github.com/kataras/iris"]
16+
import_paths = ["github.com/kataras/iris/v12"]

_proposals/generic_handler.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Iris Handler with Generics support
2+
3+
```go
4+
package x
5+
6+
import (
7+
"github.com/kataras/iris/v12/context"
8+
"github.com/kataras/iris/v12/x/errors"
9+
)
10+
11+
var ErrorHandler context.ErrorHandler = context.ErrorHandlerFunc(errors.InvalidArgument.Err)
12+
13+
type (
14+
Handler[Request any | *context.Context, Response any] func(Request) (Response, error)
15+
HandlerWithCtx[Request any, Response any] func(*context.Context, Request) (Response, error)
16+
)
17+
18+
func HandleContext[Request any, Response any](handler HandlerWithCtx[Request, Response]) context.Handler {
19+
return func(ctx *context.Context) {
20+
var req Request
21+
if err := ctx.ReadJSON(&req); err != nil {
22+
errors.InvalidArgument.Details(ctx, "unable to parse body", err.Error())
23+
return
24+
}
25+
26+
resp, err := handler(ctx, req)
27+
if err != nil {
28+
ErrorHandler.HandleContextError(ctx, err)
29+
return
30+
}
31+
32+
if _, err = ctx.JSON(resp); err != nil {
33+
errors.Internal.Details(ctx, "unable to parse response", err.Error())
34+
return
35+
}
36+
}
37+
}
38+
39+
func Handle[Request any, Response any](handler Handler[Request, Response]) context.Handler {
40+
return HandleContext(func(_ *context.Context, req Request) (Response, error) { return handler(req) })
41+
}
42+
43+
```
44+
45+
Usage Code:
46+
47+
```go
48+
import (
49+
"github.com/kataras/iris/v12"
50+
"github.com/kataras/iris/v12/x"
51+
)
52+
53+
type (
54+
Req struct {
55+
Email string `json:"email"`
56+
}
57+
58+
Res struct {
59+
Verified bool `json:"verified"`
60+
}
61+
)
62+
63+
func main() {
64+
app := iris.New()
65+
app.Post("/", your_package.Handle(handler))
66+
app.Listen(":8080")
67+
}
68+
69+
func handler(req Req) (Res, error){
70+
verified := req.Email == "[email protected]"
71+
return Res{Verified: verified}, nil
72+
}
73+
```
74+
75+
Example response:
76+
77+
```json
78+
{
79+
"verified": true
80+
}
81+
```

context/context.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -3793,10 +3793,18 @@ type JSON struct {
37933793
ErrorHandler ErrorHandler
37943794
}
37953795

3796-
// ErrorHandler describes a context error handler. As for today this is only used
3797-
// to globally or per-party or per-route handle JSON writes error.
3798-
type ErrorHandler interface {
3799-
HandleContextError(ctx *Context, err error)
3796+
type (
3797+
// ErrorHandler describes a context error handler. As for today this is only used
3798+
// to globally or per-party or per-route handle JSON writes error.
3799+
ErrorHandler interface {
3800+
HandleContextError(ctx *Context, err error)
3801+
}
3802+
// ErrorHandlerFunc a function shortcut for ErrorHandler interface.
3803+
ErrorHandlerFunc func(ctx *Context, err error)
3804+
)
3805+
3806+
func (h ErrorHandlerFunc) HandleContextError(ctx *Context, err error) {
3807+
h(ctx, err)
38003808
}
38013809

38023810
// IsDefault reports whether this JSON options structure holds the default values.

0 commit comments

Comments
 (0)