aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2020-05-19 17:33:53 +0100
committerLouis Pilfold <louis@lpil.uk>2020-05-19 19:22:49 +0100
commitf3c4b7a3170fb4beb7483bba929d1eca616f48df (patch)
tree4a92abb0428d776ac54a6b27c7bb16e27b3f9752
parentcc2beb781cb532456fca0983b91401c3fce00ca4 (diff)
downloadgleam_stdlib-f3c4b7a3170fb4beb7483bba929d1eca616f48df.tar.gz
gleam_stdlib-f3c4b7a3170fb4beb7483bba929d1eca616f48df.zip
Remove Erlang glue for uri.query_to_string
-rw-r--r--src/gleam/gleam_uri_native.erl10
-rw-r--r--src/gleam/uri.gleam29
2 files changed, 28 insertions, 11 deletions
diff --git a/src/gleam/gleam_uri_native.erl b/src/gleam/gleam_uri_native.erl
index e0f9d2d..5d225cb 100644
--- a/src/gleam/gleam_uri_native.erl
+++ b/src/gleam/gleam_uri_native.erl
@@ -1,5 +1,5 @@
-module (gleam_uri_native).
--export ([parse/1, to_string/1, parse_query/1, query_to_string/1]).
+-export ([parse/1, to_string/1, parse_query/1]).
find_key(Key, Map) ->
case maps:find(Key, Map) of
@@ -43,11 +43,3 @@ parse_query(String) ->
Parts ->
{ok, Parts}
end.
-
-query_to_string(Parts) ->
- case uri_string:compose_query(Parts, [{encoding, utf8}]) of
- String when is_binary(String) ->
- String;
- % Return value when empty
- [] -> <<"">>
- end.
diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam
index 84b2360..21145df 100644
--- a/src/gleam/uri.gleam
+++ b/src/gleam/uri.gleam
@@ -10,9 +10,11 @@
import gleam/list
import gleam/result.{Option}
import gleam/string
+import gleam/dynamic.{Dynamic}
/// Type representing holding the parsed components of an URI.
/// All components of a URI are optional, except the path.
+///
pub type Uri {
Uri(
scheme: Option(String),
@@ -29,6 +31,7 @@ pub type Uri {
/// If the string is not a valid URI string then an error is returned.
///
/// The opposite operation is `uri.to_string`
+///
pub external fn parse(String) -> Result(Uri, Nil) =
"gleam_uri_native" "parse"
@@ -36,16 +39,36 @@ pub external fn parse(String) -> Result(Uri, Nil) =
/// Returns an error for invalid encoding.
///
/// The opposite operation is `uri.query_to_string`.
+///
pub external fn parse_query(
String,
) -> Result(List(tuple(String, String)), Nil) =
"gleam_uri_native" "parse_query"
+type Encoding {
+ Utf8
+}
+
+type ErlQueryToStringOption {
+ Encoding(Encoding)
+}
+
+external fn erl_query_to_string(
+ List(tuple(String, String)),
+ List(ErlQueryToStringOption),
+) -> Dynamic =
+ "uri_string" "compose_query"
+
/// Encode a list of key value pairs as a URI query string.
///
/// The opposite operation is `uri.parse_query`.
-pub external fn query_to_string(List(tuple(String, String))) -> String =
- "gleam_uri_native" "query_to_string"
+///
+pub fn query_to_string(query: List(tuple(String, String))) -> String {
+ query
+ |> erl_query_to_string([Encoding(Utf8)])
+ |> dynamic.string
+ |> result.unwrap("")
+}
fn do_path_segments(input, accumulator) {
case input {
@@ -67,6 +90,7 @@ fn do_path_segments(input, accumulator) {
///
/// Removes empty segments and resolves dot-segments as specified in
/// [section 5.2](https://www.ietf.org/rfc/rfc3986.html#section-5.2) of the RFC.
+///
pub fn path_segments(path) {
do_path_segments(string.split(path, "/"), [])
}
@@ -74,5 +98,6 @@ pub fn path_segments(path) {
/// Encode a `Uri` value as a URI string.
///
/// The opposite operation is `uri.parse`.
+///
pub external fn to_string(Uri) -> String =
"gleam_uri_native" "to_string"