Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #24: Support for Js.Date.t #61

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ppx_src/bin/Codecs.re
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ and generateConstrCodecs = ({ doEncode, doDecode }, { Location.txt: identifier,
doEncode ? Some([%expr (v) => v]) : None,
doDecode ? Some([%expr (v) => Belt.Result.Ok(v)]) : None,
)
| Ldot(Ldot(Lident("Js"), "Date"), "t") => (
doEncode ? Some([%expr Decco.jsDateToJson]) : None,
doDecode ? Some([%expr Decco.jsDateFromJson]) : None
)
| Lident(s) => (
doEncode ? Some(Exp.ident(Ast_convenience.lid(s ++ Utils.encoderFuncSuffix))) : None,
doDecode ? Some(Exp.ident(Ast_convenience.lid(s ++ Utils.decoderFuncSuffix))) : None,
Expand Down
19 changes: 19 additions & 0 deletions src/Decco.re
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ let boolFromJson = (j) =>
let unitToJson = () => Js.Json.number(0.0);
let unitFromJson = (_) => Belt.Result.Ok(());

let jsDateToJson = (v: Js.Date.t) =>
Js.Date.toJSONUnsafe(v) |> Js.Json.string;
let jsDateFromJson = j =>
try(
{
let date =
Js.Json.decodeString(j)->Belt.Option.getExn->Js.Date.fromString;
if (Js.Float.isNaN(Js.Date.getTime(date))) {
failwith("NaN");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be Belt.Result.Error instead, using Result would make the error propagate to the parent and won't crash at runtime.

} else {
Belt.Result.Ok(date);
};
}
) {
| _ => Belt.Result.Error({path: "", message: "Not a date", value: j})
};


let arrayToJson = (encoder, arr) =>
arr
|> Js.Array.map(encoder)
Expand Down Expand Up @@ -177,4 +195,5 @@ module Codecs {
let list = (listToJson, listFromJson);
let option = (optionToJson, optionFromJson);
let unit = (unitToJson, unitFromJson);
let jsDate = (jsDateToJson, jsDateFromJson);
};
30 changes: 30 additions & 0 deletions test/__tests__/test.re
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ open Belt.Result; */
[@decco] type d('v) = Js.Dict.t('v);
[@decco] type simpleVar('a) = 'a;
[@decco] type j = Js.Json.t;
[@decco] type date = Js.Date.t;
[@decco] type optionList = l(o(s));
[@decco] type dictInt = d(int);
[@decco] type variant = A | B(i) | C(i, s);
Expand Down Expand Up @@ -231,6 +232,35 @@ describe("unit", () => {
testGoodDecode("u_decode", u_decode, Js.Json.number(0.), ());
});

describe("date", () => {
let asString = "2020-09-23T02:44:48.787Z";
let asDate = Js.Date.fromString(asString);

test("date_encode", () => {
let json = date_encode(asDate);

[@ocaml.warning "-4"]
(
switch (Js.Json.classify(json)) {
| Js.Json.JSONString(s) => expect(s) |> toBe(asString)
| _ => failwith("Not a JSONString")
}
);
});

describe("date_decode", () => {
testGoodDecode("good", date_decode, Js.Json.string(asString), asDate);

testBadDecode(
"bad",
date_decode,
Js.Json.string("heyy"),
{path: "", message: "Not a date", value: Js.Json.string("heyy")},
);
});
});


describe("tuple", () => {
testEncode("t_encode", (10, "ten"), t_encode, {|[10,"ten"]|});

Expand Down