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

Belt.Result.t support #19

Merged
merged 1 commit into from
Aug 10, 2019
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Unreleased version
### Added
* `Belt.Result.t` support
84 changes: 81 additions & 3 deletions __tests__/test.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ open Belt.Result; */
[@decco] type a('a) = array('a);
[@decco] type l('a) = list('a);
[@decco] type o('a) = option('a);
[@decco] type r('v, 'e) = Belt.Result.t('v, 'e);
[@decco] type simpleVar('a) = 'a;
[@decco] type j = Js.Json.t;
[@decco] type optionList = l(o(s));
Expand Down Expand Up @@ -345,9 +346,9 @@ describe("option", () => {
describe("o_encode", () => {
test("none", () =>
o_encode(s_encode, None)
|> Js.Json.classify
|> expect
|> toBe(Js.Json.JSONNull)
|> Js.Json.classify
|> expect
|> toBe(Js.Json.JSONNull)
);

test("some", () => {
Expand All @@ -374,6 +375,83 @@ describe("option", () => {
});
});

describe("result", () => {
let enc = r_encode(s_encode, i_encode);
let dec = r_decode(s_decode, i_decode);

describe("r_encode", () => {
test("ok", () =>
Belt.Result.Ok("oaky")
|> enc
|> Js.Json.stringify
|> expect
|> toBe("[\"Ok\",\"oaky\"]")
);

test("error", () => {
Belt.Result.Error(404)
|> enc
|> Js.Json.stringify
|> expect
|> toBe("[\"Error\",404]")
});
});

describe("r_decode", () => {
describe("good", () => {
let json = "[\"Ok\",\"yess\"]" |> Js.Json.parseExn;
testGoodDecode("ok", dec, json, Ok("yess"));

let json = "[\"Error\",911]" |> Js.Json.parseExn;
testGoodDecode("error", dec, json, Error(911));
});

describe("bad", () => {
let json = Js.Json.number(12.);
testBadDecode("not an array", dec, json, {
path: "",
message: "Not an array",
value: json
});

let json = "[]" |> Js.Json.parseExn;
testBadDecode("length != 2", dec, json, {
path: "",
message: "Expected exactly 2 values in array",
value: json
});

let json = "[0,1]" |> Js.Json.parseExn;
testBadDecode("constructor not a string", dec, json, {
path: "",
message: "Not a string",
value: Js.Json.number(0.)
});

let json = "[\"bad\",1]" |> Js.Json.parseExn;
testBadDecode("unrecognized constructor", dec, json, {
path: "",
message: "Expected either \"Ok\" or \"Error\"",
value: Js.Json.string("bad")
});

let json = "[\"Ok\",1]" |> Js.Json.parseExn;
testBadDecode("bad Ok decode", dec, json, {
path: "",
message: "Not a string",
value: Js.Json.number(1.)
});

let json = "[\"Error\",null]" |> Js.Json.parseExn;
testBadDecode("bad Error decode", dec, json, {
path: "",
message: "Not a number",
value: Js.Json.null
});
});
});
});

describe("falseable", () => {
describe("falseable_encode", () => {
test("none", () =>
Expand Down
4 changes: 4 additions & 0 deletions ppx/bin/Codecs.re
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ and generateConstrCodecs = ({ doEncode, doDecode }, { Location.txt: identifier,
doEncode ? Some([%expr Decco.optionToJson]) : None,
doDecode ? Some([%expr Decco.optionFromJson]) : None
)
| Ldot(Ldot(Lident("Belt"), "Result"), "t") => (
doEncode ? Some([%expr Decco.resultToJson]) : None,
doDecode ? Some([%expr Decco.resultFromJson]) : None
)
| Ldot(Ldot(Lident("Js"), "Json"), "t") => (
doEncode ? Some([%expr (v) => v]) : None,
doDecode ? Some([%expr (v) => Belt.Result.Ok(v)]) : None,
Expand Down
28 changes: 28 additions & 0 deletions src/Decco.re
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,34 @@ let optionFromJson = (decoder, json) =>
| _ => decoder(json) |> map(_, v => Some(v))
};

let resultToJson = (okEncoder, errorEncoder, result) =>
switch result {
| Ok(v) => [| Js.Json.string("Ok"), okEncoder(v) |]
| Error(e) => [| Js.Json.string("Error"), errorEncoder(e) |]
}
|> Js.Json.array;

let resultFromJson = (okDecoder, errorDecoder, json) =>
switch (Js.Json.decodeArray(json)) {
| Some([| variantConstructorId, payload |]) =>
switch (Js.Json.decodeString(variantConstructorId)) {
| Some("Ok") =>
okDecoder(payload)
-> Belt.Result.map(v => Ok(v))

| Some("Error") =>
switch(errorDecoder(payload)) {
| Ok(v) => Ok(Error(v))
| Error(e) => Error(e)
}

| Some(_) => error("Expected either \"Ok\" or \"Error\"", variantConstructorId)
| None => error("Not a string", variantConstructorId)
}
| Some(_) => error("Expected exactly 2 values in array", json)
| None => error("Not an array", json)
};

module Codecs {
include Codecs;
let string = (stringToJson, stringFromJson);
Expand Down