Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNilValueGiven = errors.New("given value is nil") ErrQueryParameterNameIsEmpty = errors.New("query parameter name is empty in a tag") ErrUnsupportedFieldType = errors.New("unsupported filed type has come") ErrUnsupportedUnixTimeUnit = errors.New("unsupported unix time unit has given") )
Functions ¶
func ConvertToQueryParams ¶
ConvertToQueryParams converts given structure to the query parameters according to the custom tags.
When a field of the structure has `taqc` tag, it converts a value of that field to query parameter. Currently, it supports the following field types: `string`, `int64`, `float64`, `bool`, `*string`, `*int64`, `*float64`, `*bool`, `[]string`, `[]int64`, `[]float64`, `time.Time`, `*time.Time`, and `[]time.Time`. If the bool field is `true`, the query parameter becomes `param_name=1`. Else, it omits the parameter. And when the pointer value is `nil`, it omits the parameter.
This library supports the `time.Time` fields. By default, it encodes that timestamp by `Time#Unix()`. If you want to encode it by another unix time format, you can use `unixTimeUnit` custom tag value. For example:
type Query struct { Foo time.Time `taqc:"foo, unixTimeUnit=millisec"` }
in the case of the above example, it encodes the timestamp by `Time#UnixMilli()`.
Currently `unixTmeUnit` supports the following values:
- `sec` - `millisec` - `microsec` - `nanosec`
It also supports encoding with arbitrary time layout by `timeLayout` custom tag value. e.g.
type Query struct { Foo time.Time `taqc:"foo, timeLayout=2006-01-02T15:04:05Z07:00"` // RFC3339 layout }
then, it encodes the timestamp by `Time#Format()` with given layout.
NOTE: `timeLayout` takes priority over `unixTimeUnit`. This means it uses `timeLayout` option even if you put them together.
Example ¶
type Query struct { Foo string `taqc:"foo"` Bar *string `taqc:"bar"` Buz int64 `taqc:"buz"` Qux []float64 `taqc:"qux"` FooBar bool `taqc:"foobar"` Falsy bool `taqc:"falsy"` ShouldBeIgnored string } queryParams, err := ConvertToQueryParams(&Query{ Foo: "string_value", Bar: nil, // <= should be ignored Buz: 123, Qux: []float64{123.456, 234.567}, FooBar: true, // <= be "foobar=1" Falsy: false, // <= should be ignored ShouldBeIgnored: "should be ignored", }) if err != nil { panic(err) } fmt.Printf("%s\n", queryParams.Encode())
Output: buz=123&foo=string_value&foobar=1&qux=123.456000&qux=234.567000
Types ¶
This section is empty.