aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2024-12-19 12:48:12 +0000
committerLouis Pilfold <louis@lpil.uk>2024-12-22 10:56:21 +0000
commitf43a6366e44062a5928340599591048a89f7398f (patch)
tree60f81bfb2919b8a6d67da23f02dae09ae7eb3a0a /test
parent6dc7a93fba3f1fee0913efbe2889dc1e564f3ffc (diff)
downloadgleam_stdlib-f43a6366e44062a5928340599591048a89f7398f.tar.gz
gleam_stdlib-f43a6366e44062a5928340599591048a89f7398f.zip
Recursive decoder
Diffstat (limited to 'test')
-rw-r--r--test/gleam/dynamic/decode_test.gleam26
1 files changed, 26 insertions, 0 deletions
diff --git a/test/gleam/dynamic/decode_test.gleam b/test/gleam/dynamic/decode_test.gleam
index 582f2af..b176c70 100644
--- a/test/gleam/dynamic/decode_test.gleam
+++ b/test/gleam/dynamic/decode_test.gleam
@@ -927,3 +927,29 @@ pub fn js_map_test() {
|> should.be_ok
|> should.equal(dict.from_list([#("a", 10), #("b", 20), #("c", 30)]))
}
+
+type Nested {
+ Nested(List(Nested))
+ Value(String)
+}
+
+fn recursive_decoder() -> decode.Decoder(Nested) {
+ use <- decode.recursive()
+ decode.one_of(decode.string |> decode.map(Value), [
+ decode.list(recursive_decoder()) |> decode.map(Nested),
+ ])
+}
+
+pub fn recursive_test() {
+ let nested = [["one", "two"], ["three"], []]
+ let expected =
+ Nested([
+ Nested([Value("one"), Value("two")]),
+ Nested([Value("three")]),
+ Nested([]),
+ ])
+
+ decode.run(dynamic.from(nested), recursive_decoder())
+ |> should.be_ok
+ |> should.equal(expected)
+}