urlqp: fknsrs.biz/p/urlqp Index | Examples | Files

package urlqp

import "fknsrs.biz/p/urlqp"

Index

Examples

Package Files

urlqp.go

type Pair Uses

type Pair [2]string

Pair represents a key/value pair.

type Values Uses

type Values []Pair

Values represents all the key/value pairs in a set of query parameters.

func Parse Uses

func Parse(s string) (Values, error)

Parse tries to parse the given string into a set of values. If it fails, it will return an error.

We want to be able to process some query parameters in order. This API treats the order of query parameters significantly, using them as a set of operations.

Code:

// Add one, multiply by two, add three.
str := `add=1&multiply=2&add=3`

// We should get `5` as the result.
var expected int64 = 5

// Here we use the default `net/url` query parser. It parses the parameters
// into a `map[string][]string`, which is perfectly fine for most uses, but
// not ours.
m, _ := url.ParseQuery(str)
{
    var i int64

    // The order that we range over this map is intentionally undefined. That
    // means we'll end up with one of two results:
    //
    // > add 1, add 3, multiply 2 = 8
    // or
    // > multiply 2, add 1, add 3 = 4
    //
    // This is bad for this particular use case.
    for k, l := range m {
        for _, v := range l {
            n, _ := strconv.ParseInt(v, 10, 32)

            switch k {
            case "add":
                i += n
            case "multiply":
                i *= n
            }
        }
    }

    // This is going to print `false`.
    fmt.Printf("equal: %v\n", i == expected)
}

// This is using the `urlqp` parser. It preserves the order of the
// parameters as it just returns a list of key/value pairs.
q, _ := Parse(str)
{
    var i int64

    // Iterating through this list happens in a defined order, which is the
    // order the parameters were specified in the string above.
    for _, p := range q {
        k, v := p[0], p[1]

        n, _ := strconv.ParseInt(v, 10, 32)

        switch k {
        case "add":
            i += n
        case "multiply":
            i *= n
        }
    }

    // This will print `true`
    fmt.Printf("equal: %v\n", i == expected)
}

Output:

equal: false
equal: true

func (Values) All Uses

func (v Values) All(k string) []string

All retrieves the values of all the query parameters named "k".

func (Values) Filter Uses

func (v Values) Filter(keys ...string) Values

Filter returns a new set of values, limited to only the specified keys.

func (Values) Get Uses

func (v Values) Get(k string) string

Get retrieves the value of the first query parameter named "k", or returns an empty string.

func (Values) String Uses

func (v Values) String() string

String serialises this set of query parameters into a format suitable for use in a URL.

Package urlqp imports 2 packages (graph). Updated 2016-07-16. Refresh now. Tools for package owners. This is an inactive package (no imports and no commits in at least two years).