diff options
author | Peter Saxton <peterhsaxton@gmail.com> | 2020-07-06 13:40:54 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-07-06 23:56:40 +0100 |
commit | 92a4ce54b4fb67a7b36d26c01471b1ecfc772bd7 (patch) | |
tree | a9da99f666a49303492055f571a6c12c91b93184 | |
parent | 1389c09ae4aa19acf522cacc67d89c26a10996cf (diff) | |
download | gleam_stdlib-92a4ce54b4fb67a7b36d26c01471b1ecfc772bd7.tar.gz gleam_stdlib-92a4ce54b4fb67a7b36d26c01471b1ecfc772bd7.zip |
handle valid query string case where no value is provided
-rw-r--r-- | src/gleam/uri.gleam | 17 | ||||
-rw-r--r-- | test/gleam/uri_test.gleam | 5 |
2 files changed, 21 insertions, 1 deletions
diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam index 58ffeba..1c2447b 100644 --- a/src/gleam/uri.gleam +++ b/src/gleam/uri.gleam @@ -81,7 +81,22 @@ external fn erl_parse_query(String) -> Dynamic = pub fn parse_query(query: String) -> Result(List(tuple(String, String)), Nil) { query |> erl_parse_query - |> dynamic.typed_list(dynamic.typed_tuple2(_, dynamic.string, dynamic.string)) + |> dynamic.typed_list( + dynamic.typed_tuple2( + _, + dynamic.string, + fn(raw) { + case dynamic.string(raw) { + Ok(value) -> Ok(value) + Error(_reason) -> case dynamic.bool(raw) { + Ok(True) -> Ok("") + // Note this error message will be niled at the end of this function. + _ -> Error("expected a boolean or string, got neither") + } + } + }, + ), + ) |> result.nil_error } diff --git a/test/gleam/uri_test.gleam b/test/gleam/uri_test.gleam index 2beeed5..ecefeec 100644 --- a/test/gleam/uri_test.gleam +++ b/test/gleam/uri_test.gleam @@ -72,6 +72,11 @@ pub fn parse_empty_query_string_test() { should.equal(parsed, []) } +pub fn parse_query_string_with_empty_test() { + uri.parse_query("present") + |> should.equal(Ok([tuple("present", "")])) +} + pub fn error_parsing_query_test() { should.equal(uri.parse_query("%C2"), Error(Nil)) } |