diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4fe09b3e..773324e0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,8 +15,9 @@ jobs: #- macos-latest #- windows-latest ocaml-compiler: - - 4.08.x - - 4.12.x + - '4.08' + - '4.14' + - '5.1' runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 @@ -25,6 +26,8 @@ jobs: - uses: ocaml/setup-ocaml@v2 with: ocaml-compiler: ${{ matrix.ocaml-compiler }} + allow-prerelease-opam: true + dune-cache: true #- run: sudo apt install protobuf-compiler libprotobuf-dev - run: opam pin -n . - run: opam depext -yt ocaml-protoc @@ -32,3 +35,6 @@ jobs: - run: opam exec -- dune build @install - run: opam exec -- dune runtest #- run: opam exec -- make integration + - run: opam install ocamlformat.0.24.1 + - run: opam exec -- dune build @fmt --auto-promote + - run: git diff -q diff --git a/benchs/bin/dune b/benchs/bin/dune index c3173dd5..9fb9214d 100644 --- a/benchs/bin/dune +++ b/benchs/bin/dune @@ -1,4 +1,3 @@ - (executable (name run) (ocamlopt_flags :standard -inline 100) diff --git a/benchs/dune b/benchs/dune index 207e4ef7..b710d38b 100644 --- a/benchs/dune +++ b/benchs/dune @@ -1,7 +1,10 @@ (executable (name benchs) (ocamlopt_flags :standard -inline 100) - (foreign_stubs (language c) (flags :standard -std=c99 -O2) (names stubs)) + (foreign_stubs + (language c) + (flags :standard -std=c99 -O2) + (names stubs)) (libraries ocaml-protoc benchmark)) (rule diff --git a/src/compilerlib/pb_codegen_decode_bs.mli b/src/compilerlib/pb_codegen_decode_bs.mli index 7c0a8a1c..3aa642c1 100644 --- a/src/compilerlib/pb_codegen_decode_bs.mli +++ b/src/compilerlib/pb_codegen_decode_bs.mli @@ -1,4 +1,3 @@ - include Pb_codegen_plugin.S val plugin : Pb_codegen_plugin.t diff --git a/src/compilerlib/pb_codegen_decode_yojson.ml b/src/compilerlib/pb_codegen_decode_yojson.ml index 2092c169..58477c4e 100644 --- a/src/compilerlib/pb_codegen_decode_yojson.ml +++ b/src/compilerlib/pb_codegen_decode_yojson.ml @@ -25,7 +25,7 @@ let field_pattern_match ~r_name ~rf_label field_type = | Ot.Bt_bool -> decode "bool" | Ot.Bt_bytes -> decode "bytes" in - ("json_value", exp) + "json_value", exp | Ot.Ft_unit -> "json_value", sp "Pbrt_yojson.unit json_value \"%s\" \"%s\"" r_name rf_label (* TODO Wrapper: add similar one for wrapper type (with different @@ -103,44 +103,42 @@ let gen_rft_assoc_field sc ~r_name ~rf_label ~assoc_type ~key_type ~value_type = let json_label = Pb_codegen_util.camel_case_of_label rf_label in F.linep sc "| (\"%s\", `Assoc assoc) ->" json_label; F.sub_scope sc (fun sc -> - let value_name, value_exp = field_pattern_match ~r_name ~rf_label value_type in - let key_name = "key" in - let key_exp = - match key_type with - | Ot.Bt_string -> "key" - | Ot.Bt_int -> "(Int.of_string key)" - | Ot.Bt_int32 -> "(Int32.of_string key)" - | Ot.Bt_int64 -> "(Int64.of_string key)" - | Ot.Bt_uint32 -> "(`unsigned (Int32.of_string key))" - | Ot.Bt_uint64 -> "(`unsigned (Int64.of_string key))" - | Ot.Bt_bool -> "(Bool.of_string key)" - | Ot.Bt_float -> - Printf.eprintf "float cannot be used as a map key type"; - exit 1 - | Ot.Bt_bytes -> - Printf.eprintf "bytes cannot be used as a map key type"; - exit 1 - in - F.line sc "let assoc ="; - F.sub_scope sc (fun sc -> - F.line sc "assoc"; - F.linep sc "|> List.map (fun (%s, %s) -> (%s, %s)) " - key_name - value_name - key_exp - value_exp; - F.line sc "|> List.to_seq"; - (* Passing through [Hashtbl.of_seq] even in the [At_list] case ensures that if there - is a repeated key we take the last value associated with it. *) - F.line sc "|> Hashtbl.of_seq"); - F.line sc "in"; - let assoc_exp = - match assoc_type with - | Ot.At_hashtable -> "assoc" - | Ot.At_list -> "assoc |> Hashtbl.to_seq |> List.of_seq" - in - F.linep sc "v.%s <- %s" rf_label assoc_exp); -;; + let value_name, value_exp = + field_pattern_match ~r_name ~rf_label value_type + in + let key_name = "key" in + let key_exp = + match key_type with + | Ot.Bt_string -> "key" + | Ot.Bt_int -> "(Int.of_string key)" + | Ot.Bt_int32 -> "(Int32.of_string key)" + | Ot.Bt_int64 -> "(Int64.of_string key)" + | Ot.Bt_uint32 -> "(`unsigned (Int32.of_string key))" + | Ot.Bt_uint64 -> "(`unsigned (Int64.of_string key))" + | Ot.Bt_bool -> "(Bool.of_string key)" + | Ot.Bt_float -> + Printf.eprintf "float cannot be used as a map key type"; + exit 1 + | Ot.Bt_bytes -> + Printf.eprintf "bytes cannot be used as a map key type"; + exit 1 + in + F.line sc "let assoc ="; + F.sub_scope sc (fun sc -> + F.line sc "assoc"; + F.linep sc "|> List.map (fun (%s, %s) -> (%s, %s)) " key_name + value_name key_exp value_exp; + F.line sc "|> List.to_seq"; + (* Passing through [Hashtbl.of_seq] even in the [At_list] case ensures that if there + is a repeated key we take the last value associated with it. *) + F.line sc "|> Hashtbl.of_seq"); + F.line sc "in"; + let assoc_exp = + match assoc_type with + | Ot.At_hashtable -> "assoc" + | Ot.At_list -> "assoc |> Hashtbl.to_seq |> List.of_seq" + in + F.linep sc "v.%s <- %s" rf_label assoc_exp) (* Generate decode function for a record *) let gen_record ?and_ { Ot.r_name; r_fields } sc = @@ -174,8 +172,10 @@ let gen_record ?and_ { Ot.r_name; r_fields } sc = | Ot.Rft_required _ -> Printf.eprintf "Only proto3 syntax supported in JSON encoding"; exit 1 - | Ot.Rft_associative (assoc_type, _, (key_type, _), (value_type, _)) -> - gen_rft_assoc_field sc ~r_name ~rf_label ~assoc_type ~key_type ~value_type) + | Ot.Rft_associative + (assoc_type, _, (key_type, _), (value_type, _)) -> + gen_rft_assoc_field sc ~r_name ~rf_label ~assoc_type ~key_type + ~value_type) r_fields; (* Unknown fields are simply ignored *) diff --git a/src/compilerlib/pb_codegen_encode_binary.ml b/src/compilerlib/pb_codegen_encode_binary.ml index 3e61409a..4d6e8e81 100644 --- a/src/compilerlib/pb_codegen_encode_binary.ml +++ b/src/compilerlib/pb_codegen_encode_binary.ml @@ -155,7 +155,7 @@ let gen_rft_variant sc var_name { Ot.v_constructors; _ } = gen_encode_field_type sc ~with_key:true "x" vc_encoding_number vc_payload_kind false field_type)) v_constructors; - F.line sc "| None -> ()"; + F.line sc "| None -> ()"; F.line sc "end;" let gen_rft_associative sc var_name associative_field = diff --git a/src/compilerlib/pb_codegen_encode_yojson.ml b/src/compilerlib/pb_codegen_encode_yojson.ml index 25ecce40..4fc51d52 100644 --- a/src/compilerlib/pb_codegen_encode_yojson.ml +++ b/src/compilerlib/pb_codegen_encode_yojson.ml @@ -149,13 +149,8 @@ let gen_rft_variant sc rf_label { Ot.v_constructors; _ } = F.linep sc "in (* match v.%s *)" rf_label -let gen_rft_assoc - sc - ~rf_label - ~assoc_type - ~key_type - ~value_field:(value_type, value_pk) - = +let gen_rft_assoc sc ~rf_label ~assoc_type ~key_type + ~value_field:(value_type, value_pk) = let var_name = sp "v.%s" rf_label in let json_label = Pb_codegen_util.camel_case_of_label rf_label in let key_pat, key_exp = @@ -177,44 +172,41 @@ let gen_rft_assoc let write_assoc_field ~fn ~var_name = F.line sc "let assoc_field ="; F.sub_scope sc (fun sc -> - F.linep sc "%s" var_name; - (match assoc_type with - | Ot.At_list -> () - | Ot.At_hashtable -> - F.line sc "|> Hashtbl.to_seq |> List.of_seq"); - F.linep sc "|> List.map (fun (%s, value) -> %s, %s value)" key_pat key_exp fn); - F.line sc "in"; + F.linep sc "%s" var_name; + (match assoc_type with + | Ot.At_list -> () + | Ot.At_hashtable -> F.line sc "|> Hashtbl.to_seq |> List.of_seq"); + F.linep sc "|> List.map (fun (%s, value) -> %s, %s value)" key_pat + key_exp fn); + F.line sc "in" in F.line sc "let assoc ="; F.sub_scope sc (fun sc -> - (match value_type with - | Ot.Ft_unit -> unsupported json_label - | Ot.Ft_basic_type basic_type -> - let runtime_f, map_function = - runtime_function_for_basic_type json_label basic_type value_pk - in - (match map_function with + (match value_type with + | Ot.Ft_unit -> unsupported json_label + | Ot.Ft_basic_type basic_type -> + let runtime_f, map_function = + runtime_function_for_basic_type json_label basic_type value_pk + in + (match map_function with | None -> write_assoc_field ~fn:("Pbrt_yojson." ^ runtime_f) ~var_name | Some map_function -> let fn = - Printf.sprintf - "(fun value -> value |> %s |> Pbrt_yojson.%s)" - map_function - runtime_f + Printf.sprintf "(fun value -> value |> %s |> Pbrt_yojson.%s)" + map_function runtime_f in write_assoc_field ~fn ~var_name) - (* TODO Wrapper: add similar case for Ft_wrapper_type *) - (* User defined *) - | Ot.Ft_user_defined_type udt -> - let fn = - let function_prefix = "encode_json" in - Pb_codegen_util.function_name_of_user_defined ~function_prefix udt - in - write_assoc_field ~fn ~var_name; - | _ -> unsupported json_label); - F.linep sc "(\"%s\", `Assoc assoc_field) :: assoc " json_label); + (* TODO Wrapper: add similar case for Ft_wrapper_type *) + (* User defined *) + | Ot.Ft_user_defined_type udt -> + let fn = + let function_prefix = "encode_json" in + Pb_codegen_util.function_name_of_user_defined ~function_prefix udt + in + write_assoc_field ~fn ~var_name + | _ -> unsupported json_label); + F.linep sc "(\"%s\", `Assoc assoc_field) :: assoc " json_label); F.line sc "in" -;; let gen_record ?and_ { Ot.r_name; r_fields } sc = let rn = r_name in diff --git a/src/compilerlib/pb_format_util.ml b/src/compilerlib/pb_format_util.ml index 78381fff..0c2a6885 100644 --- a/src/compilerlib/pb_format_util.ml +++ b/src/compilerlib/pb_format_util.ml @@ -1,6 +1,5 @@ open Format - (* Not available in 4.03 *) let pp_print_option ?(none = fun _ () -> ()) pp_v ppf = function | None -> none ppf () diff --git a/src/dune b/src/dune index f6dc452c..cc26033e 100644 --- a/src/dune +++ b/src/dune @@ -1,3 +1,3 @@ - (env - (_ (flags :standard -warn-error -a+8 -w +a-4-40-41-42-44-48-70))) + (_ + (flags :standard -warn-error -a+8 -w +a-4-40-41-42-44-48-70))) diff --git a/src/examples/dune b/src/examples/dune index 24d338af..7f33cbb5 100644 --- a/src/examples/dune +++ b/src/examples/dune @@ -119,14 +119,5 @@ (action (with-stdout-to dune.inc.gen - (run - %{gen-dune} - build_server - calculator - example01 - example03 - example04 - example05 - file_server - orgchart - oneof)))) + (run %{gen-dune} build_server calculator example01 example03 example04 + example05 file_server orgchart oneof)))) diff --git a/src/ocaml-protoc/dune b/src/ocaml-protoc/dune index 7204b55e..7c12257f 100644 --- a/src/ocaml-protoc/dune +++ b/src/ocaml-protoc/dune @@ -16,5 +16,5 @@ (re_export pbrt))) (documentation - (package ocaml-protoc) - (mld_files index)) + (package ocaml-protoc) + (mld_files index)) diff --git a/src/runtime-services/dune b/src/runtime-services/dune index 77e192ed..44170aab 100644 --- a/src/runtime-services/dune +++ b/src/runtime-services/dune @@ -1,4 +1,3 @@ - (library (name pbrt_services) (public_name pbrt_services) diff --git a/src/runtime-yojson/dune b/src/runtime-yojson/dune index c7e629dc..26710039 100644 --- a/src/runtime-yojson/dune +++ b/src/runtime-yojson/dune @@ -1,5 +1,6 @@ - (library - (public_name pbrt_yojson) - (wrapped false) - (libraries (re_export yojson) base64)) + (public_name pbrt_yojson) + (wrapped false) + (libraries + (re_export yojson) + base64)) diff --git a/src/runtime/dune b/src/runtime/dune index e79ebbf9..dbb53caa 100644 --- a/src/runtime/dune +++ b/src/runtime/dune @@ -2,7 +2,10 @@ (name pbrt) (public_name pbrt) (synopsis "Runtime library for ocaml-protoc") - (foreign_stubs (language c) (flags :standard -std=c99 -O2) (names stubs)) + (foreign_stubs + (language c) + (flags :standard -std=c99 -O2) + (names stubs)) ; we need to increase -inline, so that the varint encoder/decoder can ; be remembered by the inliner. (ocamlopt_flags :standard -inline 100)) diff --git a/src/tests/benchmark/dune b/src/tests/benchmark/dune index b52ecb90..e37b4e70 100644 --- a/src/tests/benchmark/dune +++ b/src/tests/benchmark/dune @@ -6,6 +6,5 @@ (executable (name benchmark_single_ml) - (modules benchmark_single_ml ocaml_test_types ocaml_test_runner - benchmark) + (modules benchmark_single_ml ocaml_test_types ocaml_test_runner benchmark) (libraries pbrt unix)) diff --git a/src/tests/expectation/tests.ml b/src/tests/expectation/tests.ml index f0cec1ce..d2875fd6 100644 --- a/src/tests/expectation/tests.ml +++ b/src/tests/expectation/tests.ml @@ -225,7 +225,7 @@ let test_cases = rpc GetShelfWithSemicolon (GetShelfRequest) returns (GetShelfResponse) {}; } - |} + |}; ] let () = List.iter run test_cases diff --git a/src/tests/google_unittest/dune b/src/tests/google_unittest/dune index 17582bdc..7cd6c25a 100644 --- a/src/tests/google_unittest/dune +++ b/src/tests/google_unittest/dune @@ -1,16 +1,20 @@ (test - (name google_unittest) - (flags :standard -w -11) - (package ocaml-protoc) - (libraries pbrt)) + (name google_unittest) + (flags :standard -w -11) + (package ocaml-protoc) + (libraries pbrt)) (rule - (targets unittest.ml unittest.mli) - (deps (:file unittest.proto) unittest_import.proto) - (action (run ocaml-protoc --ml_out . -I . %{file}))) + (targets unittest.ml unittest.mli) + (deps + (:file unittest.proto) + unittest_import.proto) + (action + (run ocaml-protoc --ml_out . -I . %{file}))) (rule - (targets unittest_import.ml unittest_import.mli) - (deps (:file unittest_import.proto)) - (action (run ocaml-protoc --ml_out . -I . %{file}))) - + (targets unittest_import.ml unittest_import.mli) + (deps + (:file unittest_import.proto)) + (action + (run ocaml-protoc --ml_out . -I . %{file}))) diff --git a/src/tests/integration-tests/dune b/src/tests/integration-tests/dune index dee780fc..344669d2 100644 --- a/src/tests/integration-tests/dune +++ b/src/tests/integration-tests/dune @@ -8,9 +8,9 @@ (targets test01.ml test01.mli) (deps test01.proto ../../include/ocaml-protoc/ocamloptions.proto) (action - (run ocaml-protoc -I ./ -I ../../include/ocaml-protoc -I %{env:INCLUDE=/usr/include/} - --binary --pp --ocaml_all_types_ppx "deriving show" --ml_out ./ ./ - test01.proto))) + (run ocaml-protoc -I ./ -I ../../include/ocaml-protoc -I + %{env:INCLUDE=/usr/include/} --binary --pp --ocaml_all_types_ppx + "deriving show" --ml_out ./ ./ test01.proto))) (executable (name test01_ml) @@ -161,8 +161,8 @@ (targets test15.ml test15.mli) (deps test15.proto ../../include/ocaml-protoc/ocamloptions.proto) (action - (run ocaml-protoc -I ../../include/ocaml-protoc -I %{env:INCLUDE=/usr/include/} - --binary --pp --ml_out ./ ./ test15.proto))) + (run ocaml-protoc -I ../../include/ocaml-protoc -I + %{env:INCLUDE=/usr/include/} --binary --pp --ml_out ./ ./ test15.proto))) (executable (name test15_ml) @@ -173,8 +173,8 @@ (targets test16.ml test16.mli) (deps test16.proto ../../include/ocaml-protoc/ocamloptions.proto) (action - (run ocaml-protoc -I ../../include/ocaml-protoc -I %{env:INCLUDE=/usr/include/} - --binary --pp --ml_out ./ ./ test16.proto))) + (run ocaml-protoc -I ../../include/ocaml-protoc -I + %{env:INCLUDE=/usr/include/} --binary --pp --ml_out ./ ./ test16.proto))) (executable (name test16_ml) @@ -185,8 +185,8 @@ (targets test17.ml test17.mli) (deps test17.proto ../../include/ocaml-protoc/ocamloptions.proto) (action - (run ocaml-protoc -I ../../include/ocaml-protoc -I %{env:INCLUDE=/usr/include/} - --binary --pp --ml_out ./ ./ test17.proto))) + (run ocaml-protoc -I ../../include/ocaml-protoc -I + %{env:INCLUDE=/usr/include/} --binary --pp --ml_out ./ ./ test17.proto))) (executable (name test17_ml) @@ -197,8 +197,8 @@ (targets test18.ml test18.mli) (deps test18.proto ../../include/ocaml-protoc/ocamloptions.proto) (action - (run ocaml-protoc -I ../../include/ocaml-protoc -I %{env:INCLUDE=/usr/include/} - --binary --pp --ml_out ./ ./ test18.proto))) + (run ocaml-protoc -I ../../include/ocaml-protoc -I + %{env:INCLUDE=/usr/include/} --binary --pp --ml_out ./ ./ test18.proto))) (executable (name test18_ml) @@ -209,8 +209,8 @@ (targets test19.ml test19.mli) (deps test19.proto ../../include/ocaml-protoc/ocamloptions.proto) (action - (run ocaml-protoc -I ../../include/ocaml-protoc -I %{env:INCLUDE=/usr/include/} - --binary --pp --ml_out ./ ./ test19.proto))) + (run ocaml-protoc -I ../../include/ocaml-protoc -I + %{env:INCLUDE=/usr/include/} --binary --pp --ml_out ./ ./ test19.proto))) (executable (name test19_ml) @@ -221,8 +221,8 @@ (targets test20.ml test20.mli) (deps test20.proto ../../include/ocaml-protoc/ocamloptions.proto) (action - (run ocaml-protoc -I ../../include/ocaml-protoc -I %{env:INCLUDE=/usr/include/} - --binary --pp --ml_out ./ ./ test20.proto))) + (run ocaml-protoc -I ../../include/ocaml-protoc -I + %{env:INCLUDE=/usr/include/} --binary --pp --ml_out ./ ./ test20.proto))) (executable (name test20_ml) @@ -235,8 +235,8 @@ (targets test21.ml test21.mli) (deps test21.proto ../../include/ocaml-protoc/ocamloptions.proto) (action - (run ocaml-protoc -I ../../include/ocaml-protoc -I %{env:INCLUDE=/usr/include/} - --binary --pp --ml_out ./ ./ test21.proto))) + (run ocaml-protoc -I ../../include/ocaml-protoc -I + %{env:INCLUDE=/usr/include/} --binary --pp --ml_out ./ ./ test21.proto))) (executable (name test21_ml) @@ -293,8 +293,8 @@ (targets test26.ml test26.mli) (deps test26.proto ../../include/ocaml-protoc/ocamloptions.proto) (action - (run ocaml-protoc -I %{env:INCLUDE=/usr/include/} --binary --pp --ml_out ./ ./ - test26.proto))) + (run ocaml-protoc -I %{env:INCLUDE=/usr/include/} --binary --pp --ml_out ./ + ./ test26.proto))) (executable (name test26_ml) diff --git a/src/tests/unit-tests/dune b/src/tests/unit-tests/dune index 0a110bdf..0583504b 100644 --- a/src/tests/unit-tests/dune +++ b/src/tests/unit-tests/dune @@ -2,8 +2,7 @@ (package ocaml-protoc) (names graph_test format_play_ground lexer_comment ocaml_codegen_test parse_enum pbtt_compile_p2 test_typing verify_syntax_invariants - parse_message parse_field_options - parse_file_options parse_import pbtt_compile_p1 backend_ocaml_test - pbrt_array wrapper_encoding varint) + parse_message parse_field_options parse_file_options parse_import + pbtt_compile_p1 backend_ocaml_test pbrt_array wrapper_encoding varint) (libraries pbrt ocaml-protoc.compiler-lib) (flags :standard -open Ocaml_protoc_compiler_lib)) diff --git a/src/tests/yojson/dune b/src/tests/yojson/dune index 9b34a351..a0f41639 100644 --- a/src/tests/yojson/dune +++ b/src/tests/yojson/dune @@ -1,11 +1,12 @@ - (test - (name yojson_unittest) - (package ocaml-protoc) - (libraries pbrt pbrt_yojson)) + (name yojson_unittest) + (package ocaml-protoc) + (libraries pbrt pbrt_yojson)) (rule - (targets yojson_unittest.ml yojson_unittest.mli) - (deps (:file yojson_unittest.proto)) - (action (run %{project_root}/src/ocaml-protoc/ocaml_protoc.exe - %{file} --yojson --ml_out=.))) + (targets yojson_unittest.ml yojson_unittest.mli) + (deps + (:file yojson_unittest.proto)) + (action + (run %{project_root}/src/ocaml-protoc/ocaml_protoc.exe %{file} --yojson + --ml_out=.))) diff --git a/src/tests/yojson/yojson/dune b/src/tests/yojson/yojson/dune index 03f6d94a..aa2c92a2 100644 --- a/src/tests/yojson/yojson/dune +++ b/src/tests/yojson/yojson/dune @@ -1,5 +1,4 @@ - (test - (name unit_tests) - (package pbrt_yojson) - (libraries yojson pbrt_yojson)) + (name unit_tests) + (package pbrt_yojson) + (libraries yojson pbrt_yojson)) diff --git a/src/tests/yojson/yojson/unit_tests.ml b/src/tests/yojson/yojson/unit_tests.ml index 4a7aaae8..c5ef64f9 100644 --- a/src/tests/yojson/yojson/unit_tests.ml +++ b/src/tests/yojson/yojson/unit_tests.ml @@ -14,4 +14,3 @@ let () = assert (true = bool (`Bool true) "r" "f"); assert (false = bool (`Bool false) "r" "f"); () -