form-query-headers-params-decoder

command
v12.2.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2023 License: BSD-3-Clause Imports: 4 Imported by: 0

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL