dsn

package
v0.0.0-...-3168f03 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2020 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BindTypeError

type BindTypeError struct {
	Value string
	Type  reflect.Type
}

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

type DSN struct {
	*url.URL
}

DSN a DSN represents a parsed DSN as same as url.URL.

func Parse

func Parse(rawdsn string) (*DSN, error)

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) Addresses

func (d *DSN) Addresses() []string

Addresses parse host split by ',' For Unix networks, return ['path']

func (*DSN) Bind

func (d *DSN) Bind(v interface{}) (url.Values, error)

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

type InvalidBindError struct {
	Type reflect.Type
}

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

Jump to

Keyboard shortcuts

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