Standard library · Encoding and data

encoding/query

Reading and building the query string of a web address, such as q=hello&page=2.

import encoding.query
On this page
  1. Types
  2. Functions

The query string is the part after the ? in a web address. One key may appear several times, as in tag=a&tag=b, so a QueryMap keeps a list of values for each key, and keeps the keys in order.

Functions such as Get and Has work on the string directly, which suits reading something once.

Types

QueryOptions

struct QueryOptions
The encoding options.
NameTypeDescription
space_as_plusbool = trueWrite a space as +; true by default. When false, %20 is written.
encoding-query-Query.salam
import encoding.query

func main:
    text := "q=hello&page=2&tag=a&tag=b&empty="
    println query.Get(text, "q"), query.Get(text, "page"), query.Has(text, "empty"), query.Has(text, "x")
    println query.GetAll(text, "tag")
    mut m := query.Decode(text)
    println query.Size(m), query.Keys(m)
    query.Add(m, "tag", "c")
    query.Set(m, "page", "3")
    println query.Remove(m, "empty")
    println query.Encode(m)
    mut keep := Vector {} as Vector<str>
    keep.push("q")
    query.Retain(m, keep)
    println query.Encode(m)
    opts := query.QueryOptions {}
    println query.EncodeWith(query.DecodeWith("b=2&a=1", opts), opts)
    empty := query.NewQueryMap()
    println query.Size(empty)
end
Outputhello 2 true false ["a", "b"] 4 ["q", "page", "tag", "empty"] true q=hello&page=3&tag=a&tag=b&tag=c q=hello b=2&a=1 0

QueryValues

struct QueryValues
The values of one key.
NameTypeDescription
itemsVectorThe values, in order.

The example for this item is shown with the ones above: encoding-query-Query.salam

QueryMap

struct QueryMap
A map from key to values that keeps the key order.
NameTypeDescription
dataHashMapThe values of each key.
orderVectorThe keys in the order they first appeared.

The example for this item is shown with the ones above: encoding-query-Query.salam

Functions

NewQueryMap

func NewQueryMap(): QueryMap
An empty map.

The example for this item is shown with the ones above: encoding-query-Query.salam

DecodeWith

func DecodeWith(qs: str, opts: QueryOptions): QueryMap
Reads a query string with the given options.

The example for this item is shown with the ones above: encoding-query-Query.salam

Decode

func Decode(qs: str): QueryMap
Reads a query string; percent encoding and + are undone.

The example for this item is shown with the ones above: encoding-query-Query.salam

EncodeWith

func EncodeWith(values: QueryMap, opts: QueryOptions): str
Turns a map into a string with the given options.

The example for this item is shown with the ones above: encoding-query-Query.salam

Encode

func Encode(values: QueryMap): str
Turns a map into a query string.

The example for this item is shown with the ones above: encoding-query-Query.salam

GetAll

func GetAll(qs: str, key: str): Vector<str>
Every value of a key in the string.

The example for this item is shown with the ones above: encoding-query-Query.salam

Get

func Get(qs: str, key: str): str
The first value of a key in the string, or an empty string when it is absent.

The example for this item is shown with the ones above: encoding-query-Query.salam

Has

func Has(qs: str, key: str): bool
True when the key is in the string, even with an empty value.

The example for this item is shown with the ones above: encoding-query-Query.salam

Size

func Size(m: QueryMap): int
The number of keys.

The example for this item is shown with the ones above: encoding-query-Query.salam

Add

func Add(values &: QueryMap, key: str, val: str)
Adds another value to a key, keeping the earlier ones.

The example for this item is shown with the ones above: encoding-query-Query.salam

Set

func Set(values &: QueryMap, key: str, val: str)
Replaces every value of a key with one value.

The example for this item is shown with the ones above: encoding-query-Query.salam

Keys

func Keys(values: QueryMap): Vector<str>
The keys, in order.

The example for this item is shown with the ones above: encoding-query-Query.salam

Remove

func Remove(values &: QueryMap, key: str): bool
Removes a key and its values and says whether it was there.

The example for this item is shown with the ones above: encoding-query-Query.salam

Retain

func Retain(values &: QueryMap, keep: Vector<str>)
Keeps only the keys you name.

The example for this item is shown with the ones above: encoding-query-Query.salam