forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (51 loc) · 1.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// package main contains an example on how to register a custom decoder
// for a custom type when using the `ReadQuery/ReadParams/ReadHeaders/ReadForm` methods.
//
// Let's take for example the mongo-driver/primite.ObjectID:
//
// ObjectID is type ObjectID [12]byte.
// You have to register a converter for that custom type.
// ReadJSON works because the ObjectID has a MarshalJSON method
// which the encoding/json pakcage uses as a converter.
// See here: https://godoc.org/go.mongodb.org/mongo-driver/bson/primitive#ObjectID.MarshalJSON.
//
// To register a converter import the github.com/iris-contrib/schema and call
// schema.Query.RegisterConverter(value interface{}, converterFunc Converter).
//
// The Converter is just a type of func(string) reflect.Value.
//
// There is another way, but requires introducing a custom type which will wrap the mongo's ObjectID
// and implements the encoding.TextUnmarshaler e.g.
// func(id *ID) UnmarshalText(text []byte) error){ ...}.
package main
import (
"reflect"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/iris-contrib/schema"
"github.com/kataras/iris/v12"
)
type MyType struct {
ID primitive.ObjectID `url:"id"`
}
func main() {
// Register on initialization.
schema.Query.RegisterConverter(primitive.ObjectID{}, func(value string) reflect.Value {
id, err := primitive.ObjectIDFromHex(value)
if err != nil {
return reflect.Value{}
}
return reflect.ValueOf(id)
})
app := iris.New()
app.Get("/", func(ctx iris.Context) {
var t MyType
err := ctx.ReadQuery(&t)
if err != nil && !iris.IsErrPath(err) {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
ctx.Writef("MyType.ID: %q", t.ID.Hex())
})
// http://localhost:8080?id=507f1f77bcf86cd799439011
app.Listen(":8080")
}