Skip to content

Commit

Permalink
allow spreading of record fields and variant arguments in json encodi…
Browse files Browse the repository at this point in the history
…ng #354 (#355)

* update type_desc

* update test_common

* update example

* update json_config

* update src/lib_codec/json.ml

* update lib_gen/caml_datatype.ml

* add codec to `nested

* fix nested_to_coretype of lib_codec/json.ml

* fix indent of lib_codec/json.ml

* update lib_gen/utils.ml

* make implementation of explain_encoded_json_shape common.

* update gen_json_encoder with nested type

* fix fold_coretypes in type_desc.ml

* add collect_nested_types to json_codec.ml

* update gen_json_encoder

* update implementation of gen_json_decoder_impl

* add validate_spreading_type to lib_codec

* fix base_mangling_style of lib_codec

* fix validate_spreading_type signature

* fix explain_encoded_json_shape

* fix imple of gen_encoder/decoder

* update gen_json_schema

* regenerate example files

* update impl of lib_gen_foreign and lib_gen_ts

* update tests

* fix gen_json_schema

* update docs

* update ocamlformat

* fix implementations

* fix implementation

* add ex15

* update Json_shape_explaner

* fix error message

* add comments

* fix gen_json_schema

* add ex16

* Move the expected json_shape_explanation to each typedesc example file.

* update caml_datatype

* update doc

* fix ex01.ml

* fix ex16 sample_values

* fix gen_json_encoder

* add samples to of_json_error_examples

* fix of_json_error_examples

* add check_field_name_collision

* fix of_json_error_examples

* fux check_field_name_collition

* update to_refl

* Update src/lib_codec/config/json_config.mli

---------

Co-authored-by: Haochen M. Kotoi-Xie <[email protected]>
  • Loading branch information
kxc-wraikny and haochenx authored Jul 26, 2023
1 parent 810bbbe commit 2508cae
Show file tree
Hide file tree
Showing 65 changed files with 3,686 additions and 1,772 deletions.
2 changes: 1 addition & 1 deletion .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
profile = default
version = 0.25.1
version = 0.26.0
2 changes: 1 addition & 1 deletion bindoj.opam
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ depends: [
"ppx_inline_test" {with-test}
"lwt" {with-test}
"uri" {>= "4.2.0"}
"ocamlformat" {with-test & >= "0.25.1"}
"ocamlformat" {with-test & = "0.26.0"}
"ocaml" {>= "4.12"}
"cohttp-lwt-unix" {with-test}
]
Expand Down
60 changes: 35 additions & 25 deletions doc/tests_src/ocaml_datatype_basic.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ val student_desc : type_decl =
Record_decl
[{rf_name = "admission_year";
rf_type =
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `int;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
`direct
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `int;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
rf_configs = Bindoj.Type_desc.Configs.[]; rf_doc = `nodoc};
{rf_name = "full_name";
rf_type =
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `string;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
`direct
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `string;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
rf_configs = Bindoj.Type_desc.Configs.[]; rf_doc = `nodoc}];
td_doc = `nodoc}
```
Expand All @@ -80,7 +82,7 @@ type nonrec type_decl =
# let person_desc =
variant_decl "person" [
variant_constructor "Anonymous" `no_param;
variant_constructor "With_id" (`tuple_like [Coretype.mk_prim `int]);
variant_constructor "With_id" (`tuple_like [variant_argument @@ Coretype.mk_prim `int]);
variant_constructor "Student" (`inline_record [
record_field "student_id" (Coretype.mk_prim `int);
record_field "name" (Coretype.mk_prim `string);
Expand All @@ -100,46 +102,54 @@ val person_desc : type_decl =
{vc_name = "With_id";
vc_param =
`tuple_like
[{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `int;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]}];
[{va_type =
`direct
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `int;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
va_configs = Bindoj.Type_desc.Configs.[]; va_doc = `nodoc}];
vc_configs = Bindoj.Type_desc.Configs.[]; vc_doc = `nodoc};
{vc_name = "Student";
vc_param =
`inline_record
[{rf_name = "student_id";
rf_type =
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `int;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
`direct
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `int;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
rf_configs = Bindoj.Type_desc.Configs.[]; rf_doc = `nodoc};
{rf_name = "name";
rf_type =
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `string;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
`direct
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `string;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
rf_configs = Bindoj.Type_desc.Configs.[]; rf_doc = `nodoc}];
vc_configs = Bindoj.Type_desc.Configs.[]; vc_doc = `nodoc};
{vc_name = "Teacher";
vc_param =
`inline_record
[{rf_name = "faculty_id";
rf_type =
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `int;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
`direct
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `int;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
rf_configs = Bindoj.Type_desc.Configs.[]; rf_doc = `nodoc};
{rf_name = "name";
rf_type =
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `string;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
`direct
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `string;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
rf_configs = Bindoj.Type_desc.Configs.[]; rf_doc = `nodoc};
{rf_name = "department";
rf_type =
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `string;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
`direct
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `string;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
rf_configs = Bindoj.Type_desc.Configs.[]; rf_doc = `nodoc}];
vc_configs = Bindoj.Type_desc.Configs.[]; vc_doc = `nodoc}];
td_doc = `nodoc}
Expand Down
22 changes: 14 additions & 8 deletions doc/tests_src/ocaml_datatype_polymorphic-variant.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ AnchorZ Inc. to satisfy its needs in its product development workflow.
# let int_or_string_desc =
variant_decl "int_or_string" [
variant_constructor "int" (`tuple_like [Coretype.mk_prim `int]);
variant_constructor "string" (`tuple_like [Coretype.mk_prim `string]);
variant_constructor "int" (`tuple_like [variant_argument @@ Coretype.mk_prim `int]);
variant_constructor "string" (`tuple_like [variant_argument @@ Coretype.mk_prim `string]);
] ~configs:[
Caml_config.variant_type `polymorphic
];;
Expand All @@ -47,16 +47,22 @@ val int_or_string_desc : type_decl =
[{vc_name = "int";
vc_param =
`tuple_like
[{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `int;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]}];
[{va_type =
`direct
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `int;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
va_configs = Bindoj.Type_desc.Configs.[]; va_doc = `nodoc}];
vc_configs = Bindoj.Type_desc.Configs.[]; vc_doc = `nodoc};
{vc_name = "string";
vc_param =
`tuple_like
[{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `string;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]}];
[{va_type =
`direct
{Bindoj.Type_desc.Coretype.ct_desc =
Bindoj.Type_desc.Coretype.Prim `string;
ct_configs = Bindoj_typedesc.Type_desc.Configs.[]};
va_configs = Bindoj.Type_desc.Configs.[]; va_doc = `nodoc}];
vc_configs = Bindoj.Type_desc.Configs.[]; vc_doc = `nodoc}];
td_doc = `nodoc}
Expand Down
Loading

0 comments on commit 2508cae

Please sign in to comment.