Skip to content

Commit

Permalink
Upgrade Kxclib and use Json_ext.to/of_xjv (#404)
Browse files Browse the repository at this point in the history
* upgrade kxclib to latest main

* fix code to build successfully

* update example/ex01/test/test.t

* update jsoo_full_bridge to use Kxclib_js

* gen dune inc files

* regenerate examples

---------

Co-authored-by: Haochen M. Kotoi-Xie <[email protected]>
  • Loading branch information
kxc-wraikny and haochenx authored Jan 10, 2024
1 parent 49ce37a commit b81ff79
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 87 deletions.
4 changes: 2 additions & 2 deletions example/ex01/test/test.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ Check endpoints
> -d '{"kind":"student","studentId":0}'
HTTP/1.1 400 Status 400
Content-Type: application/json
content-length: 764
content-length: 1611

"Bad request: invalid json format - mandatory field 'name' does not exist at root; expected shape: `with_warning (\n(\"not considering any config if exists\",\n `named ((\"Person\",\n `anyone_of ([`object_of ([`mandatory_field ((\"kind\", `exactly (\"anonymous\")))]);\n`object_of ([`mandatory_field ((\"kind\", `exactly (\"with-id\"))); `mandatory_field ((\"value\", `integral))]);\n`object_of ([`mandatory_field ((\"kind\", `exactly (\"student\"))); `mandatory_field ((\"studentId\", `integral));\n`mandatory_field ((\"name\", `string))]);\n`object_of ([`mandatory_field ((\"kind\", `exactly (\"teacher\"))); `mandatory_field ((\"facultyId\", `integral));\n`mandatory_field ((\"name\", `string));\n`mandatory_field ((\"department\", `string))])\n])))))"
"Bad request: invalid json format - mandatory field 'name' does not exist at root; expected shape: `with_warning (\n(\"not considering any config if exists\",\n `named ((\"Person\",\n `anyone_of ([`object_of ([`mandatory_field ((\"kind\",\n `exactly (\"anonymous\")))\n ]);\n `object_of ([`mandatory_field ((\"kind\",\n `exactly (\"with-id\")));\n `mandatory_field ((\"value\", `integral))\n ]);\n `object_of ([`mandatory_field ((\"kind\",\n `exactly (\"student\")));\n `mandatory_field ((\"studentId\",\n `integral));\n `mandatory_field ((\"name\", `string))]);\n `object_of ([`mandatory_field ((\"kind\",\n `exactly (\"teacher\")));\n `mandatory_field ((\"facultyId\",\n `integral));\n `mandatory_field ((\"name\", `string));\n `mandatory_field ((\"department\",\n `string))\n ])\n ])))))"

Kill the server process in the background.
$ kill -9 $PID
5 changes: 2 additions & 3 deletions example/for_dev/generator/duneinc_generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,9 @@ let () =
"bindoj.objintf_shared";
"bindoj_objintf_gen_jsoo_test_gen_utils";
"js_of_ocaml";
"yojson";
"kxclib.js";
objintf_gen_lib_name;
]
~preprocess:[ "(pps js_of_ocaml-ppx)" ];
];
many (
let outputs = [
fmt "%s_jsoo_gen.ml";
Expand Down
3 changes: 1 addition & 2 deletions example/for_dev/objintf_examples-dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
(subdir objintf_examples/lib_objintf_gen_jsoo
(library
(name dev_example_objintf_examples_lib_objintf_gen_jsoo)
(preprocess (pps js_of_ocaml-ppx))
(modules
ex_objintf_system_io_jsoo_gen
ex_objintf_empty_jsoo_gen
Expand All @@ -92,7 +91,7 @@
bindoj.objintf_shared
bindoj_objintf_gen_jsoo_test_gen_utils
js_of_ocaml
yojson
kxclib.js
dev_example_objintf_examples_lib_objintf_gen))
(rule
(alias gen)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,20 +348,11 @@ module Full_bridge_with_jsoo : Peer_setup_only_full_bridge =
Unsafe.obj
[|("student",
(Unsafe.inject
((let open Js_of_ocaml in
fun x ->
((((ex_record_student_to_json
x) |>
Kxclib.Json.to_yojson)
|>
Yojson.Safe.to_string)
|>
Js.string)
|>
(fun x ->
(Js._JSON
## parse)
x))
((fun x ->
(ex_record_student_to_json
x)
|>
Kxclib_js.Json_ext.to_xjv)
(student |>
(Option.value
~default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,25 +304,18 @@ module Full_bridge_with_jsoo : Endemic_setup_only_full_bridge = functor (M :
(fun la ->
Optdef.map
(Unsafe.get la "student")
(let open Js_of_ocaml in
fun x ->
((((((Js._JSON ##
stringify) x)
|> to_string)
|>
Yojson.Safe.from_string)
|>
Kxclib.Json.of_yojson)
|>
ex_record_student_of_json')
|>
(function
| Error e ->
failwith
(Bindoj_runtime.OfJsonResult.Err.to_string
e)
| Ok result ->
result))))
(fun x ->
((Kxclib_js.Json_ext.of_xjv
x)
|>
ex_record_student_of_json')
|>
(function
| Error e ->
failwith
(Bindoj_runtime.OfJsonResult.Err.to_string
e)
| Ok result -> result))))
|> Optdef.to_option)
|>
(Option.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,9 @@ module Full_bridge_with_jsoo : Dual_setup_full_bridge = functor (M :
(__caml_obj : byte_source') =
Js_of_ocaml.Js.Unsafe.callback
(fun labeledArgs ->
(let open Js_of_ocaml in
fun x ->
((((byte_source_state_to_json x) |>
Kxclib.Json.to_yojson)
|> Yojson.Safe.to_string)
|> Js.string)
|>
(fun x -> (Js._JSON ## parse) x))
(fun x ->
(byte_source_state_to_json x) |>
Kxclib_js.Json_ext.to_xjv)
(__caml_obj
?max:(let open Js_of_ocaml.Js in
(Optdef.bind labeledArgs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,20 +417,16 @@ module Full_bridge_with_jsoo : Dual_setup_full_bridge = functor (M :
let rec decode_byte_source'_of_js __js_obj ?max
() =
let open Js_of_ocaml in
(let open Js_of_ocaml in
fun x ->
((((((Js._JSON ## stringify) x) |>
to_string)
|> Yojson.Safe.from_string)
|> Kxclib.Json.of_yojson)
|> byte_source_state_of_json')
|>
(function
| Error e ->
failwith
(Bindoj_runtime.OfJsonResult.Err.to_string
e)
| Ok result -> result))
(fun x ->
((Kxclib_js.Json_ext.of_xjv x) |>
byte_source_state_of_json')
|>
(function
| Error e ->
failwith
(Bindoj_runtime.OfJsonResult.Err.to_string
e)
| Ok result -> result))
(Js.Unsafe.fun_call __js_obj
[|(Js.Unsafe.inject
(let open Js_of_ocaml.Js in
Expand Down
14 changes: 4 additions & 10 deletions src/lib_objintf_gen_jsoo/jsoo_full_bridge.ml
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,9 @@ let jsoo_full_bridge_impl : type bridgeable_ident.
| _ -> codec
in
let open_, name = Json_config.get_encoder_name ~resolution_strategy:codec td_name in
[%expr Js_of_ocaml.(fun x ->
[%expr (fun x ->
[%e evar ?open_ name] x
|> Kxclib.Json.to_yojson
|> Yojson.Safe.to_string
|> Js.string
|> (fun x -> Js._JSON##parse x)
|> Kxclib_js.Json_ext.to_xjv
)]
| `bridgeable (party, bi) ->
bi
Expand Down Expand Up @@ -494,11 +491,8 @@ let jsoo_full_bridge_impl : type bridgeable_ident.
| _ -> codec
in
let open_, name = Json_config.get_decoder_name ~resolution_strategy:codec td_name in
[%expr Js_of_ocaml.(fun x ->
Js._JSON##stringify x
|> to_string
|> Yojson.Safe.from_string
|> Kxclib.Json.of_yojson
[%expr (fun x ->
Kxclib_js.Json_ext.of_xjv x
|> [%e evar ?open_ name]
|> (function
| Error e -> failwith (Bindoj_runtime.OfJsonResult.Err.to_string e)
Expand Down
5 changes: 2 additions & 3 deletions src/lib_objintf_gen_jsoo/unit_test/gen/duneinc_generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ let () =
| `output_full_bridge_only -> [ "bindoj_objintf_gen_test_gen_output" ]
) @ [
"kxclib";
"yojson";
"kxclib.js";
"js_of_ocaml";
])
~preprocess:[ "(pps js_of_ocaml-ppx)" ];
]);
many caml_module
]
]
Expand Down
3 changes: 1 addition & 2 deletions src/lib_objintf_gen_jsoo/unit_test/gen/output-dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
(subdir output
(library
(name bindoj_objintf_gen_jsoo_test_gen_output)
(preprocess (pps js_of_ocaml-ppx))
(modules
ex_objintf_system_io_jsoo_gen
ex_objintf_empty_jsoo_gen
Expand All @@ -17,7 +16,7 @@
bindoj.std_runtime
bindoj.objintf_shared
kxclib
yojson
kxclib.js
js_of_ocaml))
(rule
(alias gen)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
(subdir output_full_bridge_only
(library
(name bindoj_objintf_gen_jsoo_test_gen_output_full_bridge_only)
(preprocess (pps js_of_ocaml-ppx))
(modules
ex_objintf_system_io_jsoo_gen
ex_objintf_empty_jsoo_gen
Expand All @@ -18,7 +17,7 @@
bindoj.objintf_shared
bindoj_objintf_gen_test_gen_output
kxclib
yojson
kxclib.js
js_of_ocaml))
(rule
(alias gen)
Expand Down
4 changes: 2 additions & 2 deletions src/lib_typedesc/unit_test/coretypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ module TestTup2(M0 : Testable) (M1 : Testable) : Testable = struct
let testable = Alcotest.pair M0.testable M1.testable
let to_string (a0, a1) = sprintf "(%s, %s)" (M0.to_string a0) (M1.to_string a1)
let values =
let open MonadOps(List0) in
let open MonadOps(List) in
M0.values
>>= (fun v0 ->
M1.values
Expand All @@ -176,7 +176,7 @@ module TestTup3(M0 : Testable) (M1 : Testable) (M2 : Testable) : Testable = stru
let testable = Alcotest.triple M0.testable M1.testable M2.testable
let to_string (a0, a1, a2) = sprintf "(%s, %s, %s)" (M0.to_string a0) (M1.to_string a1) (M2.to_string a2)
let values =
let open MonadOps(List0) in
let open MonadOps(List) in
M0.values
>>= (fun v0 ->
M1.values
Expand Down
2 changes: 1 addition & 1 deletion with_js/apidir-typescript-tests/gen/dune
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
bindoj_apidir_shared bindoj_apidir_runtime bindoj_apidir_typescript
bindoj_test_common_jsoo_utils
js_of_ocaml prr
kxclib kxclib.jsoo)
kxclib kxclib-jsoo kxclib.js)
(preprocess (pps js_of_ocaml-ppx))
(flags (:standard -open Kxclib)))

Expand Down
4 changes: 2 additions & 2 deletions with_js/apidir-typescript-tests/gen/gen_mock_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ let export_to_js (module Dir : Sample_apidir) =
let postprocess resp =
let open FutexnIoOps in
resp >|= prepare_response_for_js
>|= cast % Kxclib_jsoo.Json_ext.to_xjv
>|= cast % Kxclib_js.Json_ext.to_xjv
|> FutexnIo.to_promise in
object%js
method handle_path_json_get_js path =
Expand All @@ -65,7 +65,7 @@ let export_to_js (module Dir : Sample_apidir) =
method handle_path_json_post_js path reqbody =
let path = ostr path in
let reqbody : Json.jv =
reqbody |> Kxclib_jsoo.Json_ext.of_xjv |> cast in
reqbody |> Kxclib_js.Json_ext.of_xjv |> cast in
Bridge.handle_path_json_post path reqbody
|> postprocess
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
bindoj_runtime
bindoj_typedesc
bindoj_test_common_objintf_examples
kxclib kxclib.jsoo yojson jsonm js_of_ocaml prr)
kxclib kxclib-jsoo yojson jsonm js_of_ocaml prr)
(flags (:standard -open Kxclib))
(preprocess (pps ppx_deriving.show js_of_ocaml-ppx))
(modes js))
Expand Down
2 changes: 1 addition & 1 deletion with_js/objintf-tests/tests/dune
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
bindoj_runtime
bindoj_typedesc
bindoj_objintf_gen_jsoo_test_gen_output
kxclib kxclib.jsoo yojson jsonm js_of_ocaml prr)
kxclib kxclib-jsoo yojson jsonm js_of_ocaml prr)
(flags (:standard -open Kxclib))
(preprocess (pps ppx_deriving.show js_of_ocaml-ppx))
(modes js))

0 comments on commit b81ff79

Please sign in to comment.