Documentation ¶
Overview ¶
Package qstring provides an an easy way to marshal and unmarshal query string data to and from structs representations.
This library was designed with consistency in mind, to this end the provided Marshal and Unmarshal interfaces should seem familiar to those who have used packages such as "encoding/json".
Additionally this library includes support for converting query parameters to and from time.Time types as well as nested structs and pointer values.
Index ¶
Examples ¶
Constants ¶
const ( // Tag indicates the name of the struct tag to extract from provided structs // when marshalling or unmarshalling Tag = "qstring" )
Variables ¶
This section is empty.
Functions ¶
func MarshalString ¶
Marshal marshals the provided struct into a raw query string and returns a conditional error
Example ¶
// Query is the http request query struct. type Query struct { Names []string Limit int Page int } query := &Query{ Names: []string{"foo", "bar"}, Limit: 50, Page: 1, } q, _ := MarshalString(query) os.Stdout.Write([]byte(q))
Output: limit=50&names=foo&names=bar&page=1
func Unmarshal ¶
Unmarshal unmarshalls the provided url.Values (query string) into the interface provided
Example ¶
// Query is the http request query struct. type Query struct { Names []string Limit int Page int } query := &Query{} qValues, _ := url.ParseQuery("names=foo&names=bar&limit=50&page=1") err := Unmarshal(qValues, query) if err != nil { panic("Unable to Parse Query String") } os.Stdout.Write([]byte(fmt.Sprintf("%+v", query)))
Output: &{Names:[foo bar] Limit:50 Page:1}
Example (Complex) ¶
// PagingParams represents common pagination information for query strings type PagingParams struct { Page int `qstring:"page"` Limit int `qstring:"limit"` } // Query is the http request query struct. type Query struct { Names []string IDs []int PageInfo *PagingParams Created time.Time } query := &Query{} qValues, _ := url.ParseQuery("names=foo&names=bar&limit=50&page=1&ids=1&ids=2&created=2006-01-02T15:04:05Z") err := Unmarshal(qValues, query) if err != nil { panic("Unable to Parse Query String") }
Output:
Types ¶
type ComparativeString ¶
func (*ComparativeString) Parse ¶
func (c *ComparativeString) Parse(query string) error
Parse is used to parse a query string into a ComparativeString instance
func (ComparativeString) String ¶
func (c ComparativeString) String() string
String returns this ComparativeString instance in the form of the query parameter that it came in on
type ComparativeTime ¶
ComparativeTime is a field that can be used for specifying a query parameter which includes a conditional operator and a timestamp
Example ¶
type DateQuery struct { Created ComparativeTime Modified ComparativeTime } var query DateQuery qValues, _ := url.ParseQuery("created=>=2006-01-02T15:04:05Z&modified=<=2016-01-01T15:04Z") err := Unmarshal(qValues, &query) if err != nil { panic("Unable to Parse Query String") }
Output:
func NewComparativeTime ¶
func NewComparativeTime() *ComparativeTime
NewComparativeTime returns a new ComparativeTime instance with a default operator of "="
func (*ComparativeTime) Parse ¶
func (c *ComparativeTime) Parse(query string) error
Parse is used to parse a query string into a ComparativeTime instance
func (ComparativeTime) String ¶
func (c ComparativeTime) String() string
String returns this ComparativeTime instance in the form of the query parameter that it came in on
type InvalidMarshalError ¶
An InvalidMarshalError describes an invalid argument passed to Marshal or MarshalValue. (The argument to Marshal must be a non-nil pointer.)
func (InvalidMarshalError) Error ¶
func (e InvalidMarshalError) Error() string
type InvalidUnmarshalError ¶
An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. (The argument to Unmarshal must be a non-nil pointer.)
func (InvalidUnmarshalError) Error ¶
func (e InvalidUnmarshalError) Error() string
type Marshaller ¶
Marshaller defines the interface for performing custom marshalling of struct values into query strings
type Unmarshaller ¶
Unmarshaller defines the interface for performing custom unmarshalling of query strings into struct values