Skip to content

Commit

Permalink
Update typescript optional field (#362)
Browse files Browse the repository at this point in the history
* update tests

* update implementation

* update docs

* regenerate example

* add examples (ex17, ex18)

* Fixed to not generate unused option_to_json

* fix lib_gen_ts

* fix error message in test

* add sample_values to ex18

* update error message of apidir_srever_bridge

* update copyright

* update fold_coretypes'

* fix error message

* fix signature of internal functions

* add more docs

* fix ancestral_configs

* add rope tests with tsps_optional=true

---------

Co-authored-by: Haochen M. Kotoi-Xie <[email protected]>
  • Loading branch information
kxc-wraikny and haochenx authored Aug 21, 2023
1 parent d821b5a commit 3d15f55
Show file tree
Hide file tree
Showing 56 changed files with 1,746 additions and 948 deletions.
68 changes: 23 additions & 45 deletions doc/tests_src/ocaml_json_codec.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,16 @@ let student_of_json' =
| `obj param ->
let (>>=) = Result.bind in
(((List.assoc_opt "admissionYear" param) |>
(function
| Some a -> Ok a
| None ->
Error
("mandatory field 'admissionYear' does not exist",
path)))
(Option.to_result
~none:("mandatory field 'admissionYear' does not exist",
path)))
>>= (int_of_json' ((`f "admissionYear") :: path)))
>>=
((fun x0 ->
(((List.assoc_opt "fullName" param) |>
(function
| Some a -> Ok a
| None ->
Error
("mandatory field 'fullName' does not exist",
path)))
(Option.to_result
~none:("mandatory field 'fullName' does not exist",
path)))
>>= (string_of_json' ((`f "fullName") :: path)))
>>=
(fun x1 -> Ok { admission_year = x0; full_name = x1 })))
Expand Down Expand Up @@ -221,7 +215,7 @@ let student_of_json' =
let person_to_json =
(function
| Anonymous -> `obj [("kind", (`str "anonymous"))]
| With_id (x0) ->
| With_id x0 ->
`obj [("kind", (`str "with-id")); ("arg", (int_to_json x0))]
| Student { student_id = x0; name = x1 } ->
`obj
Expand Down Expand Up @@ -256,54 +250,39 @@ let person_of_json' =
| `obj (("kind", `str "student")::param) ->
let (>>=) = Result.bind in
(((List.assoc_opt "studentId" param) |>
(function
| Some a -> Ok a
| None ->
Error
("mandatory field 'studentId' does not exist",
path)))
(Option.to_result
~none:("mandatory field 'studentId' does not exist",
path)))
>>= (int_of_json' ((`f "studentId") :: path)))
>>=
((fun x0 ->
(((List.assoc_opt "name" param) |>
(function
| Some a -> Ok a
| None ->
Error
("mandatory field 'name' does not exist",
path)))
(Option.to_result
~none:("mandatory field 'name' does not exist",
path)))
>>= (string_of_json' ((`f "name") :: path)))
>>=
(fun x1 -> Ok (Student { student_id = x0; name = x1 }))))
| `obj (("kind", `str "teacher")::param) ->
let (>>=) = Result.bind in
(((List.assoc_opt "facultyId" param) |>
(function
| Some a -> Ok a
| None ->
Error
("mandatory field 'facultyId' does not exist",
path)))
(Option.to_result
~none:("mandatory field 'facultyId' does not exist",
path)))
>>= (int_of_json' ((`f "facultyId") :: path)))
>>=
((fun x0 ->
(((List.assoc_opt "name" param) |>
(function
| Some a -> Ok a
| None ->
Error
("mandatory field 'name' does not exist",
path)))
(Option.to_result
~none:("mandatory field 'name' does not exist",
path)))
>>= (string_of_json' ((`f "name") :: path)))
>>=
(fun x1 ->
(((List.assoc_opt "department" param) |>
(function
| Some a -> Ok a
| None ->
Error
("mandatory field 'department' does not exist",
path)))
(Option.to_result
~none:("mandatory field 'department' does not exist",
path)))
>>=
(string_of_json' ((`f "department") :: path)))
>>=
Expand Down Expand Up @@ -350,8 +329,7 @@ let person_of_json' =
`object_of
[`mandatory_field
("kind", (`exactly (`str "with-id")));
`mandatory_field
("arg", (`tuple_of [`integral]))];
`mandatory_field ("arg", `integral)];
`object_of
[`mandatory_field
("kind", (`exactly (`str "student")));
Expand Down
74 changes: 31 additions & 43 deletions example/ex01/generated/ex01.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ let (my_tuple_reflect : _ Bindoj_runtime.Refl.t) =
let my_tuple_json_shape_explanation =
(`with_warning
( "not considering any config if exists",
`named ("MyTuple", `tuple_of [ `proper_float; `string ]) )
`named
( "MyTuple",
`object_of
[
`mandatory_field ("_0", `proper_float);
`mandatory_field ("_1", `string);
] ) )
: Bindoj_runtime.json_shape_explanation)
[@@warning "-39"]

Expand Down Expand Up @@ -110,19 +116,17 @@ and my_tuple_of_json' =
in
fun path -> function
| (`obj fields : Kxclib.Json.jv) ->
let fields = Bindoj_runtime.StringMap.of_list fields in
let ( >>= ) = Result.bind in
(Bindoj_runtime.StringMap.find_opt "_0" fields |> function
| Some a -> Ok a
| None -> Error ("mandatory field '_0' does not exist", path))
List.assoc_opt "_0" fields
|> Option.to_result
~none:("mandatory field '_0' does not exist", path)
>>= float_of_json' (`f "_0" :: path)
>>= fun x0 ->
(Bindoj_runtime.StringMap.find_opt "_1" fields |> function
| Some a -> Ok a
| None -> Error ("mandatory field '_1' does not exist", path))
>>= fun x1 ->
let ( >>= ) = Result.bind in
float_of_json' (`f "_0" :: path) x0 >>= fun x0 ->
string_of_json' (`f "_1" :: path) x1 >>= fun x1 -> Ok (x0, x1)
List.assoc_opt "_1" fields
|> Option.to_result
~none:("mandatory field '_1' does not exist", path)
>>= string_of_json' (`f "_1" :: path)
>>= fun x1 -> Ok (x0, x1)
| jv ->
Error
( Printf.sprintf
Expand Down Expand Up @@ -225,18 +229,13 @@ and student_of_json' =
| `obj param ->
let ( >>= ) = Result.bind in
List.assoc_opt "admissionYear" param
|> (function
| Some a -> Ok a
| None ->
Error
("mandatory field 'admissionYear' does not exist", path))
|> Option.to_result
~none:("mandatory field 'admissionYear' does not exist", path)
>>= int_of_json' (`f "admissionYear" :: path)
>>= fun x0 ->
List.assoc_opt "name" param
|> (function
| Some a -> Ok a
| None ->
Error ("mandatory field 'name' does not exist", path))
|> Option.to_result
~none:("mandatory field 'name' does not exist", path)
>>= string_of_json' (`f "name" :: path)
>>= fun x1 -> Ok { admission_year = x0; name = x1 }
| jv ->
Expand Down Expand Up @@ -376,7 +375,7 @@ let person_json_shape_explanation =
`object_of
[
`mandatory_field ("kind", `exactly (`str "with-id"));
`mandatory_field ("arg", `tuple_of [ `integral ]);
`mandatory_field ("arg", `integral);
];
`object_of
[
Expand Down Expand Up @@ -459,41 +458,30 @@ and person_of_json' =
| `obj (("kind", `str "student") :: param) ->
let ( >>= ) = Result.bind in
List.assoc_opt "studentId" param
|> (function
| Some a -> Ok a
| None ->
Error ("mandatory field 'studentId' does not exist", path))
|> Option.to_result
~none:("mandatory field 'studentId' does not exist", path)
>>= int_of_json' (`f "studentId" :: path)
>>= fun x0 ->
List.assoc_opt "name" param
|> (function
| Some a -> Ok a
| None ->
Error ("mandatory field 'name' does not exist", path))
|> Option.to_result
~none:("mandatory field 'name' does not exist", path)
>>= string_of_json' (`f "name" :: path)
>>= fun x1 -> Ok (Student { student_id = x0; name = x1 })
| `obj (("kind", `str "teacher") :: param) ->
let ( >>= ) = Result.bind in
List.assoc_opt "facultyId" param
|> (function
| Some a -> Ok a
| None ->
Error ("mandatory field 'facultyId' does not exist", path))
|> Option.to_result
~none:("mandatory field 'facultyId' does not exist", path)
>>= int_of_json' (`f "facultyId" :: path)
>>= fun x0 ->
List.assoc_opt "name" param
|> (function
| Some a -> Ok a
| None ->
Error ("mandatory field 'name' does not exist", path))
|> Option.to_result
~none:("mandatory field 'name' does not exist", path)
>>= string_of_json' (`f "name" :: path)
>>= fun x1 ->
List.assoc_opt "department" param
|> (function
| Some a -> Ok a
| None ->
Error
("mandatory field 'department' does not exist", path))
|> Option.to_result
~none:("mandatory field 'department' does not exist", path)
>>= string_of_json' (`f "department" :: path)
>>= fun x2 ->
Ok (Teacher { faculty_id = x0; name = x1; department = x2 })
Expand Down
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: 768
content-length: 762

"Bad request: invalid json format - mandatory field 'name' does not exist; 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 ((\"arg\", `tuple_of ([`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\", `exactly (\"anonymous\")))]);\n`object_of ([`mandatory_field ((\"kind\", `exactly (\"with-id\"))); `mandatory_field ((\"arg\", `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])))))"

Kill the server process in the background.
$ kill -9 $PID
Loading

0 comments on commit 3d15f55

Please sign in to comment.