1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

urllib: change Values.get to return an option type (#17636)

This commit is contained in:
Heptalon 2023-03-14 07:44:40 +01:00 committed by GitHub
parent daa9034583
commit 618c92a13b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 9 deletions

View File

@ -40,8 +40,8 @@ fn test_parse_query() {
q2 := urllib.parse_query('format="%l:+%c+%t"')! q2 := urllib.parse_query('format="%l:+%c+%t"')!
// dump(q1) // dump(q1)
// dump(q2) // dump(q2)
assert q1.get('format') == '"%l: %c %t"' assert q1.get('format')? == '"%l: %c %t"'
assert q2.get('format') == '"%l: %c %t"' assert q2.get('format')? == '"%l: %c %t"'
} }
fn test_parse_query_orders() { fn test_parse_query_orders() {

View File

@ -27,17 +27,17 @@ pub fn new_values() Values {
// 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
// a empty string. // none.
pub fn (v &Values) get(key string) string { pub fn (v &Values) get(key string) ?string {
if v.data.len == 0 { if v.data.len == 0 {
return '' return none
} }
for qvalue in v.data { for qvalue in v.data {
if qvalue.key == key { if qvalue.key == key {
return qvalue.value return qvalue.value
} }
} }
return '' return none
} }
// get_all gets the all the values associated with the given key. // get_all gets the all the values associated with the given key.

View File

@ -3,11 +3,13 @@
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module urllib module urllib
fn test_add_key_val() { fn test_add_and_get_key_val() {
mut values := Values{} mut values := Values{}
values.add('key', 'value') values.add('key', 'value')
val := values.get('key') present_val := values.get('key') or { '' }
assert val == 'value' absent_val := values.get('key1') or { '' }
assert present_val == 'value'
assert absent_val == ''
} }
fn test_get_all_with_key() { fn test_get_all_with_key() {