encoding/query
Reading and building the query string of a web address, such as q=hello&page=2.
import encoding.query
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| Name | Type | Description |
|---|---|---|
space_as_plus | bool = true | Write a space as +; true by default. When false, %20 is written. |
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)
endQueryValues
struct QueryValues| Name | Type | Description |
|---|---|---|
items | Vector | The values, in order. |
The example for this item is shown with the ones above: encoding-query-Query.salam
QueryMap
struct QueryMap| Name | Type | Description |
|---|---|---|
data | HashMap | The values of each key. |
order | Vector | The 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(): QueryMapThe example for this item is shown with the ones above: encoding-query-Query.salam
DecodeWith
func DecodeWith(qs: str, opts: QueryOptions): QueryMapThe example for this item is shown with the ones above: encoding-query-Query.salam
Decode
func Decode(qs: str): QueryMap+ are undone.The example for this item is shown with the ones above: encoding-query-Query.salam
EncodeWith
func EncodeWith(values: QueryMap, opts: QueryOptions): strThe example for this item is shown with the ones above: encoding-query-Query.salam
Encode
func Encode(values: QueryMap): strThe example for this item is shown with the ones above: encoding-query-Query.salam
GetAll
func GetAll(qs: str, key: str): Vector<str>The example for this item is shown with the ones above: encoding-query-Query.salam
Get
func Get(qs: str, key: str): strThe example for this item is shown with the ones above: encoding-query-Query.salam
Has
func Has(qs: str, key: str): boolThe example for this item is shown with the ones above: encoding-query-Query.salam
Size
func Size(m: QueryMap): intThe example for this item is shown with the ones above: encoding-query-Query.salam
Add
func Add(values &: QueryMap, key: str, val: str)The example for this item is shown with the ones above: encoding-query-Query.salam
Set
func Set(values &: QueryMap, key: str, val: str)The example for this item is shown with the ones above: encoding-query-Query.salam
Keys
func Keys(values: QueryMap): Vector<str>The example for this item is shown with the ones above: encoding-query-Query.salam
Remove
func Remove(values &: QueryMap, key: str): boolThe example for this item is shown with the ones above: encoding-query-Query.salam
Retain
func Retain(values &: QueryMap, keep: Vector<str>)The example for this item is shown with the ones above: encoding-query-Query.salam