Skip to content

Commit

Permalink
Support ReScript v11-rc.5 (#61)
Browse files Browse the repository at this point in the history
Co-authored-by: mununki <[email protected]>
  • Loading branch information
namenu and mununki authored Nov 9, 2023
1 parent e572c98 commit 6066c7c
Show file tree
Hide file tree
Showing 28 changed files with 103 additions and 102 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- a190663 Utilize Js.Json.Boolean(bool) instead oif Js.Json.True, False https://github.com/green-labs/ppx_spice/pull/58
- a190663 Add support of uncurried mode for interface(*.resi) https://github.com/green-labs/ppx_spice/pull/58
- Support the compiler v11-rc.5 https://github.com/green-labs/ppx_spice/pull/61

## 0.2.0

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ let t_decode = ...
let encoded = One->t_encode // Js.Json.string(`하나`)
let decoded = Js.Json.string(`second`)->t_decode // Belt.Result.Ok(Two)
let decoded = Js.Json.string(`second`)->t_decode // Ok(Two)
```

2. Record
Expand All @@ -117,7 +117,7 @@ let sampleRecord: t = {
let encoded = sampleRecord->Records.t_encode // sampleJson
let decoded = sampleJson->Records.t_decode // Belt.Result.Ok(sampleRecord)
let decoded = sampleJson->Records.t_decode // Ok(sampleRecord)
```

## Getting Started
Expand Down
4 changes: 2 additions & 2 deletions docs/GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type user = {
nickname?: string,
}
let user: Belt.Result.t<user, Spice.decodeError> = data->user_decode // user_decode is generated by ppx_spice
let user: result<user, Spice.decodeError> = data->user_decode // user_decode is generated by ppx_spice
let json: Js.Json.t = user->Belt.Result.getExn->user_encode // user_encode is generated by ppx_spice
```
Expand Down Expand Up @@ -58,7 +58,7 @@ type user = {
language: language,
}
let user: Belt.Result.t<user, Spice.decodeError> = json->user_decode
let user: result<user, Spice.decodeError> = json->user_decode
let json: Js.Json.t = user->Result.getExn->user_encode
```
Expand Down
2 changes: 1 addition & 1 deletion examples/src/Tuple.res
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ let data = %raw(`
}
`)

let user: Belt.Result.t<user, Spice.decodeError> = data->user_decode
let user: result<user, Spice.decodeError> = data->user_decode

let json: Js.Json.t = user->Result.getExn->user_encode
26 changes: 13 additions & 13 deletions examples/src/UntaggedVariant.res
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ let t0_decode = v =>
if Js.Array.length(json_arr) != 1 {
Spice.error("Invalid number of arguments to variant constructor", v)
} else {
Belt.Result.Ok(A)
Ok(A)
}
| Js.Json.String("B") =>
if Js.Array.length(json_arr) != 1 {
Spice.error("Invalid number of arguments to variant constructor", v)
} else {
Belt.Result.Ok(B)
Ok(B)
}
| _ => Spice.error("Invalid variant constructor", Belt.Array.getExn(json_arr, 0))
}
Expand Down Expand Up @@ -59,16 +59,16 @@ let t1_decode = v =>
switch Spice.stringFromJson(
Belt.Option.getWithDefault(Js.Dict.get(dict, "a"), Js.Json.Null),
) {
| Belt.Result.Ok(a) =>
| Ok(a) =>
switch Spice.intFromJson(
Belt.Option.getWithDefault(Js.Dict.get(dict, "b"), Js.Json.null),
) {
| Belt.Result.Ok(b) => Belt.Result.Ok({a, b})
| Belt.Result.Error(e: Spice.decodeError) =>
Belt.Result.Error({...e, path: "." ++ "b" ++ e.path})
| Ok(b) => Ok({a, b})
| Error(e: Spice.decodeError) =>
Error({...e, path: "." ++ "b" ++ e.path})
}
| Belt.Result.Error(e: Spice.decodeError) =>
Belt.Result.Error({...e, path: "." ++ "a" ++ e.path})
| Error(e: Spice.decodeError) =>
Error({...e, path: "." ++ "a" ++ e.path})
}
| _ => Spice.error("Not an object", v)
}
Expand All @@ -83,11 +83,11 @@ let t2_decode = v =>
switch (json: Js.Json.t) {
| Js.Json.Array([v0, v1]) =>
switch (Spice.stringFromJson(v0), Spice.intFromJson(v1)) {
| (Belt.Result.Ok(v0), Belt.Result.Ok(v1)) => Belt.Result.Ok((v0, v1))
| (Belt.Result.Error(e: Spice.decodeError), _) =>
Belt.Result.Error({...e, path: "[0]" ++ e.path})
| (_, Belt.Result.Error(e: Spice.decodeError)) =>
Belt.Result.Error({...e, path: "[1]" ++ e.path})
| (Ok(v0), Ok(v1)) => Ok((v0, v1))
| (Error(e: Spice.decodeError), _) =>
Error({...e, path: "[0]" ++ e.path})
| (_, Error(e: Spice.decodeError)) =>
Error({...e, path: "[1]" ++ e.path})
}
| Js.Json.Array(_) => Spice.error("Incorrect cardinality", json)
| _ => Spice.error("Not a tuple", json)
Expand Down
2 changes: 1 addition & 1 deletion examples/src/Variants.res
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let data = %raw(`
}
`)

let user: Belt.Result.t<user, Spice.decodeError> = data->user_decode
let user: result<user, Spice.decodeError> = data->user_decode

let json: Js.Json.t = user->Result.getExn->user_encode

Expand Down
2 changes: 1 addition & 1 deletion examples/src/Variants2.res
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let data = %raw(`
}
`)

let user: Belt.Result.t<user, Spice.decodeError> = data->user_decode
let user: result<user, Spice.decodeError> = data->user_decode

let json: Js.Json.t = user->Result.getExn->user_encode

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@greenlabs/ppx-spice",
"version": "0.2.1-rc.0",
"version": "0.2.1-rc.1",
"description": "ReScript PPX which generate JSON (de)serializer",
"license": "MIT",
"author": "Greenlabs Dev <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion src/ppx/curried/codecs_curried.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ and generate_constr_codecs { do_encode; do_decode }
if do_decode then Some [%expr Spice.dictFromJson] else None )
| Ldot (Ldot (Lident "Js", "Json"), "t") ->
( (if do_encode then Some [%expr fun v -> v] else None),
if do_decode then Some [%expr fun v -> Belt.Result.Ok v] else None )
if do_decode then Some [%expr fun v -> Ok v] else None )
| Lident s ->
( (if do_encode then Some (make_ident_expr (s ^ Utils.encoder_func_suffix))
else None),
Expand Down
8 changes: 4 additions & 4 deletions src/ppx/curried/polyvariants_curried.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ let generate_decode_success_case num_args constructor_name =
pc_lhs =
Array.init num_args (fun i ->
mknoloc ("v" ^ string_of_int i) |> Pat.var |> fun p ->
[%pat? Belt.Result.Ok [%p p]])
[%pat? Ok [%p p]])
|> Array.to_list
|> tuple_or_singleton Pat.tuple;
pc_guard = None;
Expand All @@ -86,7 +86,7 @@ let generate_decode_success_case num_args constructor_name =
|> tuple_or_singleton Exp.tuple
|> fun v ->
Some v |> Exp.variant constructor_name |> fun e ->
[%expr Belt.Result.Ok [%e e]] );
[%expr Ok [%e e]] );
}

let generate_arg_decoder generator_settings args constructor_name =
Expand Down Expand Up @@ -122,7 +122,7 @@ let generate_decoder_case generator_settings { prf_desc } =
match args with
| [] ->
let resultant_exp = Exp.variant txt None in
[%expr Belt.Result.Ok [%e resultant_exp]]
[%expr Ok [%e resultant_exp]]
| _ -> generate_arg_decoder generator_settings args txt
in

Expand Down Expand Up @@ -151,7 +151,7 @@ let generate_decoder_case_attr generator_settings row =
match args with
| [] ->
let resultant_exp = Exp.variant txt None in
[%expr Belt.Result.Ok [%e resultant_exp]]
[%expr Ok [%e resultant_exp]]
| _ -> generate_arg_decoder generator_settings args txt
in

Expand Down
10 changes: 5 additions & 5 deletions src/ppx/curried/records_curried.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,29 @@ let generate_dict_get { key; codecs = _, decoder; default } =
[%expr
Belt.Option.getWithDefault
(Belt.Option.map (Js.Dict.get dict [%e key]) [%e decoder])
(Belt.Result.Ok [%e default])]
(Ok [%e default])]
| None ->
[%expr
[%e decoder]
(Belt.Option.getWithDefault (Js.Dict.get dict [%e key]) Js.Json.null)]

let generate_error_case { key } =
{
pc_lhs = [%pat? Belt.Result.Error (e : Spice.decodeError)];
pc_lhs = [%pat? Error (e : Spice.decodeError)];
pc_guard = None;
pc_rhs = [%expr Belt.Result.Error { e with path = "." ^ [%e key] ^ e.path }];
pc_rhs = [%expr Error { e with path = "." ^ [%e key] ^ e.path }];
}

let generate_final_record_expr decls =
decls
|> List.map (fun { name; is_optional } ->
let attrs = if is_optional then [ Utils.attr_optional ] else [] in
(lid name, make_ident_expr ~attrs name))
|> fun l -> [%expr Belt.Result.Ok [%e Exp.record l None]]
|> fun l -> [%expr Ok [%e Exp.record l None]]

let generate_success_case { name } success_expr =
{
pc_lhs = (mknoloc name |> Pat.var |> fun p -> [%pat? Belt.Result.Ok [%p p]]);
pc_lhs = (mknoloc name |> Pat.var |> fun p -> [%pat? Ok [%p p]]);
pc_guard = None;
pc_rhs = success_expr;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ppx/curried/signature_curried.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let rec add_encoder_params param_names result_type =
|> add_encoder_params tl

let make_result_type value_type =
[%type: ([%t value_type], Spice.decodeError) Belt.Result.t]
[%type: ([%t value_type], Spice.decodeError) result]

let rec add_decoder_params param_names result_type =
match param_names with
Expand Down
4 changes: 2 additions & 2 deletions src/ppx/curried/tuple_curried.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ let generate_decode_success_case num_args =
pc_lhs =
Array.init num_args (fun i ->
mknoloc ("v" ^ string_of_int i) |> Pat.var |> fun p ->
[%pat? Belt.Result.Ok [%p p]])
[%pat? Ok [%p p]])
|> Array.to_list
|> tuple_or_singleton Pat.tuple;
pc_guard = None;
pc_rhs =
( Array.init num_args (fun i -> make_ident_expr ("v" ^ string_of_int i))
|> Array.to_list |> Exp.tuple
|> fun e -> [%expr Belt.Result.Ok [%e e]] );
|> fun e -> [%expr Ok [%e e]] );
}

let generate_decode_switch composite_decoders =
Expand Down
8 changes: 4 additions & 4 deletions src/ppx/curried/variants_curried.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ let generate_decode_success_case num_args constructor_name =
pc_lhs =
Array.init num_args (fun i ->
mknoloc ("v" ^ string_of_int i) |> Pat.var |> fun p ->
[%pat? Belt.Result.Ok [%p p]])
[%pat? Ok [%p p]])
|> Array.to_list
|> tuple_or_singleton Pat.tuple;
pc_guard = None;
Expand All @@ -64,7 +64,7 @@ let generate_decode_success_case num_args constructor_name =
|> tuple_or_singleton Exp.tuple
|> fun v ->
Some v |> Exp.construct (lid constructor_name) |> fun e ->
[%expr Belt.Result.Ok [%e e]] );
[%expr Ok [%e e]] );
}

let generate_arg_decoder generator_settings args constructor_name =
Expand Down Expand Up @@ -100,7 +100,7 @@ let generate_decoder_case generator_settings
match args with
| [] ->
let ident = lid name in
[%expr Belt.Result.Ok [%e Exp.construct ident None]]
[%expr Ok [%e Exp.construct ident None]]
| _ -> generate_arg_decoder generator_settings args name
in

Expand All @@ -126,7 +126,7 @@ let generate_decoder_case_attr generator_settings
match args with
| [] ->
let ident = lid name in
[%expr Belt.Result.Ok [%e Exp.construct ident None]]
[%expr Ok [%e Exp.construct ident None]]
| _ -> generate_arg_decoder generator_settings args name
in

Expand Down
4 changes: 2 additions & 2 deletions src/ppx/decode_cases.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ let generate_error_case numArgs i _ =
pc_lhs =
Array.init numArgs (fun which ->
match which == i with
| true -> [%pat? Belt.Result.Error (e : Spice.decodeError)]
| true -> [%pat? Error (e : Spice.decodeError)]
| false -> [%pat? _])
|> Array.to_list
|> tuple_or_singleton Pat.tuple;
pc_guard = None;
pc_rhs =
[%expr Belt.Result.Error { e with path = [%e index_const i] ^ e.path }];
[%expr Error { e with path = [%e index_const i] ^ e.path }];
}
2 changes: 1 addition & 1 deletion src/ppx/uncurried/codecs_uncurried.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ and generate_constr_codecs { do_encode; do_decode }
( (if do_encode then Some (Utils.expr_func ~arity:1 [%expr fun v -> v])
else None),
if do_decode then
Some (Utils.expr_func ~arity:1 [%expr fun v -> Belt.Result.Ok v])
Some (Utils.expr_func ~arity:1 [%expr fun v -> Ok v])
else None )
| Lident s ->
( (if do_encode then Some (make_ident_expr (s ^ Utils.encoder_func_suffix))
Expand Down
8 changes: 4 additions & 4 deletions src/ppx/uncurried/polyvariants_uncurried.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ let generate_decode_success_case num_args constructor_name =
pc_lhs =
Array.init num_args (fun i ->
mknoloc ("v" ^ string_of_int i) |> Pat.var |> fun p ->
[%pat? Belt.Result.Ok [%p p]])
[%pat? Ok [%p p]])
|> Array.to_list
|> tuple_or_singleton Pat.tuple;
pc_guard = None;
Expand All @@ -86,7 +86,7 @@ let generate_decode_success_case num_args constructor_name =
|> tuple_or_singleton Exp.tuple
|> fun v ->
Some v |> Exp.variant constructor_name |> fun e ->
[%expr Belt.Result.Ok [%e e]] );
[%expr Ok [%e e]] );
}

let generate_arg_decoder generator_settings args constructor_name =
Expand Down Expand Up @@ -122,7 +122,7 @@ let generate_decoder_case generator_settings { prf_desc } =
match args with
| [] ->
let resultant_exp = Exp.variant txt None in
[%expr Belt.Result.Ok [%e resultant_exp]]
[%expr Ok [%e resultant_exp]]
| _ -> generate_arg_decoder generator_settings args txt
in

Expand Down Expand Up @@ -151,7 +151,7 @@ let generate_decoder_case_attr generator_settings row =
match args with
| [] ->
let resultant_exp = Exp.variant txt None in
[%expr Belt.Result.Ok [%e resultant_exp]]
[%expr Ok [%e resultant_exp]]
| _ -> generate_arg_decoder generator_settings args txt
in

Expand Down
10 changes: 5 additions & 5 deletions src/ppx/uncurried/records_uncurried.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ let generate_dict_get { key; codecs = _, decoder; default } =
[%expr
Belt.Option.getWithDefault
(Belt.Option.map (Js.Dict.get dict [%e key]) [%e decoder])
(Belt.Result.Ok [%e default]) [@res.uapp]]
(Ok [%e default]) [@res.uapp]]
| None ->
[%expr
[%e decoder]
Expand All @@ -59,21 +59,21 @@ let generate_dict_get { key; codecs = _, decoder; default } =

let generate_error_case { key } =
{
pc_lhs = [%pat? Belt.Result.Error (e : Spice.decodeError)];
pc_lhs = [%pat? Error (e : Spice.decodeError)];
pc_guard = None;
pc_rhs = [%expr Belt.Result.Error { e with path = "." ^ [%e key] ^ e.path }];
pc_rhs = [%expr Error { e with path = "." ^ [%e key] ^ e.path }];
}

let generate_final_record_expr decls =
decls
|> List.map (fun { name; is_optional } ->
let attrs = if is_optional then [ Utils.attr_optional ] else [] in
(lid name, make_ident_expr ~attrs name))
|> fun l -> [%expr Belt.Result.Ok [%e Exp.record l None]]
|> fun l -> [%expr Ok [%e Exp.record l None]]

let generate_success_case { name } success_expr =
{
pc_lhs = (mknoloc name |> Pat.var |> fun p -> [%pat? Belt.Result.Ok [%p p]]);
pc_lhs = (mknoloc name |> Pat.var |> fun p -> [%pat? Ok [%p p]]);
pc_guard = None;
pc_rhs = success_expr;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ppx/uncurried/signature_uncurried.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let rec add_encoder_params param_names result_type =
|> Utils.ctyp_arrow ~arity:1 |> add_encoder_params tl

let make_result_type value_type =
[%type: ([%t value_type], Spice.decodeError) Belt.Result.t]
[%type: ([%t value_type], Spice.decodeError) result]

let rec add_decoder_params param_names result_type =
match param_names with
Expand Down
4 changes: 2 additions & 2 deletions src/ppx/uncurried/tuple_uncurried.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ let generate_decode_success_case num_args =
pc_lhs =
Array.init num_args (fun i ->
mknoloc ("v" ^ string_of_int i) |> Pat.var |> fun p ->
[%pat? Belt.Result.Ok [%p p]])
[%pat? Ok [%p p]])
|> Array.to_list
|> tuple_or_singleton Pat.tuple;
pc_guard = None;
pc_rhs =
( Array.init num_args (fun i -> make_ident_expr ("v" ^ string_of_int i))
|> Array.to_list |> Exp.tuple
|> fun e -> [%expr Belt.Result.Ok [%e e]] );
|> fun e -> [%expr Ok [%e e]] );
}

let generate_decode_switch composite_decoders =
Expand Down
Loading

0 comments on commit 6066c7c

Please sign in to comment.