Skip to content

Commit 8377871

Browse files
committed
add context.Protobuf, MsgPack, ReadProtobuf, ReadMsgPack methods
Former-commit-id: 39d547ecfb1516505a1eb76a12a1f6e9e4111962
1 parent ee4213f commit 8377871

File tree

16 files changed

+215
-40
lines changed

16 files changed

+215
-40
lines changed

HISTORY.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ Prior to this version the `iris.Context` was the only one dependency that has be
150150
| `float, float32, float64`, | |
151151
| `bool`, | |
152152
| `slice` | [Path Parameter](https://github.com/kataras/iris/wiki/Routing-path-parameter-types) |
153-
| Struct | [Request Body](https://github.com/kataras/iris/tree/master/_examples/http_request) of `JSON`, `XML`, `YAML`, `Form`, `URL Query` |
153+
| Struct | [Request Body](https://github.com/kataras/iris/tree/master/_examples/http_request) of `JSON`, `XML`, `YAML`, `Form`, `URL Query`, `Protobuf`, `MsgPack` |
154154

155155
Here is a preview of what the new Hero handlers look like:
156156

@@ -176,6 +176,10 @@ Other Improvements:
176176

177177
New Context Methods:
178178

179+
- `context.Protobuf(proto.Message)` sends protobuf to the client
180+
- `context.MsgPack(interface{})` sends msgpack format data to the client
181+
- `context.ReadProtobuf(ptr)` binds request body to a proto message
182+
- `context.ReadMsgPack(ptr)` binds request body of a msgpack format to a struct
179183
- `context.Defer(Handler)` works like `Party.Done` but for the request life-cycle.
180184
- `context.ReflectValue() []reflect.Value` stores and returns the `[]reflect.ValueOf(context)`
181185
- `context.Controller() reflect.Value` returns the current MVC Controller value (when fired from inside a controller's method).

_examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ You can serve [quicktemplate](https://github.com/valyala/quicktemplate) and [her
248248
- [Read JSON](http_request/read-json/main.go)
249249
* [Struct Validation](http_request/read-json-struct-validation/main.go)
250250
- [Read XML](http_request/read-xml/main.go)
251+
- [Read MsgPack](http_request/read-msgpack/main.go) **NEW**
251252
- [Read YAML](http_request/read-yaml/main.go)
252253
- [Read Form](http_request/read-form/main.go)
253254
- [Read Query](http_request/read-query/main.go)

_examples/configuration/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func main() {
3535
DisableBodyConsumptionOnUnmarshal: false,
3636
DisableAutoFireStatusCode: false,
3737
TimeFormat: "Mon, 02 Jan 2006 15:04:05 GMT",
38-
Charset: "UTF-8",
38+
Charset: "utf-8",
3939
}))
4040
}
4141
```
@@ -60,10 +60,10 @@ func main() {
6060
// Prefix: "With", code editors will help you navigate through all
6161
// configuration options without even a glitch to the documentation.
6262

63-
app.Listen(":8080", iris.WithoutStartupLog, iris.WithCharset("UTF-8"))
63+
app.Listen(":8080", iris.WithoutStartupLog, iris.WithCharset("utf-8"))
6464

6565
// or before run:
66-
// app.Configure(iris.WithoutStartupLog, iris.WithCharset("UTF-8"))
66+
// app.Configure(iris.WithoutStartupLog, iris.WithCharset("utf-8"))
6767
// app.Listen(":8080")
6868
}
6969
```
@@ -76,7 +76,7 @@ EnablePathEscape = false
7676
FireMethodNotAllowed = true
7777
DisableBodyConsumptionOnUnmarshal = false
7878
TimeFormat = "Mon, 01 Jan 2006 15:04:05 GMT"
79-
Charset = "UTF-8"
79+
Charset = "utf-8"
8080

8181
[Other]
8282
MyServerName = "iris"

_examples/configuration/from-configuration-structure/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func main() {
2121
DisableBodyConsumptionOnUnmarshal: false,
2222
DisableAutoFireStatusCode: false,
2323
TimeFormat: "Mon, 02 Jan 2006 15:04:05 GMT",
24-
Charset: "UTF-8",
24+
Charset: "utf-8",
2525
}))
2626

2727
// or before Run:

_examples/configuration/from-toml-file/configs/iris.tml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ EnablePathEscape = false
33
FireMethodNotAllowed = true
44
DisableBodyConsumptionOnUnmarshal = false
55
TimeFormat = "Mon, 01 Jan 2006 15:04:05 GMT"
6-
Charset = "UTF-8"
6+
Charset = "utf-8"
77

88
[Other]
99
MyServerName = "iris"

_examples/configuration/functional/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ func main() {
1515
// Prefix: "With", code editors will help you navigate through all
1616
// configuration options without even a glitch to the documentation.
1717

18-
app.Listen(":8080", iris.WithoutStartupLog, iris.WithCharset("UTF-8"))
18+
app.Listen(":8080", iris.WithoutStartupLog, iris.WithCharset("utf-8"))
1919

2020
// or before run:
21-
// app.Configure(iris.WithoutStartupLog, iris.WithCharset("UTF-8"))
21+
// app.Configure(iris.WithoutStartupLog, iris.WithCharset("utf-8"))
2222
// app.Listen(":8080")
2323
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import "github.com/kataras/iris/v12"
4+
5+
// User example struct to bind to.
6+
type User struct {
7+
Firstname string `msgpack:"firstname"`
8+
Lastname string `msgpack:"lastname"`
9+
City string `msgpack:"city"`
10+
Age int `msgpack:"age"`
11+
}
12+
13+
// readMsgPack reads a `User` from MsgPack post body.
14+
func readMsgPack(ctx iris.Context) {
15+
var u User
16+
err := ctx.ReadMsgPack(&u)
17+
if err != nil {
18+
ctx.StatusCode(iris.StatusBadRequest)
19+
ctx.WriteString(err.Error())
20+
return
21+
}
22+
23+
ctx.Writef("Received: %#+v\n", u)
24+
}
25+
26+
func main() {
27+
app := iris.New()
28+
app.Post("/", readMsgPack)
29+
30+
// POST: http://localhost:8080
31+
//
32+
// To run the example, use a tool like Postman:
33+
// 1. Body: Binary
34+
// 2. Select File, select the one from "_examples/http_responsewriter/write-rest" example.
35+
// The output should be:
36+
// Received: main.User{Firstname:"John", Lastname:"Doe", City:"Neither FBI knows!!!", Age:25}
37+
app.Listen(":8080")
38+
}

_examples/http_responsewriter/write-rest/main.go

+29-7
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
"github.com/kataras/iris/v12/context"
88
)
99

10-
// User bind struct
10+
// User example struct for json and msgpack.
1111
type User struct {
12-
Firstname string `json:"firstname"`
13-
Lastname string `json:"lastname"`
14-
City string `json:"city"`
15-
Age int `json:"age"`
12+
Firstname string `json:"firstname" msgpack:"firstname"`
13+
Lastname string `json:"lastname" msgpack:"lastname"`
14+
City string `json:"city" msgpack:"city"`
15+
Age int `json:"age" msgpack:"age"`
1616
}
1717

1818
// ExampleXML just a test struct to view represents xml content-type
@@ -22,6 +22,12 @@ type ExampleXML struct {
2222
Two string `xml:"two,attr"`
2323
}
2424

25+
// ExampleYAML just a test struct to write yaml to the client.
26+
type ExampleYAML struct {
27+
Name string `yaml:"name"`
28+
ServerAddr string `yaml:"ServerAddr"`
29+
}
30+
2531
func main() {
2632
app := iris.New()
2733

@@ -36,15 +42,15 @@ func main() {
3642

3743
// Write
3844
app.Get("/encode", func(ctx iris.Context) {
39-
peter := User{
45+
u := User{
4046
Firstname: "John",
4147
Lastname: "Doe",
4248
City: "Neither FBI knows!!!",
4349
Age: 25,
4450
}
4551

4652
// Manually setting a content type: ctx.ContentType("application/javascript")
47-
ctx.JSON(peter)
53+
ctx.JSON(u)
4854
})
4955

5056
// Other content types,
@@ -74,6 +80,21 @@ func main() {
7480
ctx.Markdown([]byte("# Hello Dynamic Markdown -- iris"))
7581
})
7682

83+
app.Get("/yaml", func(ctx iris.Context) {
84+
ctx.YAML(ExampleYAML{Name: "Iris", ServerAddr: "localhost:8080"})
85+
})
86+
87+
app.Get("/msgpack", func(ctx iris.Context) {
88+
u := User{
89+
Firstname: "John",
90+
Lastname: "Doe",
91+
City: "Neither FBI knows!!!",
92+
Age: 25,
93+
}
94+
95+
ctx.MsgPack(u)
96+
})
97+
7798
// http://localhost:8080/decode
7899
// http://localhost:8080/encode
79100
//
@@ -83,6 +104,7 @@ func main() {
83104
// http://localhost:8080/jsonp
84105
// http://localhost:8080/xml
85106
// http://localhost:8080/markdown
107+
// http://localhost:8080/msgpack
86108
//
87109
// `iris.WithOptimizations` is an optional configurator,
88110
// if passed to the `Run` then it will ensure that the application

_examples/overview/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func main() {
8383
})
8484

8585
// Listen for incoming HTTP/1.x & HTTP/2 clients on localhost port 8080.
86-
app.Listen(":8080", iris.WithCharset("UTF-8"))
86+
app.Listen(":8080", iris.WithCharset("utf-8"))
8787
}
8888

8989
func logThisMiddleware(ctx iris.Context) {

_examples/view/template_html_0/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func main() {
2424
app.Get("/", hi)
2525

2626
// http://localhost:8080
27-
app.Listen(":8080", iris.WithCharset("UTF-8")) // defaults to that but you can change it.
27+
app.Listen(":8080", iris.WithCharset("utf-8")) // defaults to that but you can change it.
2828
}
2929

3030
func hi(ctx iris.Context) {

configuration.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ type Configuration struct {
803803

804804
// Charset character encoding for various rendering
805805
// used for templates and the rest of the responses
806-
// Defaults to "UTF-8".
806+
// Defaults to "utf-8".
807807
Charset string `json:"charset,omitempty" yaml:"Charset" toml:"Charset"`
808808

809809
// PostMaxMemory sets the maximum post data size
@@ -1109,7 +1109,7 @@ func DefaultConfiguration() Configuration {
11091109
DisableBodyConsumptionOnUnmarshal: false,
11101110
DisableAutoFireStatusCode: false,
11111111
TimeFormat: "Mon, 02 Jan 2006 15:04:05 GMT",
1112-
Charset: "UTF-8",
1112+
Charset: "utf-8",
11131113

11141114
// PostMaxMemory is for post body max memory.
11151115
//

configuration_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ FireMethodNotAllowed: true
148148
EnableOptimizations: true
149149
DisableBodyConsumptionOnUnmarshal: true
150150
TimeFormat: "Mon, 02 Jan 2006 15:04:05 GMT"
151-
Charset: "UTF-8"
151+
Charset: "utf-8"
152152
153153
RemoteAddrHeaders:
154154
X-Real-Ip: true
@@ -192,7 +192,7 @@ Other:
192192
t.Fatalf("error on TestConfigurationYAML: Expected TimeFormat %s but got %s", expected, c.TimeFormat)
193193
}
194194

195-
if expected := "UTF-8"; c.Charset != expected {
195+
if expected := "utf-8"; c.Charset != expected {
196196
t.Fatalf("error on TestConfigurationYAML: Expected Charset %s but got %s", expected, c.Charset)
197197
}
198198

@@ -245,7 +245,7 @@ FireMethodNotAllowed = true
245245
EnableOptimizations = true
246246
DisableBodyConsumptionOnUnmarshal = true
247247
TimeFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
248-
Charset = "UTF-8"
248+
Charset = "utf-8"
249249
250250
[RemoteAddrHeaders]
251251
X-Real-Ip = true
@@ -291,7 +291,7 @@ Charset = "UTF-8"
291291
t.Fatalf("error on TestConfigurationTOML: Expected TimeFormat %s but got %s", expected, c.TimeFormat)
292292
}
293293

294-
if expected := "UTF-8"; c.Charset != expected {
294+
if expected := "utf-8"; c.Charset != expected {
295295
t.Fatalf("error on TestConfigurationTOML: Expected Charset %s but got %s", expected, c.Charset)
296296
}
297297

0 commit comments

Comments
 (0)