| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module urllib |
| 5 | |
| 6 | pub struct QueryValue { |
| 7 | pub mut: |
| 8 | key string |
| 9 | value string |
| 10 | } |
| 11 | |
| 12 | pub struct Values { |
| 13 | pub mut: |
| 14 | data []QueryValue |
| 15 | len int |
| 16 | } |
| 17 | |
| 18 | // new_values returns a new Values struct for creating |
| 19 | // urlencoded query string parameters. it can also be to |
| 20 | // post form data with application/x-www-form-urlencoded. |
| 21 | // values.encode() will return the encoded data |
| 22 | pub fn new_values() Values { |
| 23 | return Values{ |
| 24 | data: []QueryValue{} |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // get gets the first value associated with the given key. |
| 29 | // If there are no values associated with the key, get returns |
| 30 | // none. |
| 31 | pub fn (v &Values) get(key string) ?string { |
| 32 | if v.data.len == 0 { |
| 33 | return none |
| 34 | } |
| 35 | for qvalue in v.data { |
| 36 | if qvalue.key == key { |
| 37 | return qvalue.value |
| 38 | } |
| 39 | } |
| 40 | return none |
| 41 | } |
| 42 | |
| 43 | // get_all gets the all the values associated with the given key. |
| 44 | // If there are no values associated with the key, get returns |
| 45 | // a empty []string. |
| 46 | pub fn (v &Values) get_all(key string) []string { |
| 47 | if v.data.len == 0 { |
| 48 | return [] |
| 49 | } |
| 50 | mut values := []string{} |
| 51 | for qvalue in v.data { |
| 52 | if qvalue.key == key { |
| 53 | values << qvalue.value |
| 54 | } |
| 55 | } |
| 56 | return values |
| 57 | } |
| 58 | |
| 59 | // set sets the key to value. It replaces any existing |
| 60 | // values, or create a new bucket with the new key if it is missed. |
| 61 | pub fn (mut v Values) set(key string, value string) { |
| 62 | // A query string can contains several |
| 63 | // duplicate, so we need to make sure that we |
| 64 | // cover all the edge case. |
| 65 | mut found := false |
| 66 | for mut qvalue in v.data { |
| 67 | if qvalue.key == key { |
| 68 | found = true |
| 69 | qvalue.value = value |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if !found { |
| 74 | v.add(key, value) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // add adds the value to key. It appends to any existing |
| 79 | // values associated with key. |
| 80 | pub fn (mut v Values) add(key string, value string) { |
| 81 | v.data << QueryValue{ |
| 82 | key: key |
| 83 | value: value |
| 84 | } |
| 85 | v.len = v.data.len |
| 86 | } |
| 87 | |
| 88 | // del deletes the values associated with key. |
| 89 | pub fn (mut v Values) del(key string) { |
| 90 | for idx, qvalue in v.data { |
| 91 | if qvalue.key == key { |
| 92 | v.data.delete(idx) |
| 93 | } |
| 94 | } |
| 95 | v.len = v.data.len |
| 96 | } |
| 97 | |
| 98 | // return the list of values in the query string |
| 99 | pub fn (v Values) values() []string { |
| 100 | mut values := []string{} |
| 101 | for qvalue in v.data { |
| 102 | if qvalue.value != '' { |
| 103 | values << qvalue.value |
| 104 | } |
| 105 | } |
| 106 | return values |
| 107 | } |
| 108 | |
| 109 | // return a map <key []value> of the query string |
| 110 | pub fn (v Values) to_map() map[string][]string { |
| 111 | mut result := map[string][]string{} |
| 112 | |
| 113 | for qvalue in v.data { |
| 114 | if qvalue.key in result { |
| 115 | result[qvalue.key] << qvalue.value |
| 116 | } else { |
| 117 | result[qvalue.key] = [qvalue.value] |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return result |
| 122 | } |
| 123 | |