net.urllib: fix values (used for query string/form data)
parent
69084b0c57
commit
9e76d8a638
|
@ -861,7 +861,7 @@ fn _parse_query(m mut Values, query string) ?bool {
|
||||||
|
|
||||||
// encode encodes the values into ``URL encoded'' form
|
// encode encodes the values into ``URL encoded'' form
|
||||||
// ('bar=baz&foo=quux') sorted by key.
|
// ('bar=baz&foo=quux') sorted by key.
|
||||||
fn (v Values) encode() string {
|
pub fn (v Values) encode() string {
|
||||||
if v.size == 0 {
|
if v.size == 0 {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,8 @@ mut:
|
||||||
data []string
|
data []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// using this instead of just ValueStruct
|
||||||
|
// because of unknown map initializer bug
|
||||||
type Value ValueStruct
|
type Value ValueStruct
|
||||||
|
|
||||||
struct Values {
|
struct Values {
|
||||||
|
@ -19,21 +21,27 @@ mut:
|
||||||
size int
|
size int
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_values() Values {
|
// new_values returns a new Values struct for creating
|
||||||
|
// urlencoded query string parameters. it can also be to
|
||||||
|
// post form data with application/x-www-form-urlencoded.
|
||||||
|
// values.encode() will return the encoded data
|
||||||
|
pub fn new_values() Values {
|
||||||
return Values{
|
return Values{
|
||||||
data: map[string]Value{}
|
data: map[string]Value{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (v Value) all() []string {
|
// 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
|
return v.data
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get gets the first value associated with the given key.
|
// get gets the first value associated with the given key.
|
||||||
// If there are no values associated with the key, Get returns
|
// If there are no values associated with the key, get returns
|
||||||
// the empty string. To access multiple values, use the map
|
// a empty string.
|
||||||
// directly.
|
pub fn (v Values) get(key string) string {
|
||||||
fn (v Values) get(key string) string {
|
|
||||||
if v.data.size == 0 {
|
if v.data.size == 0 {
|
||||||
return ''
|
return ''
|
||||||
}
|
}
|
||||||
|
@ -44,26 +52,43 @@ fn (v Values) get(key string) string {
|
||||||
return vs.data[0]
|
return vs.data[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets the key to value. It replaces any existing
|
// 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.
|
||||||
|
pub fn (v Values) get_all(key string) []string {
|
||||||
|
if v.data.size == 0 {
|
||||||
|
return []string
|
||||||
|
}
|
||||||
|
vs := v.data[key]
|
||||||
|
if vs.data.len == 0 {
|
||||||
|
return []string
|
||||||
|
}
|
||||||
|
return vs.data
|
||||||
|
}
|
||||||
|
|
||||||
|
// set sets the key to value. It replaces any existing
|
||||||
// values.
|
// values.
|
||||||
fn (v Values) set(key, value string) {
|
pub fn (v mut Values) set(key, value string) {
|
||||||
v.data[key].data = [value]
|
mut a := v.data[key]
|
||||||
|
a.data = [value]
|
||||||
|
v.data[key] = a
|
||||||
v.size = v.data.size
|
v.size = v.data.size
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add adds the value to key. It appends to any existing
|
// add adds the value to key. It appends to any existing
|
||||||
// values associated with key.
|
// values associated with key.
|
||||||
fn (v mut Values) add(key, value string) {
|
pub fn (v mut Values) add(key, value string) {
|
||||||
mut a := v.data[key]
|
mut a := v.data[key]
|
||||||
|
if a.data.len == 0 {
|
||||||
|
a.data = []string
|
||||||
|
}
|
||||||
a.data << value
|
a.data << value
|
||||||
|
v.data[key] = a
|
||||||
|
v.size = v.data.size
|
||||||
}
|
}
|
||||||
|
|
||||||
// Del deletes the values associated with key.
|
// del deletes the values associated with key.
|
||||||
fn (v mut Values) del(key string) {
|
pub fn (v mut Values) del(key string) {
|
||||||
v.data.delete(key)
|
v.data.delete(key)
|
||||||
v.size = v.data.size
|
v.size = v.data.size
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (v Values) iter() map[string]Value {
|
|
||||||
return v.data
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue