mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
net.urllib: keep the query parameter order (#13405)
This commit is contained in:
@@ -3,14 +3,15 @@
|
||||
// that can be found in the LICENSE file.
|
||||
module urllib
|
||||
|
||||
struct Value {
|
||||
struct QueryValue {
|
||||
pub mut:
|
||||
data []string
|
||||
key string
|
||||
value string
|
||||
}
|
||||
|
||||
struct Values {
|
||||
pub mut:
|
||||
data map[string]Value
|
||||
data []QueryValue
|
||||
len int
|
||||
}
|
||||
|
||||
@@ -20,17 +21,10 @@ pub mut:
|
||||
// values.encode() will return the encoded data
|
||||
pub fn new_values() Values {
|
||||
return Values{
|
||||
data: map[string]Value{}
|
||||
data: []QueryValue{}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return v.data
|
||||
}
|
||||
|
||||
// get gets the first value associated with the given key.
|
||||
// If there are no values associated with the key, get returns
|
||||
// a empty string.
|
||||
@@ -38,11 +32,12 @@ pub fn (v &Values) get(key string) string {
|
||||
if v.data.len == 0 {
|
||||
return ''
|
||||
}
|
||||
vs := v.data[key]
|
||||
if vs.data.len == 0 {
|
||||
return ''
|
||||
for qvalue in v.data {
|
||||
if qvalue.key == key {
|
||||
return qvalue.value
|
||||
}
|
||||
}
|
||||
return vs.data[0]
|
||||
return ''
|
||||
}
|
||||
|
||||
// get_all gets the all the values associated with the given key.
|
||||
@@ -52,36 +47,68 @@ pub fn (v &Values) get_all(key string) []string {
|
||||
if v.data.len == 0 {
|
||||
return []
|
||||
}
|
||||
vs := v.data[key]
|
||||
if vs.data.len == 0 {
|
||||
return []
|
||||
mut values := []string{}
|
||||
for qvalue in v.data {
|
||||
if qvalue.key == key {
|
||||
values << qvalue.value
|
||||
}
|
||||
}
|
||||
return vs.data
|
||||
return values
|
||||
}
|
||||
|
||||
// set sets the key to value. It replaces any existing
|
||||
// values.
|
||||
pub fn (mut v Values) set(key string, value string) {
|
||||
mut a := v.data[key]
|
||||
a.data = [value]
|
||||
v.data[key] = a
|
||||
v.len = v.data.len
|
||||
// A query string can contains several
|
||||
// duplicate, so we need to make sure that we
|
||||
// cover all the edge case.
|
||||
for mut qvalue in v.data {
|
||||
qvalue.value = value
|
||||
}
|
||||
}
|
||||
|
||||
// add adds the value to key. It appends to any existing
|
||||
// values associated with key.
|
||||
pub fn (mut v Values) add(key string, value string) {
|
||||
mut a := v.data[key]
|
||||
if a.data.len == 0 {
|
||||
a.data = []
|
||||
v.data << QueryValue{
|
||||
key: key
|
||||
value: value
|
||||
}
|
||||
a.data << value
|
||||
v.data[key] = a
|
||||
v.len = v.data.len
|
||||
}
|
||||
|
||||
// del deletes the values associated with key.
|
||||
pub fn (mut v Values) del(key string) {
|
||||
v.data.delete(key)
|
||||
for idx, qvalue in v.data {
|
||||
if qvalue.key == key {
|
||||
v.data.delete(idx)
|
||||
}
|
||||
}
|
||||
v.len = v.data.len
|
||||
}
|
||||
|
||||
// return the list of values in the query string
|
||||
pub fn (v Values) values() []string {
|
||||
mut values := []string{}
|
||||
for qvalue in v.data {
|
||||
if qvalue.value != '' {
|
||||
values << qvalue.value
|
||||
}
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
// return a map <key []value> of the query string
|
||||
pub fn (v Values) to_map() map[string][]string {
|
||||
mut result := map[string][]string{}
|
||||
|
||||
for qvalue in v.data {
|
||||
if qvalue.key in result {
|
||||
result[qvalue.key] << qvalue.value
|
||||
} else {
|
||||
result[qvalue.key] = [qvalue.value]
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
Reference in New Issue
Block a user