Documentation ¶
Overview ¶
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){ ...}.