Skip to content

Commit

Permalink
urllib: change Values.get to return an option type (#17636)
Browse files Browse the repository at this point in the history
  • Loading branch information
Heptalon committed Mar 14, 2023
1 parent daa9034 commit 618c92a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions vlib/net/urllib/urllib_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ fn test_parse_query() {
q2 := urllib.parse_query('format="%l:+%c+%t"')!
// dump(q1)
// dump(q2)
assert q1.get('format') == '"%l: %c %t"'
assert q2.get('format') == '"%l: %c %t"'
assert q1.get('format')? == '"%l: %c %t"'
assert q2.get('format')? == '"%l: %c %t"'
}

fn test_parse_query_orders() {
Expand Down
8 changes: 4 additions & 4 deletions vlib/net/urllib/values.v
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ pub fn new_values() Values {

// 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 {
// none.
pub fn (v &Values) get(key string) ?string {
if v.data.len == 0 {
return ''
return none
}
for qvalue in v.data {
if qvalue.key == key {
return qvalue.value
}
}
return ''
return none
}

// get_all gets the all the values associated with the given key.
Expand Down
8 changes: 5 additions & 3 deletions vlib/net/urllib/values_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
// that can be found in the LICENSE file.
module urllib

fn test_add_key_val() {
fn test_add_and_get_key_val() {
mut values := Values{}
values.add('key', 'value')
val := values.get('key')
assert val == 'value'
present_val := values.get('key') or { '' }
absent_val := values.get('key1') or { '' }
assert present_val == 'value'
assert absent_val == ''
}

fn test_get_all_with_key() {
Expand Down

0 comments on commit 618c92a

Please sign in to comment.