aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/net/urllib/values.v
blob: ee5c329b172461eafbf57076f194de0d7d204fa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module urllib

struct Value {
pub mut:
	data []string
}

struct Values {
pub mut:
	data map[string]Value
	len  int
}

// 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{
		data: map[string]Value{}
	}
}

// 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.
pub fn (v &Values) get(key string) string {
	if v.data.len == 0 {
		return ''
	}
	vs := v.data[key]
	if vs.data.len == 0 {
		return ''
	}
	return vs.data[0]
}

// 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.len == 0 {
		return []
	}
	vs := v.data[key]
	if vs.data.len == 0 {
		return []
	}
	return vs.data
}

// 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
}

// 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 = []
	}
	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)
	v.len = v.data.len
}