Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BindTypeError ¶
BindTypeError describes a query value that was not appropriate for a value of a specific Go type.
func (*BindTypeError) Error ¶
func (e *BindTypeError) Error() string
type DSN ¶
DSN a DSN represents a parsed DSN as same as url.URL.
func Parse ¶
Parse parses rawdsn into a URL structure.
Example ¶
package main import ( "log" "btc-project/library/conf/dsn" xtime "btc-project/library/time" ) // Config struct type Config struct { Network string `dsn:"network" validate:"required"` Host string `dsn:"host" validate:"required"` Username string `dsn:"username" validate:"required"` Password string `dsn:"password" validate:"required"` Timeout xtime.Duration `dsn:"query.timeout,1s"` Offset int `dsn:"query.offset" validate:"gte=0"` } func main() { cfg := &Config{} d, err := dsn.Parse("tcp://root:toor@172.12.12.23:2233?timeout=10s") if err != nil { log.Fatal(err) } _, err = d.Bind(cfg) if err != nil { log.Fatal(err) } log.Printf("%v", cfg) }
Output:
func (*DSN) Bind ¶
Bind dsn to specify struct and validate use use go-playground/validator format
The bind of each struct field can be customized by the format string stored under the 'dsn' key in the struct field's tag. The format string gives the name of the field, possibly followed by a comma-separated list of options. The name may be empty in order to specify options without overriding the default field name.
A two type data you can bind to struct built-in values, use below keys to bind built-in value
username password address network
the value in query string, use query.{name} to bind value in query string
As a special case, if the field tag is "-", the field is always omitted. NOTE: that a field with name "-" can still be generated using the tag "-,".
Examples of struct field tags and their meanings:
// Field bind username Field string `dsn:"username"` // Field is ignored by this package. Field string `dsn:"-"` // Field bind value from query Field string `dsn:"query.name"`
type InvalidBindError ¶
InvalidBindError describes an invalid argument passed to DecodeQuery. (The argument to DecodeQuery must be a non-nil pointer.)
func (*InvalidBindError) Error ¶
func (e *InvalidBindError) Error() string