2021-04-13 07:33:04 +02:00
# httpin
2021-04-22 12:45:15 +02:00
[![codecov ](https://codecov.io/gh/ggicci/httpin/branch/main/graph/badge.svg?token=RT61L9ngHj )](https://codecov.io/gh/ggicci/httpin)
2021-05-08 10:15:26 +02:00
HTTP Input for Go - Decode an HTTP request into a custom struct
2021-05-08 10:34:38 +02:00
**Define the struct for your input and then fetch your data!**
2021-04-29 05:55:02 +02:00
2021-05-08 08:07:09 +02:00
## Quick View
2021-04-29 05:55:02 +02:00
2021-05-08 08:07:09 +02:00
< table >
< tr >
2021-05-08 10:15:26 +02:00
< th > BEFORE (use net/http)< / th >
< th > AFTER (use httpin)< / th >
2021-05-08 08:07:09 +02:00
< / tr >
< tr >
< td >
2021-04-29 05:55:02 +02:00
2021-05-08 08:07:09 +02:00
```go
func ListUsers(rw http.ResponseWriter, r *http.Request) {
page, err := strconv.ParseInt(r.FormValue("page"), 10, 64)
if err != nil {
// ... invalid page
return
}
perPage, err := strconv.ParseInt(r.FormValue("per_page"), 10, 64)
if err != nil {
// ... invalid per_page
return
}
isVip, _ := strconv.ParseBool(r.FormValue("is_vip"))
// do sth.
}
```
2021-04-29 05:55:02 +02:00
2021-05-08 08:07:09 +02:00
< / td >
< td >
```go
type ListUsersInput struct {
Page int `in:"form=page"`
PerPage int `in:"form=per_page"`
IsVip bool `in:"form=is_vip"`
}
func ListUsers(rw http.ResponseWriter, r *http.Request) {
2021-05-08 09:27:37 +02:00
interfaceInput, err := httpin.New(ListUsersInput{}).Decode(r)
2021-05-08 08:07:09 +02:00
if err != nil {
// err can be *httpin.InvalidField
return
}
input := interfaceInput.(*ListUsersInput)
// do sth.
}
```
< / td >
< / tr >
< / table >
## Features
2021-05-08 10:33:23 +02:00
- [x] Builtin directive `form` to decode a field from HTTP query, i.e. `http.Request.Form`
- [x] Builtin directive `header` to decode a field from HTTP headers, e.g. `http.Request.Header`
- [x] Builtin decoders used by `form` and `header` directives for basic types, e.g. `bool` , `int` , `int64` , `float32` , `time.Time` , ... [full list ](./decoders.go )
- [x] Decode a field by inspecting a set of keys from the same source
- [x] Decode a field from multiple sources, e.g. both query and headers
- [ ] Register or replace decoders for both builtin basic types and custom types
2021-05-08 08:07:09 +02:00
- [x] Define input struct with embedded struct fields
2021-05-08 10:33:23 +02:00
- [x] Builtin directive `required` to tag a field as **required**
2021-05-08 08:07:09 +02:00
- [ ] Builtin encoders for basic types
2021-05-08 10:33:23 +02:00
- [ ] Register or replace encoders for both builtin basic types and custom types
- [x] Register custom directive executors to extend the field resolving abilities, see directive [required ](./required.go ) as an example and think about implementing your own directives like `trim` , `to_lowercase` , `base58_to_int` , etc.
2021-05-08 08:07:09 +02:00
## Sample User Defined Input Structs
2021-04-29 05:55:02 +02:00
```go
type Authorization struct {
2021-05-08 08:07:09 +02:00
// Decode from multiple sources, the former with higher priority
2021-05-08 10:42:32 +02:00
Token string `in:"form=access_token;header=x-api-token;required"`
2021-04-29 05:55:02 +02:00
}
type Pagination struct {
2021-05-08 08:07:09 +02:00
Page int `in:"form=page"`
// Decode from multiple keys in the same source, the former with higher priority
2021-05-06 10:06:55 +02:00
PerPage int `in:"form=per_page,page_size"`
2021-04-29 05:55:02 +02:00
}
2021-05-08 08:07:09 +02:00
type ListUsersInput struct {
2021-05-06 10:06:55 +02:00
Gender string `in:"form=gender"`
AgeRange []int `in:"form=age_range"`
IsMember bool `in:"form=is_member"`
2021-04-29 05:55:02 +02:00
2021-05-08 08:07:09 +02:00
Pagination // Embedded field works
Authorization // Embedded field works
2021-04-29 05:55:02 +02:00
}
```
2021-05-08 16:46:16 +02:00
## Advanced
2021-05-08 16:48:43 +02:00
### Use middleware handlers to reduce much more trivial code
2021-04-29 05:55:02 +02:00
2021-05-08 08:11:26 +02:00
First, set up the middleware for your handlers. We recommend using [alice ](https://github.com/justinas/alice ) to chain your HTTP middleware functions.
2021-04-29 05:55:02 +02:00
```go
2021-05-08 08:07:09 +02:00
func init() {
2021-05-08 12:18:57 +02:00
http.Handle("/users", alice.New(
2021-05-08 08:07:09 +02:00
httpin.NewInput(ListUsersInput{}),
2021-05-08 12:18:57 +02:00
).ThenFunc(ListUsers))
2021-05-08 08:07:09 +02:00
}
2021-04-29 05:55:02 +02:00
```
2021-05-08 08:07:09 +02:00
Second, fetch your input with **only one line** of code.
2021-04-29 05:55:02 +02:00
```go
2021-05-08 08:07:09 +02:00
func ListUsers(rw http.ResponseWriter, r *http.Request) {
input := r.Context().Value(httpin.Input).(*UserQuery)
// do sth.
2021-04-29 05:55:02 +02:00
}
```
2021-05-08 16:46:16 +02:00
### Extend `httpin` by adding custom directives
Know the concept of a `Directive` :
```go
type Authorization struct {
Token string `in:"form=access_token,token;header=x-api-token;required"`
^---------------------^ ^----------------^ ^------^
2021-05-08 16:48:43 +02:00
d1 d2 d3
2021-05-08 16:46:16 +02:00
}
```
There are three directives above:
2021-05-08 16:48:43 +02:00
- d1: `form=access_token,token`
- d2: `header=x-api-token`
- d3: `required`
2021-05-08 16:46:16 +02:00
2021-05-08 16:52:58 +02:00
A directive consists of two parts separated by an equal sign (`=`). The left part is the name of an executor who was designed to run this directive. The right part is a list of arguments separated by commas (`,`) which will be passed to the corresponding executor at runtime.
2021-05-08 16:46:16 +02:00
2021-05-08 16:52:58 +02:00
For instance, `form=access_token,token` , here `form` is the name of the executor, and `access_token,token` is the argument list which will be parsed as `[]string{"access_token", "token"}` .
2021-05-08 16:46:16 +02:00
2021-05-08 16:52:58 +02:00
There are several builtin directive executors, e.g. `form` , `header` , `required` , ... [full list ](./directives.go ). And you can define your own by:
2021-05-08 16:46:16 +02:00
2021-05-08 16:52:58 +02:00
First, create a **directive executor** by implementing the `httpin.DirectiveExecutor` interface:
2021-05-08 16:46:16 +02:00
```go
func toLower(ctx *DirectiveContext) error {
if ctx.ValueType.Kind() != reflect.String {
return errors.New("not a string")
}
currentValue := ctx.Value.Elem().String()
newValue := strings.ToLower(currentValue)
ctx.Value.Elem().SetString(newValue)
return nil
}
2021-05-08 16:52:58 +02:00
// Adapt toLower to httpin.DirectiveExecutor.
var MyLowercaseDirectiveExecutor = httpin.DirectiveExecutorFunc(toLower)
2021-05-08 16:46:16 +02:00
```
Seconds, register it to `httpin` :
```go
2021-05-08 16:52:58 +02:00
httpin.RegisterDirectiveExecutor("to_lowercase", MyLowercaseDirectiveExecutor)
2021-05-08 16:46:16 +02:00
```
Finally, you can use your own directives in the struct tags:
```go
type Authorization struct {
Token string `in:"form=token;required;to_lowercase"`
}
```
2021-05-08 16:52:58 +02:00
The directives will run in the order as they defined in the struct tag.