import decode.{type Decoder} import json.{Malformed, Mismatch, Missing} import std/option import std/test.{assert_equal} @derive Eq Debug Show type Ship { Ship(symbol: String, fuel: Int, route: List) } fn ship :: -> Decoder { use symbol <- decode.field "symbol" (decode.string ()) use fuel <- decode.optional_field "fuel" (decode.int ()) use route <- decode.field "route" (decode.list (decode.string ())) decode.succeed Ship(symbol, option.unwrap_or fuel 0, route) } @test fn a_record_decodes_field_by_field :: { assert_equal ( decode.from_string "\{\"symbol\": \"ORION-1\", \"fuel\": 80, \"route\": [\"A\", \"B\"]\}" (ship ()) ) Ok(Ship("ORION-1", 80, ["A", "B"])) } @test fn an_optional_field_may_be_absent_or_null :: { assert_equal (decode.from_string "\{\"symbol\": \"X\", \"route\": []\}" (ship ())) Ok(Ship("X", 0, [])) assert_equal ( decode.from_string "\{\"symbol\": \"X\", \"fuel\": null, \"route\": []\}" (ship ()) ) Ok(Ship("X", 0, [])) } @test fn fields_not_asked_for_are_ignored :: { assert_equal ( decode.from_string "\{\"symbol\": \"X\", \"route\": [], \"extra\": true\}" (ship ()) ) Ok(Ship("X", 0, [])) } @test fn an_error_names_its_path :: { assert_equal (decode.from_string "\{\"symbol\": 7, \"route\": []\}" (ship ())) Error(Mismatch(["symbol"], "a string", "an integer")) assert_equal ( decode.from_string "\{\"symbol\": \"X\", \"route\": [\"A\", 2]\}" (ship ()) ) Error(Mismatch(["route", "1"], "a string", "an integer")) assert_equal (decode.from_string "\{\"route\": []\}" (ship ())) Error(Missing([], "symbol")) assert_equal (decode.from_string "[1]" (ship ())) Error(Mismatch([], "an object", "an array")) } @test fn nested_paths_accumulate :: { let fleet = decode.field "ships" (decode.list (ship ())) decode.succeed assert_equal ( decode.from_string "\{\"ships\": [\{\"symbol\": \"A\", \"route\": []\}, \{\"route\": []\}]\}" fleet ) Error(Missing(["ships", "1"], "symbol")) assert_equal "{Missing(["ships", "1"], "symbol")}" "$.ships.1: missing field \"symbol\"" } @test fn a_parse_error_comes_through :: { assert_equal (decode.from_string "\{" (ship ())) Error(Malformed(1, "expected a key (a string)")) } @test fn scalars :: { assert_equal (decode.from_string "1.5" (decode.float ())) Ok(1.5) assert_equal (decode.from_string "2" (decode.float ())) Ok(2.0) assert_equal (decode.from_string "2.0" (decode.int ())) Error(Mismatch([], "an integer", "a number")) assert_equal (decode.from_string "true" (decode.bool ())) Ok(True) assert_equal (decode.from_string "null" (decode.nullable (decode.int ()))) Ok(None) assert_equal (decode.from_string "3" (decode.nullable (decode.int ()))) Ok(Some(3)) } @derive Eq Debug Show type Command { Orbit, Navigate(String) } fn command :: -> Decoder { decode.field "action" (decode.string ()) fn action { match action { "orbit" -> decode.succeed Orbit , "navigate" -> decode.field "waypoint" (decode.string ()) fn w { decode.succeed Navigate(w) } , _ -> decode.fail "orbit or navigate" } } } @test fn then_and_fail_read_a_tagged_union :: { assert_equal (decode.from_string "\{\"action\": \"orbit\"\}" (command ())) Ok(Orbit) assert_equal ( decode.from_string "\{\"action\": \"navigate\", \"waypoint\": \"SOL-A2\"\}" (command ()) ) Ok(Navigate("SOL-A2")) assert_equal (decode.from_string "\{\"action\": \"jump\"\}" (command ())) Error(Mismatch([], "orbit or navigate", "an object")) } @test fn one_of_tries_each_in_turn :: { let id = decode.one_of (decode.int () |> decode.map fn n { "#{n}" }) [decode.string ()] assert_equal (decode.from_string "7" id) Ok("#7") assert_equal (decode.from_string "\"seven\"" id) Ok("seven") assert_equal (decode.from_string "true" id) Error(Mismatch([], "an integer", "a bool")) } @test fn map_and_then :: { let doubled = decode.int () |> decode.map fn n { n * 2 } assert_equal (decode.from_string "21" doubled) Ok(42) let positive = decode.int () |> decode.then fn n { if n > 0 { decode.succeed n } else { decode.fail "a positive integer" } } assert_equal (decode.from_string "-1" positive) Error(Mismatch([], "a positive integer", "an integer")) }