2020-02-03 05:00:36 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-08-01 15:01:03 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module urllib
|
|
|
|
|
2019-10-10 19:04:11 +02:00
|
|
|
struct Value {
|
2019-12-13 18:09:11 +01:00
|
|
|
pub mut:
|
2019-08-01 15:01:03 +02:00
|
|
|
data []string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Values {
|
2019-12-13 18:09:11 +01:00
|
|
|
pub mut:
|
2019-08-01 15:01:03 +02:00
|
|
|
data map[string]Value
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
2019-08-02 23:28:55 +02:00
|
|
|
// new_values returns a new Values struct for creating
|
2019-11-14 07:53:05 +01:00
|
|
|
// urlencoded query string parameters. it can also be to
|
2019-08-02 23:28:55 +02:00
|
|
|
// post form data with application/x-www-form-urlencoded.
|
|
|
|
// values.encode() will return the encoded data
|
|
|
|
pub fn new_values() Values {
|
2019-08-01 15:01:03 +02:00
|
|
|
return Values{
|
2019-08-17 01:55:11 +02:00
|
|
|
data: map[string]Value
|
2019-08-01 15:01:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 23:28:55 +02:00
|
|
|
// Currently you will need to use all()[key].data
|
|
|
|
// once map[string][]string is implemented
|
|
|
|
// this will be fixed
|
|
|
|
pub fn (v &Value) all() []string {
|
2019-08-01 15:01:03 +02:00
|
|
|
return v.data
|
|
|
|
}
|
|
|
|
|
2019-08-02 23:28:55 +02:00
|
|
|
// get gets the first value associated with the given key.
|
|
|
|
// If there are no values associated with the key, get returns
|
|
|
|
// a empty string.
|
2019-10-10 19:04:11 +02:00
|
|
|
pub fn (v &Values) get(key string) string {
|
2019-08-01 15:01:03 +02:00
|
|
|
if v.data.size == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
vs := v.data[key]
|
|
|
|
if vs.data.len == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return vs.data[0]
|
|
|
|
}
|
|
|
|
|
2019-08-02 23:28:55 +02:00
|
|
|
// get_all gets the all the values associated with the given key.
|
|
|
|
// If there are no values associated with the key, get returns
|
|
|
|
// a empty []string.
|
2019-10-10 19:04:11 +02:00
|
|
|
pub fn (v &Values) get_all(key string) []string {
|
2019-08-02 23:28:55 +02:00
|
|
|
if v.data.size == 0 {
|
2019-11-14 07:53:05 +01:00
|
|
|
return []
|
2019-08-02 23:28:55 +02:00
|
|
|
}
|
|
|
|
vs := v.data[key]
|
|
|
|
if vs.data.len == 0 {
|
2019-11-14 07:53:05 +01:00
|
|
|
return []
|
2019-08-02 23:28:55 +02:00
|
|
|
}
|
|
|
|
return vs.data
|
|
|
|
}
|
|
|
|
|
|
|
|
// set sets the key to value. It replaces any existing
|
2019-08-01 15:01:03 +02:00
|
|
|
// values.
|
2019-08-02 23:28:55 +02:00
|
|
|
pub fn (v mut Values) set(key, value string) {
|
|
|
|
mut a := v.data[key]
|
|
|
|
a.data = [value]
|
|
|
|
v.data[key] = a
|
2019-08-01 15:01:03 +02:00
|
|
|
v.size = v.data.size
|
|
|
|
}
|
|
|
|
|
2019-08-02 23:28:55 +02:00
|
|
|
// add adds the value to key. It appends to any existing
|
2019-08-01 15:01:03 +02:00
|
|
|
// values associated with key.
|
2019-08-02 23:28:55 +02:00
|
|
|
pub fn (v mut Values) add(key, value string) {
|
2019-08-01 15:01:03 +02:00
|
|
|
mut a := v.data[key]
|
2019-08-02 23:28:55 +02:00
|
|
|
if a.data.len == 0 {
|
2019-11-14 07:53:05 +01:00
|
|
|
a.data = []
|
2019-08-02 23:28:55 +02:00
|
|
|
}
|
2019-08-01 15:01:03 +02:00
|
|
|
a.data << value
|
2019-08-02 23:28:55 +02:00
|
|
|
v.data[key] = a
|
|
|
|
v.size = v.data.size
|
2019-08-01 15:01:03 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 23:28:55 +02:00
|
|
|
// del deletes the values associated with key.
|
|
|
|
pub fn (v mut Values) del(key string) {
|
2019-08-01 15:01:03 +02:00
|
|
|
v.data.delete(key)
|
|
|
|
v.size = v.data.size
|
|
|
|
}
|
2019-12-21 23:41:42 +01:00
|
|
|
|