diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-09-10 18:59:09 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-09-10 18:59:09 +0100 |
commit | 1d6a4b91d7db1cd7d5c66a9a0e5bc4be22e6099b (patch) | |
tree | 2f62b6cdeab79ad265b0fb5b1bf46a86d7b57b66 | |
parent | 32ac4ec2523e7226cecbf73a1708cd36ea3b3eb1 (diff) | |
download | gleam_stdlib-1d6a4b91d7db1cd7d5c66a9a0e5bc4be22e6099b.tar.gz gleam_stdlib-1d6a4b91d7db1cd7d5c66a9a0e5bc4be22e6099b.zip |
JS map decoding
-rw-r--r-- | src/gleam/dynamic.gleam | 45 | ||||
-rw-r--r-- | src/gleam_stdlib.js | 4 | ||||
-rw-r--r-- | test/gleam/dynamic_test.gleam | 20 |
3 files changed, 40 insertions, 29 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index 5dd0feb..4cc71a4 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -5,9 +5,9 @@ import gleam/int import gleam/option import gleam/result import gleam/string_builder +import gleam/map.{Map} if erlang { - import gleam/map.{Map} import gleam/option.{Option} } @@ -783,26 +783,35 @@ pub fn typed_tuple6( Ok(#(a, b, c, d, e, f)) } +/// Checks to see if the Dynamic value is map. +/// +/// ## Examples +/// +/// > import gleam/map +/// > map(from(map.new())) +/// Ok(map.new()) +/// +/// > map(from(1)) +/// Error(DecodeError(expected: "Map", found: "Int")) +/// +/// > map(from("")) +/// Error(DecodeError(expected: "Map", found: "String")) +/// +pub fn map(from value: Dynamic) -> Result(Map(Dynamic, Dynamic), DecodeError) { + decode_map(value) +} + if erlang { - /// Checks to see if the Dynamic value is map. - /// - /// ## Examples - /// - /// > import gleam/map - /// > map(from(map.new())) - /// Ok(map.new()) - /// - /// > map(from(1)) - /// Error(DecodeError(expected: "Map", found: "Int")) - /// - /// > map(from("")) - /// Error(DecodeError(expected: "Map", found: "String")) - /// - pub external fn map( - from: Dynamic, - ) -> Result(Map(Dynamic, Dynamic), DecodeError) = + external fn decode_map(Dynamic) -> Result(Map(Dynamic, Dynamic), DecodeError) = "gleam_stdlib" "decode_map" +} +if javascript { + external fn decode_map(Dynamic) -> Result(Map(Dynamic, Dynamic), DecodeError) = + "../gleam_stdlib.js" "decode_map" +} + +if erlang { /// Joins multiple decoders into one. When run they will each be tried in turn /// until one succeeds, or they all fail. /// diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index 500066d..4a3026c 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -531,3 +531,7 @@ export function tuple_get(data, index) { export function decode_list(data) { return List.isList(data) ? new Ok(data) : decoder_error("List", data); } + +export function decode_map(data) { + return data instanceof Map ? new Ok(data) : decoder_error("Map", data); +} diff --git a/test/gleam/dynamic_test.gleam b/test/gleam/dynamic_test.gleam index c93210b..b901c86 100644 --- a/test/gleam/dynamic_test.gleam +++ b/test/gleam/dynamic_test.gleam @@ -718,18 +718,16 @@ pub fn typed_tuple6_test() { ))) } -if erlang { - pub fn map_test() { - map.new() - |> dynamic.from - |> dynamic.map - |> should.equal(Ok(map.new())) +pub fn map_test() { + map.new() + |> dynamic.from + |> dynamic.map + |> should.equal(Ok(map.new())) - 1 - |> dynamic.from - |> dynamic.map - |> should.equal(Error(DecodeError(expected: "Map", found: "Int"))) - } + 1 + |> dynamic.from + |> dynamic.map + |> should.equal(Error(DecodeError(expected: "Map", found: "Int"))) } pub fn list_test() { |