aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2020-06-18 21:39:29 +0100
committerLouis Pilfold <louis@lpil.uk>2020-06-18 21:39:29 +0100
commit5271f212d976e9fe76145ebc516bb56522934f33 (patch)
tree230e863f541f23c18f4dd76ddc77dd1f3f2e946f
parent1c72f8236612ef5a4f5a062157d3639ae468fe0e (diff)
downloadgleam_stdlib-5271f212d976e9fe76145ebc516bb56522934f33.tar.gz
gleam_stdlib-5271f212d976e9fe76145ebc516bb56522934f33.zip
More consistent naming in Dynamic module
-rw-r--r--CHANGELOG.md5
-rw-r--r--src/gleam/dynamic.gleam38
-rw-r--r--src/gleam/uri.gleam4
-rw-r--r--test/gleam/dynamic_test.gleam40
4 files changed, 52 insertions, 35 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 877854a..de8c788 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,7 @@
# Changelog
+## Unreleased
+
- `bit_string` module created with `from_string`, `byte_size`, `append`,
`part`, `int_to_u32` and `int_from_u32`.
- `os` module created with `get_env`, `insert_env`, `delete_env` and
@@ -8,6 +10,9 @@
- The `dynamic` module gains the `bit_string` function.
- The `uri` module gains the `origin` and `merge` function.
- The `io.debug` function returns the printed term.
+- The `dynamic.list` function has been renamed to `dynamic.typed_list`.
+- The `dynamic.opaque_list` function has been renamed to `dynamic.list`.
+- The `dynamic.tuple2_of` function has been renamed to `dynamic.typed_tuple2`.
## 0.9.0 - 2020-05-26
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam
index f1c8f2d..c82ae86 100644
--- a/src/gleam/dynamic.gleam
+++ b/src/gleam/dynamic.gleam
@@ -131,15 +131,18 @@ pub external fn thunk(from: Dynamic) -> Result(fn() -> Dynamic, String) =
/// Check to see whether a Dynamic value is a list, and return the list if it
/// is.
///
+/// If you wish to decode all the elements in the list use the `typed_list`
+/// instead.
+///
/// ## Examples
///
-/// > opaque_list(from(["a", "b", "c"]))
+/// > list(from(["a", "b", "c"]))
/// Ok([from("a"), from("b"), from("c")])
///
-/// > opaque_list(1)
+/// > list(1)
/// Error("Expected an Int, got a binary")
///
-pub external fn opaque_list(from: Dynamic) -> Result(List(Dynamic), String) =
+pub external fn list(from: Dynamic) -> Result(List(Dynamic), String) =
"gleam_stdlib" "decode_list"
/// Check to see whether a Dynamic value is a list of a particular type, and
@@ -149,23 +152,26 @@ pub external fn opaque_list(from: Dynamic) -> Result(List(Dynamic), String) =
/// the list. The list is only decoded if all elements in the list can be
/// successfully decoded using this function.
///
+/// If you do not wish to decode all the elements in the list use the `list`
+/// function instead.
+///
/// ## Examples
///
-/// > list(from(["a", "b", "c"]), string)
+/// > typed_list(from(["a", "b", "c"]), containing: string)
/// Ok(["a", "b", "c"])
///
-/// > list(from([1, 2, 3]), string)
+/// > typed_list(from([1, 2, 3]), containing: string)
/// Error("Expected an Int, got a binary")
///
-/// > list(from("ok"), string)
+/// > typed_list(from("ok"), containing: string)
/// Error("Expected a List, got a binary")
///
-pub fn list(
+pub fn typed_list(
from dynamic: Dynamic,
containing decoder_type: fn(Dynamic) -> Result(inner, String),
) -> Result(List(inner), String) {
dynamic
- |> opaque_list
+ |> list
|> result.then(list_mod.traverse(_, decoder_type))
}
@@ -208,6 +214,9 @@ pub external fn element(
/// Check to see if the Dynamic value is a 2 element tuple.
///
+/// If you do not wish to decode all the elements in the tuple use the
+/// `typed_tuple2` function instead.
+///
/// ## Examples
///
/// > tuple2(from(tuple(1, 2)))
@@ -227,21 +236,24 @@ pub external fn tuple2(
/// Check to see if the Dynamic value is a 2 element tuple containing two
/// specifically typed elements.
///
+/// If you wish to decode all the elements in the list use the `typed_tuple2`
+/// instead.
+///
/// ## Examples
///
-/// > tuple2_of(from(tuple(1, 2)), int, int)
+/// > typed_tuple2(from(tuple(1, 2)), int, int)
/// Ok(tuple(1, 2))
///
-/// > tuple2_of(from(tuple(1, 2.0)), int, float)
+/// > typed_tuple2(from(tuple(1, 2.0)), int, float)
/// Ok(tuple(1, 2.0))
///
-/// > tuple2_of(from(tuple(1, 2, 3)), int, float)
+/// > typed_tuple2(from(tuple(1, 2, 3)), int, float)
/// Error("Expected a 2 element tuple, got a 3 element tuple")
///
-/// > tuple2_of(from(""), int, float)
+/// > typed_tuple2(from(""), int, float)
/// Error("Expected a tuple, got a binary")
///
-pub fn tuple2_of(
+pub fn typed_tuple2(
from tup: Dynamic,
first decode_first: Decoder(a),
second decode_second: Decoder(b),
diff --git a/src/gleam/uri.gleam b/src/gleam/uri.gleam
index 8f8d754..fbd95ec 100644
--- a/src/gleam/uri.gleam
+++ b/src/gleam/uri.gleam
@@ -81,8 +81,8 @@ external fn erl_parse_query(String) -> Dynamic =
pub fn parse_query(query: String) -> Result(List(tuple(String, String)), Nil) {
query
|> erl_parse_query
- |> dynamic.list(dynamic.tuple2_of(_, dynamic.string, dynamic.string))
- |> result.map_error(fn(_) { Nil })
+ |> dynamic.typed_list(dynamic.typed_tuple2(_, dynamic.string, dynamic.string))
+ |> result.nil_error
}
type Encoding {
diff --git a/test/gleam/dynamic_test.gleam b/test/gleam/dynamic_test.gleam
index fea0382..18d7b4d 100644
--- a/test/gleam/dynamic_test.gleam
+++ b/test/gleam/dynamic_test.gleam
@@ -168,45 +168,45 @@ pub fn atom_test() {
|> should.be_error
}
-pub fn list_test() {
+pub fn typed_list_test() {
[]
|> dynamic.from
- |> dynamic.list(dynamic.string)
+ |> dynamic.typed_list(dynamic.string)
|> should.equal(Ok([]))
[]
|> dynamic.from
- |> dynamic.list(dynamic.int)
+ |> dynamic.typed_list(dynamic.int)
|> should.equal(Ok([]))
[1, 2, 3]
|> dynamic.from
- |> dynamic.list(dynamic.int)
+ |> dynamic.typed_list(dynamic.int)
|> should.equal(Ok([1, 2, 3]))
[[1], [2], [3]]
|> dynamic.from
- |> dynamic.list(dynamic.list(_, dynamic.int))
+ |> dynamic.typed_list(dynamic.typed_list(_, dynamic.int))
|> should.equal(Ok([[1], [2], [3]]))
1
|> dynamic.from
- |> dynamic.list(dynamic.string)
+ |> dynamic.typed_list(dynamic.string)
|> should.be_error
1.0
|> dynamic.from
- |> dynamic.list(dynamic.int)
+ |> dynamic.typed_list(dynamic.int)
|> should.be_error
[""]
|> dynamic.from
- |> dynamic.list(dynamic.int)
+ |> dynamic.typed_list(dynamic.int)
|> should.be_error
[dynamic.from(1), dynamic.from("not an int")]
|> dynamic.from
- |> dynamic.list(dynamic.int)
+ |> dynamic.typed_list(dynamic.int)
|> should.be_error
}
@@ -301,30 +301,30 @@ pub fn tuple2_test() {
|> should.equal(Error("Expected a 2 element tuple, got an int"))
}
-pub fn tuple2_of_test() {
+pub fn typed_tuple2_test() {
tuple(1, 2)
|> dynamic.from
- |> dynamic.tuple2_of(dynamic.int, dynamic.int)
+ |> dynamic.typed_tuple2(dynamic.int, dynamic.int)
|> should.equal(Ok(tuple(1, 2)))
tuple(1, "")
|> dynamic.from
- |> dynamic.tuple2_of(dynamic.int, dynamic.string)
+ |> dynamic.typed_tuple2(dynamic.int, dynamic.string)
|> should.equal(Ok(tuple(1, "")))
tuple(1, "")
|> dynamic.from
- |> dynamic.tuple2_of(dynamic.int, dynamic.int)
+ |> dynamic.typed_tuple2(dynamic.int, dynamic.int)
|> should.equal(Error("Expected an int, got a binary"))
tuple(1, 2, 3)
|> dynamic.from
- |> dynamic.tuple2
+ |> dynamic.typed_tuple2(dynamic.int, dynamic.int)
|> should.equal(Error("Expected a 2 element tuple, got a 3 element tuple"))
1
|> dynamic.from
- |> dynamic.tuple2
+ |> dynamic.typed_tuple2(dynamic.int, dynamic.int)
|> should.equal(Error("Expected a 2 element tuple, got an int"))
}
@@ -340,24 +340,24 @@ pub fn map_test() {
|> should.equal(Error("Expected a map, got an int"))
}
-pub fn opaque_list_test() {
+pub fn list_test() {
[]
|> dynamic.from
- |> dynamic.opaque_list
+ |> dynamic.list
|> should.equal(Ok([]))
[1, 2]
|> dynamic.from
- |> dynamic.opaque_list
+ |> dynamic.list
|> should.equal(Ok([dynamic.from(1), dynamic.from(2)]))
[dynamic.from(1), dynamic.from(2.0)]
|> dynamic.from
- |> dynamic.opaque_list
+ |> dynamic.list
|> should.equal(Ok([dynamic.from(1), dynamic.from(2.0)]))
1
|> dynamic.from
- |> dynamic.opaque_list
+ |> dynamic.list
|> should.equal(Error("Expected a list, got an int"))
}