From 2de6199d6afa6532d988ac7dddd14c88548fef7b Mon Sep 17 00:00:00 2001 From: Ewen Maclean Date: Tue, 23 Oct 2018 17:08:31 +0100 Subject: [PATCH 1/6] adding Matt's Json decoders --- src-core-pp/base_types_json.ml | 128 ++++++++++++++++++--------------- src-core-pp/datetime_json.ml | 25 ++++++- src-core-pp/dune | 2 +- 3 files changed, 95 insertions(+), 60 deletions(-) diff --git a/src-core-pp/base_types_json.ml b/src-core-pp/base_types_json.ml index 70bb0e0c..87270e28 100644 --- a/src-core-pp/base_types_json.ml +++ b/src-core-pp/base_types_json.ml @@ -10,6 +10,8 @@ open Yojson;; open Numeric;; module JU = Yojson.Basic.Util;; +module D = Decoders_yojson.Basic.Decode;; +open D.Infix;; (** * Int @@ -22,8 +24,8 @@ let int_opt_to_json : int option -> json = function | Some x -> int_to_json x ;; -let json_to_int_opt x : int option= - JU.to_int_option x;; +let to_int_decoder : int Decoders_yojson.Basic.Decode.decoder = + D.int;; (** @@ -35,8 +37,8 @@ let char_opt_to_json = function | None -> `Null | Some x -> `String x;; -let json_to_char_opt x : string option = - JU.to_string_option x;; +let to_char_decoder : string Decoders_yojson.Basic.Decode.decoder = + D.string;; (** @@ -56,11 +58,13 @@ let float_0_opt_to_json = function | Some x -> float_0_to_json x ;; -let json_to_float_0_opt json : Numeric.fix_float_0 option = - match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision -> - if precision != 0 then None else - match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number -> - Some Numeric.( float_Create_0 number);; +let float_0_decoder : Numeric.fix_float_0 Decoders_yojson.Basic.Decode.decoder = + D.field "Precision" D.int >>= (fun p -> + if (p <> 0) then + D.fail "Precision must be 0 in float_0 json encoding." else + D.field "Number" D.int >>= (fun x -> + D.succeed + Numeric.(float_Create_0 x)));; let float_1_to_json : fix_float_1 -> json = function | Float_1 x -> @@ -75,11 +79,13 @@ let float_1_opt_to_json = function | Some x -> float_1_to_json x ;; -let json_to_float_1_opt json : Numeric.fix_float_1 option = - match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision -> - if precision != 1 then None else - match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number -> - Some Numeric.( float_Create_1 number);; +let float_1_decoder : Numeric.fix_float_1 Decoders_yojson.Basic.Decode.decoder = + D.field "Precision" D.int >>= (fun p -> + if (p <> 1) then + D.fail "Precision must be 1 in float_1 json encoding." else + D.field "Number" D.int >>= (fun x -> + D.succeed + Numeric.(float_Create_1 x)));; let float_2_to_json : fix_float_2 -> json = function | Float_2 x -> @@ -94,11 +100,14 @@ let float_2_opt_to_json = function | Some x -> float_2_to_json x ;; -let json_to_float_2_opt json : Numeric.fix_float_2 option = - match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision -> - if precision != 2 then None else - match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number -> - Some Numeric.( float_Create_2 number);; +let float_2_decoder : Numeric.fix_float_2 Decoders_yojson.Basic.Decode.decoder = + D.field "Precision" D.int >>= (fun p -> + if (p <> 2) then + D.fail "Precision must be 2 in float_2 json encoding." else + D.field "Number" D.int >>= (fun x -> + D.succeed + Numeric.(float_Create_2 x)));; + let float_3_to_json : fix_float_3 -> json = function | Float_3 x -> @@ -113,11 +122,14 @@ let float_3_opt_to_json = function | Some x -> float_3_to_json x ;; -let json_to_float_3_opt json : Numeric.fix_float_3 option = - match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision -> - if precision != 3 then None else - match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number -> - Some Numeric.( float_Create_3 number);; +let float_3_decoder : Numeric.fix_float_3 Decoders_yojson.Basic.Decode.decoder = + D.field "Precision" D.int >>= (fun p -> + if (p <> 3) then + D.fail "Precision must be 3 in float_3 json encoding." else + D.field "Number" D.int >>= (fun x -> + D.succeed + Numeric.(float_Create_3 x)));; + let float_4_to_json : fix_float_4 -> json = function | Float_4 x -> @@ -132,11 +144,14 @@ let float_4_opt_to_json = function | Some x -> float_4_to_json x ;; -let json_to_float_4_opt json : Numeric.fix_float_4 option = - match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision -> - if precision != 4 then None else - match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number -> - Some Numeric.( float_Create_4 number);; +let float_4_decoder : Numeric.fix_float_4 Decoders_yojson.Basic.Decode.decoder = + D.field "Precision" D.int >>= (fun p -> + if (p <> 4) then + D.fail "Precision must be 4 in float_4 json encoding." else + D.field "Number" D.int >>= (fun x -> + D.succeed + Numeric.(float_Create_4 x)));; + let float_5_to_json : fix_float_5-> json = function | Float_5 x -> @@ -151,11 +166,14 @@ let float_5_opt_to_json = function | Some x -> float_5_to_json x ;; -let json_to_float_5_opt json : Numeric.fix_float_5 option = - match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision -> - if precision != 5 then None else - match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number -> - Some Numeric.( float_Create_5 number);; +let float_5_decoder : Numeric.fix_float_5 Decoders_yojson.Basic.Decode.decoder = + D.field "Precision" D.int >>= (fun p -> + if (p <> 5) then + D.fail "Precision must be 5 in float_5 json encoding." else + D.field "Number" D.int >>= (fun x -> + D.succeed + Numeric.(float_Create_5 x)));; + let float_6_to_json : fix_float_6 -> json = function | Float_6 x -> @@ -170,11 +188,13 @@ let float_6_opt_to_json = function | Some x -> float_6_to_json x ;; -let json_to_float_6_opt json : Numeric.fix_float_6 option = - match JU.(json |> member "Precision" |> to_int_option ) with None -> None | Some precision -> - if precision != 6 then None else - match JU.(json |> member "Number" |> to_int_option ) with None -> None | Some number -> - Some Numeric.( float_Create_6 number);; +let float_6_decoder : Numeric.fix_float_6 Decoders_yojson.Basic.Decode.decoder = + D.field "Precision" D.int >>= (fun p -> + if (p <> 6) then + D.fail "Precision must be 6 in float_6 json encoding." else + D.field "Number" D.int >>= (fun x -> + D.succeed + Numeric.(float_Create_6 x)));; let float_to_json x = float_6_to_json x;; @@ -182,7 +202,7 @@ let float_to_json x = float_6_to_json x;; let float_opt_to_json x = float_6_opt_to_json x;; -let json_to_float_opt = json_to_float_6_opt;; +let json_to_float_decoder = float_6_decoder;; (* FIX_String *) @@ -191,16 +211,15 @@ let string_to_json x : json = `String x ;; -let json_to_string_opt x : string option = - JU.to_string_option x -;; - - let string_opt_to_json = function | None -> `Null | Some x -> string_to_json x ;; +let json_to_string_decoder : string Decoders_yojson.Basic.Decode.decoder = + D.string +;; + (** * FIX_Symbol @@ -210,15 +229,15 @@ let symbol_to_json x : json = `String x ;; -let json_to_symbol_opt x : string option = - JU.to_string_option x -;; - let symbol_opt_to_json = function | None -> `Null | Some x -> symbol_to_json x ;; +let json_to_symbol_decoder : string Decoders_yojson.Basic.Decode.decoder = + D.string +;; + (** * FIX_Bool @@ -229,15 +248,8 @@ let bool_to_json : bool -> json = function | false -> `String "false" ;; -let json_to_bool_opt x : bool option = - match (JU.to_string_option x) with - | None -> None - | Some b -> (match b with - | "true" -> Some true - | "false" -> Some false - | _ -> None - ) -;; +let json_to_bool_decoder : bool Decoders_yojson.Basic.Decode.decoder = + D.bool;; let bool_opt_to_json : bool option -> json = function | None -> `Null diff --git a/src-core-pp/datetime_json.ml b/src-core-pp/datetime_json.ml index 9b2f6ec5..37967799 100644 --- a/src-core-pp/datetime_json.ml +++ b/src-core-pp/datetime_json.ml @@ -10,7 +10,10 @@ open Yojson;; open Datetime;; open Base_types_json;; -module JU = Yojson.Basic.Util ;; +module JU = Yojson.Basic.Util;; +module D = Decoders_yojson.Basic.Decode;; +open D.Infix;; + let filter_nulls = @@ -30,6 +33,26 @@ let utctimestamp_milli_to_json ( ts : fix_utctimestamp_milli ) : json = `Assoc list_assoc ;; +let utctimestamp_milli_decoder : fix_utctimestamp_milli Decoders_yojson.Basic.Decode.decoder = + (D.field "utc_timestamp_year" D.int) >>= (fun utc_timestamp_year -> + (D.field "utc_timestamp_month" D.int) >>= (fun utc_timestamp_month -> + (D.field "utc_timestamp_day" D.int) >>= (fun utc_timestamp_day -> + (D.field "utc_timestamp_hour" D.int) >>= (fun utc_timestamp_hour -> + (D.field "utc_timestamp_minute" D.int) >>= (fun utc_timestamp_minute -> + (D.field "utc_timestamp_second" D.int) >>= (fun utc_timestamp_second -> + (D.maybe (D.field "utc_timestamp_millisec" D.int)) >>= (fun utc_timestamp_millisec -> + D.succeed { + utc_timestamp_year + ; utc_timestamp_month + ; utc_timestamp_day + ; utc_timestamp_hour + ; utc_timestamp_minute + ; utc_timestamp_second + ; utc_timestamp_millisec + })))))));; + + + let json_to_utctimestamp_milli_opt json = match JU.(json |> member "utc_timestamp_year" |> to_int_option ) with None -> None | Some utc_timestamp_year -> match JU.(json |> member "utc_timestamp_month" |> to_int_option ) with None -> None | Some utc_timestamp_month -> diff --git a/src-core-pp/dune b/src-core-pp/dune index ae85ad7f..7ff1083d 100644 --- a/src-core-pp/dune +++ b/src-core-pp/dune @@ -2,5 +2,5 @@ (name core_pp) (public_name core_pp) (wrapped false) - (libraries core yojson) + (libraries decoders-yojson decoders core yojson) ) From a28f5330b53c44147f64f27b217c4c7e3c4e816e Mon Sep 17 00:00:00 2001 From: Ewen Maclean Date: Wed, 24 Oct 2018 10:10:34 +0100 Subject: [PATCH 2/6] finalising datetime --- src-core-pp/datetime_json.ml | 207 ++++++++++++++++------------------- 1 file changed, 92 insertions(+), 115 deletions(-) diff --git a/src-core-pp/datetime_json.ml b/src-core-pp/datetime_json.ml index 37967799..43f14598 100644 --- a/src-core-pp/datetime_json.ml +++ b/src-core-pp/datetime_json.ml @@ -51,26 +51,6 @@ let utctimestamp_milli_decoder : fix_utctimestamp_milli Decoders_yojson.Basic.De ; utc_timestamp_millisec })))))));; - - -let json_to_utctimestamp_milli_opt json = - match JU.(json |> member "utc_timestamp_year" |> to_int_option ) with None -> None | Some utc_timestamp_year -> - match JU.(json |> member "utc_timestamp_month" |> to_int_option ) with None -> None | Some utc_timestamp_month -> - match JU.(json |> member "utc_timestamp_day" |> to_int_option ) with None -> None | Some utc_timestamp_day -> - match JU.(json |> member "utc_timestamp_hour" |> to_int_option ) with None -> None | Some utc_timestamp_hour -> - match JU.(json |> member "utc_timestamp_minute" |> to_int_option ) with None -> None | Some utc_timestamp_minute -> - match JU.(json |> member "utc_timestamp_second" |> to_int_option ) with None -> None | Some utc_timestamp_second -> - let utc_timestamp_millisec = JU.(json |> member "utc_timestamp_millisec" |> to_int_option ) in - Some { utc_timestamp_year - ; utc_timestamp_month - ; utc_timestamp_day - ; utc_timestamp_hour - ; utc_timestamp_minute - ; utc_timestamp_second - ; utc_timestamp_millisec - } -;; - let utctimestamp_micro_to_json ( ts : fix_utctimestamp_micro ) : json = let list_assoc = [ ( "utc_timestamp_year" , `Int ts.utc_timestamp_year ); @@ -84,23 +64,23 @@ let utctimestamp_micro_to_json ( ts : fix_utctimestamp_micro ) : json = `Assoc list_assoc ;; -let json_to_utctimestamp_micro_opt json = - match JU.(json |> member "utc_timestamp_year" |> to_int_option ) with None -> None | Some utc_timestamp_year -> - match JU.(json |> member "utc_timestamp_month" |> to_int_option ) with None -> None | Some utc_timestamp_month -> - match JU.(json |> member "utc_timestamp_day" |> to_int_option ) with None -> None | Some utc_timestamp_day -> - match JU.(json |> member "utc_timestamp_hour" |> to_int_option ) with None -> None | Some utc_timestamp_hour -> - match JU.(json |> member "utc_timestamp_minute" |> to_int_option ) with None -> None | Some utc_timestamp_minute -> - match JU.(json |> member "utc_timestamp_second" |> to_int_option ) with None -> None | Some utc_timestamp_second -> - let utc_timestamp_microsec = JU.(json |> member "utc_timestamp_microsec" |> to_int_option ) in - Some { utc_timestamp_year - ; utc_timestamp_month - ; utc_timestamp_day - ; utc_timestamp_hour - ; utc_timestamp_minute - ; utc_timestamp_second - ; utc_timestamp_microsec - } -;; +let utctimestamp_micro_decoder : fix_utctimestamp_micro Decoders_yojson.Basic.Decode.decoder = + (D.field "utc_timestamp_year" D.int) >>= (fun utc_timestamp_year -> + (D.field "utc_timestamp_month" D.int) >>= (fun utc_timestamp_month -> + (D.field "utc_timestamp_day" D.int) >>= (fun utc_timestamp_day -> + (D.field "utc_timestamp_hour" D.int) >>= (fun utc_timestamp_hour -> + (D.field "utc_timestamp_minute" D.int) >>= (fun utc_timestamp_minute -> + (D.field "utc_timestamp_second" D.int) >>= (fun utc_timestamp_second -> + (D.maybe (D.field "utc_timestamp_microsec" D.int)) >>= (fun utc_timestamp_microsec -> + D.succeed { + utc_timestamp_year + ; utc_timestamp_month + ; utc_timestamp_day + ; utc_timestamp_hour + ; utc_timestamp_minute + ; utc_timestamp_second + ; utc_timestamp_microsec + })))))));; let utctimestamp_milli_opt_to_json = function | None -> `Null @@ -124,21 +104,21 @@ let duration_to_json ( d : fix_duration ) = `Assoc list_assoc ;; -let json_to_duration_opt json = - match JU.(json |> member "dur_years" |> to_int_option ) with None -> None | Some dur_years -> - match JU.(json |> member "dur_months" |> to_int_option ) with None -> None | Some dur_months -> - match JU.(json |> member "dur_days" |> to_int_option ) with None -> None | Some dur_days -> - match JU.(json |> member "dur_hours" |> to_int_option ) with None -> None | Some dur_hours -> - match JU.(json |> member "dur_minutes" |> to_int_option ) with None -> None | Some dur_minutes -> - match JU.(json |> member "dur_seconds" |> to_int_option ) with None -> None | Some dur_seconds -> - Some { dur_years - ; dur_months - ; dur_days - ; dur_hours - ; dur_minutes - ; dur_seconds - } -;; +let duration_decoder : fix_duration Decoders_yojson.Basic.Decode.decoder = + (D.field "dur_years" D.int) >>= (fun dur_years -> + (D.field "dur_months" D.int) >>= (fun dur_months -> + (D.field "dur_days" D.int) >>= (fun dur_days -> + (D.field "dur_hours" D.int) >>= (fun dur_hours -> + (D.field "dur_minutes" D.int) >>= (fun dur_minutes -> + (D.field "dur_seconds" D.int) >>= (fun dur_seconds -> + D.succeed { + dur_years + ; dur_months + ; dur_days + ; dur_hours + ; dur_minutes + ; dur_seconds + }))))));; let duration_opt_to_json = function | None -> `Null @@ -154,22 +134,21 @@ let localmktdate_to_json ( d : fix_localmktdate ) = `Assoc list_assoc ;; -let json_to_localmktdate_opt json = - match JU.(json |> member "localmktdate_year" |> to_int_option ) with None -> None | Some localmktdate_year -> - match JU.(json |> member "localmktdate_month" |> to_int_option ) with None -> None | Some localmktdate_month -> - match JU.(json |> member "localmktdate_day" |> to_int_option ) with None -> None | Some localmktdate_day -> - Some { localmktdate_year; - localmktdate_month; - localmktdate_day - } -;; +let localmktdate_decoder : fix_localmktdate Decoders_yojson.Basic.Decode.decoder = + (D.field "localmktdate_year" D.int) >>= (fun localmktdate_year -> + (D.field "localmktdate_month" D.int) >>= (fun localmktdate_month -> + (D.field "localmktdate_day" D.int) >>= (fun localmktdate_day -> + D.succeed { + localmktdate_year + ; localmktdate_month + ; localmktdate_day + })));; let localmktdate_opt_to_json = function | None -> `Null | Some x -> localmktdate_to_json x ;; - let utcdateonly_to_json ( d : fix_utcdateonly ) = let list_assoc = [ ( "utc_dateonly_year" , int_to_json d.utc_dateonly_year ); @@ -179,15 +158,15 @@ let utcdateonly_to_json ( d : fix_utcdateonly ) = `Assoc list_assoc ;; -let json_to_utcdateonly_opt json = - match JU.(json |> member "utc_dateonly_year" |> to_int_option ) with None -> None | Some utc_dateonly_year -> - match JU.(json |> member "utc_dateonly_month" |> to_int_option ) with None -> None | Some utc_dateonly_month -> - match JU.(json |> member "utc_dateonly_day" |> to_int_option ) with None -> None | Some utc_dateonly_day -> - Some { utc_dateonly_year; - utc_dateonly_month; - utc_dateonly_day - } -;; +let utcdateonly_decoder : fix_utcdateonly Decoders_yojson.Basic.Decode.decoder = + (D.field "utc_dateonly_year" D.int) >>= (fun utc_dateonly_year -> + (D.field "utc_dateonly_month" D.int) >>= (fun utc_dateonly_month -> + (D.field "utc_dateonly_day" D.int) >>= (fun utc_dateonly_day -> + D.succeed { + utc_dateonly_year + ; utc_dateonly_month + ; utc_dateonly_day + })));; let utcdateonly_opt_to_json = function | None -> `Null @@ -205,17 +184,17 @@ let utctimeonly_milli_to_json ( d : fix_utctimeonly_milli ) = `Assoc list_assoc ;; -let json_to_utctimeonly_milli_opt json = - match JU.(json |> member "utc_timesonly_hour" |> to_int_option ) with None -> None | Some utc_timeonly_hour -> - match JU.(json |> member "utc_timesonly_minute" |> to_int_option ) with None -> None | Some utc_timeonly_minute -> - match JU.(json |> member "utc_timesonly_second" |> to_int_option ) with None -> None | Some utc_timeonly_second -> - let utc_timeonly_millisec = JU.(json |> member "utc_timeonly_millisec" |> to_int_option ) in - Some { utc_timeonly_hour - ; utc_timeonly_minute - ; utc_timeonly_second - ; utc_timeonly_millisec - } -;; +let utctimeonly_milli_decoder : fix_utctimeonly_milli Decoders_yojson.Basic.Decode.decoder = + (D.field "utc_timeonly_hour" D.int) >>= (fun utc_timeonly_hour -> + (D.field "utc_timeonly_minute" D.int) >>= (fun utc_timeonly_minute -> + (D.field "utc_timeonly_second" D.int) >>= (fun utc_timeonly_second -> + D.maybe (D.field "utc_timeonly_millisec" D.int) >>= (fun utc_timeonly_millisec -> + D.succeed { + utc_timeonly_hour + ; utc_timeonly_minute + ; utc_timeonly_second + ; utc_timeonly_millisec + }))));; let utctimeonly_milli_opt_to_json = function | None -> `Null @@ -232,17 +211,17 @@ let utctimeonly_micro_to_json ( d : fix_utctimeonly_micro ) = `Assoc list_assoc ;; -let json_to_utctimeonly_micro_opt json = - match JU.(json |> member "utc_timesonly_hour" |> to_int_option ) with None -> None | Some utc_timeonly_hour -> - match JU.(json |> member "utc_timesonly_minute" |> to_int_option ) with None -> None | Some utc_timeonly_minute -> - match JU.(json |> member "utc_timesonly_second" |> to_int_option ) with None -> None | Some utc_timeonly_second -> - let utc_timeonly_microsec = JU.(json |> member "utc_timeonly_microsec" |> to_int_option ) in - Some { utc_timeonly_hour - ; utc_timeonly_minute - ; utc_timeonly_second - ; utc_timeonly_microsec - } -;; +let utctimeonly_micro_decoder : fix_utctimeonly_micro Decoders_yojson.Basic.Decode.decoder = + (D.field "utc_timeonly_hour" D.int) >>= (fun utc_timeonly_hour -> + (D.field "utc_timeonly_minute" D.int) >>= (fun utc_timeonly_minute -> + (D.field "utc_timeonly_second" D.int) >>= (fun utc_timeonly_second -> + D.maybe (D.field "utc_timeonly_microsec" D.int) >>= (fun utc_timeonly_microsec -> + D.succeed { + utc_timeonly_hour + ; utc_timeonly_minute + ; utc_timeonly_second + ; utc_timeonly_microsec + }))));; let utctimeonly_micro_opt_to_json = function | None -> `Null @@ -257,23 +236,21 @@ let week_to_json = function | Week_5 -> `String "Week5" ;; -let json_to_week_opt json = - match JU.(json |> to_string_option ) with None -> None | Some w -> - (match w with - | "Week1" -> Some Week_1 - | "Week2" -> Some Week_2 - | "Week3" -> Some Week_3 - | "Week4" -> Some Week_4 - | "Week5" -> Some Week_5 - | _ -> None) -;; +let week_decoder : fix_week Decoders_yojson.Basic.Decode.decoder = + D.string >>= (fun w -> + match w with + | "Week1" -> D.succeed (Week_1) + | "Week2" -> D.succeed (Week_2) + | "Week3" -> D.succeed (Week_3) + | "Week4" -> D.succeed (Week_4) + | "Week5" -> D.succeed (Week_5) + | x -> D.fail (x^" is not a valid Week encoding."));; let week_opt_to_json = function | None -> `Null | Some x -> week_to_json x ;; - let monthyear_to_json ( d : fix_monthyear) = let list_assoc = [ ( "monthyear_year" , int_to_json d.monthyear_year ); @@ -284,17 +261,17 @@ let monthyear_to_json ( d : fix_monthyear) = `Assoc list_assoc ;; -let json_to_monthyear_opt json = - match JU.(json |> member "monthyear_year" |> to_int_option ) with None -> None | Some monthyear_year -> - match JU.(json |> member "monthyear_month" |> to_int_option ) with None -> None | Some monthyear_month -> - let monthyear_day = JU.(json |> member "monthyear_day" |> to_int_option ) in - let monthyear_week = JU.(json |> member "monthyear_week" |> json_to_week_opt )in - Some {monthyear_year; - monthyear_month; - monthyear_day; - monthyear_week - } -;; +let monthyear : fix_monthyear Decoders_yojson.Basic.Decode.decoder = + (D.field "monthyear_year" D.int) >>= (fun monthyear_year -> + (D.field "monthyear_month" D.int) >>= (fun monthyear_month -> + D.maybe (D.field "monthyear_day" D.int) >>= (fun monthyear_day -> + D.maybe (D.field "monthyear_week" week_decoder) >>= (fun monthyear_week -> + D.succeed { + monthyear_year + ; monthyear_month + ; monthyear_day + ; monthyear_week + }))));; let monthyear_opt_to_json = function | None -> `Null From 83c0adaea72cb6f4d510187da1067213d3af469d Mon Sep 17 00:00:00 2001 From: Ewen Maclean Date: Wed, 24 Oct 2018 10:20:45 +0100 Subject: [PATCH 3/6] updating wercker --- wercker.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wercker.yml b/wercker.yml index d26ddbf1..40ceddc9 100644 --- a/wercker.yml +++ b/wercker.yml @@ -12,6 +12,6 @@ build: set -ex export OPAMROOT=/home/opam/.opam opam update - opam install dune + opam install dune decoders-yojson decoders make opam1-setup make From 0e465b5725de59fa1ac8fe54856a587e5cb89699 Mon Sep 17 00:00:00 2001 From: Ewen Maclean Date: Wed, 24 Oct 2018 12:15:03 +0100 Subject: [PATCH 4/6] updating bracketing for abstractions --- src-core-pp/base_types_json.ml | 84 +++++++------- src-core-pp/datetime_json.ml | 198 ++++++++++++++++----------------- 2 files changed, 141 insertions(+), 141 deletions(-) diff --git a/src-core-pp/base_types_json.ml b/src-core-pp/base_types_json.ml index 87270e28..f74985a3 100644 --- a/src-core-pp/base_types_json.ml +++ b/src-core-pp/base_types_json.ml @@ -59,12 +59,12 @@ let float_0_opt_to_json = function ;; let float_0_decoder : Numeric.fix_float_0 Decoders_yojson.Basic.Decode.decoder = - D.field "Precision" D.int >>= (fun p -> - if (p <> 0) then - D.fail "Precision must be 0 in float_0 json encoding." else - D.field "Number" D.int >>= (fun x -> - D.succeed - Numeric.(float_Create_0 x)));; + D.field "Precision" D.int >>= fun p -> + if (p <> 0) then + D.fail "Precision must be 0 in float_0 json encoding." else + D.field "Number" D.int >>= fun x -> + D.succeed + Numeric.(float_Create_0 x);; let float_1_to_json : fix_float_1 -> json = function | Float_1 x -> @@ -80,12 +80,12 @@ let float_1_opt_to_json = function ;; let float_1_decoder : Numeric.fix_float_1 Decoders_yojson.Basic.Decode.decoder = - D.field "Precision" D.int >>= (fun p -> - if (p <> 1) then - D.fail "Precision must be 1 in float_1 json encoding." else - D.field "Number" D.int >>= (fun x -> - D.succeed - Numeric.(float_Create_1 x)));; + D.field "Precision" D.int >>= fun p -> + if (p <> 1) then + D.fail "Precision must be 1 in float_1 json encoding." else + D.field "Number" D.int >>= fun x -> + D.succeed + Numeric.(float_Create_1 x);; let float_2_to_json : fix_float_2 -> json = function | Float_2 x -> @@ -101,12 +101,12 @@ let float_2_opt_to_json = function ;; let float_2_decoder : Numeric.fix_float_2 Decoders_yojson.Basic.Decode.decoder = - D.field "Precision" D.int >>= (fun p -> - if (p <> 2) then - D.fail "Precision must be 2 in float_2 json encoding." else - D.field "Number" D.int >>= (fun x -> - D.succeed - Numeric.(float_Create_2 x)));; + D.field "Precision" D.int >>= fun p -> + if (p <> 2) then + D.fail "Precision must be 2 in float_2 json encoding." else + D.field "Number" D.int >>= fun x -> + D.succeed + Numeric.(float_Create_2 x);; let float_3_to_json : fix_float_3 -> json = function @@ -123,12 +123,12 @@ let float_3_opt_to_json = function ;; let float_3_decoder : Numeric.fix_float_3 Decoders_yojson.Basic.Decode.decoder = - D.field "Precision" D.int >>= (fun p -> - if (p <> 3) then - D.fail "Precision must be 3 in float_3 json encoding." else - D.field "Number" D.int >>= (fun x -> - D.succeed - Numeric.(float_Create_3 x)));; + D.field "Precision" D.int >>= fun p -> + if (p <> 3) then + D.fail "Precision must be 3 in float_3 json encoding." else + D.field "Number" D.int >>= fun x -> + D.succeed + Numeric.(float_Create_3 x);; let float_4_to_json : fix_float_4 -> json = function @@ -145,12 +145,12 @@ let float_4_opt_to_json = function ;; let float_4_decoder : Numeric.fix_float_4 Decoders_yojson.Basic.Decode.decoder = - D.field "Precision" D.int >>= (fun p -> - if (p <> 4) then - D.fail "Precision must be 4 in float_4 json encoding." else - D.field "Number" D.int >>= (fun x -> - D.succeed - Numeric.(float_Create_4 x)));; + D.field "Precision" D.int >>= fun p -> + if (p <> 4) then + D.fail "Precision must be 4 in float_4 json encoding." else + D.field "Number" D.int >>= fun x -> + D.succeed + Numeric.(float_Create_4 x);; let float_5_to_json : fix_float_5-> json = function @@ -167,12 +167,12 @@ let float_5_opt_to_json = function ;; let float_5_decoder : Numeric.fix_float_5 Decoders_yojson.Basic.Decode.decoder = - D.field "Precision" D.int >>= (fun p -> - if (p <> 5) then - D.fail "Precision must be 5 in float_5 json encoding." else - D.field "Number" D.int >>= (fun x -> - D.succeed - Numeric.(float_Create_5 x)));; + D.field "Precision" D.int >>= fun p -> + if (p <> 5) then + D.fail "Precision must be 5 in float_5 json encoding." else + D.field "Number" D.int >>= fun x -> + D.succeed + Numeric.(float_Create_5 x);; let float_6_to_json : fix_float_6 -> json = function @@ -189,12 +189,12 @@ let float_6_opt_to_json = function ;; let float_6_decoder : Numeric.fix_float_6 Decoders_yojson.Basic.Decode.decoder = - D.field "Precision" D.int >>= (fun p -> - if (p <> 6) then - D.fail "Precision must be 6 in float_6 json encoding." else - D.field "Number" D.int >>= (fun x -> - D.succeed - Numeric.(float_Create_6 x)));; + D.field "Precision" D.int >>= fun p -> + if (p <> 6) then + D.fail "Precision must be 6 in float_6 json encoding." else + D.field "Number" D.int >>= fun x -> + D.succeed + Numeric.(float_Create_6 x);; let float_to_json x = float_6_to_json x;; diff --git a/src-core-pp/datetime_json.ml b/src-core-pp/datetime_json.ml index 43f14598..81c4173e 100644 --- a/src-core-pp/datetime_json.ml +++ b/src-core-pp/datetime_json.ml @@ -34,22 +34,22 @@ let utctimestamp_milli_to_json ( ts : fix_utctimestamp_milli ) : json = ;; let utctimestamp_milli_decoder : fix_utctimestamp_milli Decoders_yojson.Basic.Decode.decoder = - (D.field "utc_timestamp_year" D.int) >>= (fun utc_timestamp_year -> - (D.field "utc_timestamp_month" D.int) >>= (fun utc_timestamp_month -> - (D.field "utc_timestamp_day" D.int) >>= (fun utc_timestamp_day -> - (D.field "utc_timestamp_hour" D.int) >>= (fun utc_timestamp_hour -> - (D.field "utc_timestamp_minute" D.int) >>= (fun utc_timestamp_minute -> - (D.field "utc_timestamp_second" D.int) >>= (fun utc_timestamp_second -> - (D.maybe (D.field "utc_timestamp_millisec" D.int)) >>= (fun utc_timestamp_millisec -> - D.succeed { - utc_timestamp_year - ; utc_timestamp_month - ; utc_timestamp_day - ; utc_timestamp_hour - ; utc_timestamp_minute - ; utc_timestamp_second - ; utc_timestamp_millisec - })))))));; + (D.field "utc_timestamp_year" D.int) >>= fun utc_timestamp_year -> + (D.field "utc_timestamp_month" D.int) >>= fun utc_timestamp_month -> + (D.field "utc_timestamp_day" D.int) >>= fun utc_timestamp_day -> + (D.field "utc_timestamp_hour" D.int) >>= fun utc_timestamp_hour -> + (D.field "utc_timestamp_minute" D.int) >>= fun utc_timestamp_minute -> + (D.field "utc_timestamp_second" D.int) >>= fun utc_timestamp_second -> + D.maybe (D.field "utc_timestamp_millisec" D.int) >>= fun utc_timestamp_millisec -> + D.succeed { + utc_timestamp_year + ; utc_timestamp_month + ; utc_timestamp_day + ; utc_timestamp_hour + ; utc_timestamp_minute + ; utc_timestamp_second + ; utc_timestamp_millisec + };; let utctimestamp_micro_to_json ( ts : fix_utctimestamp_micro ) : json = let list_assoc = [ @@ -65,22 +65,22 @@ let utctimestamp_micro_to_json ( ts : fix_utctimestamp_micro ) : json = ;; let utctimestamp_micro_decoder : fix_utctimestamp_micro Decoders_yojson.Basic.Decode.decoder = - (D.field "utc_timestamp_year" D.int) >>= (fun utc_timestamp_year -> - (D.field "utc_timestamp_month" D.int) >>= (fun utc_timestamp_month -> - (D.field "utc_timestamp_day" D.int) >>= (fun utc_timestamp_day -> - (D.field "utc_timestamp_hour" D.int) >>= (fun utc_timestamp_hour -> - (D.field "utc_timestamp_minute" D.int) >>= (fun utc_timestamp_minute -> - (D.field "utc_timestamp_second" D.int) >>= (fun utc_timestamp_second -> - (D.maybe (D.field "utc_timestamp_microsec" D.int)) >>= (fun utc_timestamp_microsec -> - D.succeed { - utc_timestamp_year - ; utc_timestamp_month - ; utc_timestamp_day - ; utc_timestamp_hour - ; utc_timestamp_minute - ; utc_timestamp_second - ; utc_timestamp_microsec - })))))));; + (D.field "utc_timestamp_year" D.int) >>= fun utc_timestamp_year -> + (D.field "utc_timestamp_month" D.int) >>= fun utc_timestamp_month -> + (D.field "utc_timestamp_day" D.int) >>= fun utc_timestamp_day -> + (D.field "utc_timestamp_hour" D.int) >>= fun utc_timestamp_hour -> + (D.field "utc_timestamp_minute" D.int) >>= fun utc_timestamp_minute -> + (D.field "utc_timestamp_second" D.int) >>= fun utc_timestamp_second -> + D.maybe (D.field "utc_timestamp_microsec" D.int) >>= fun utc_timestamp_microsec -> + D.succeed { + utc_timestamp_year + ; utc_timestamp_month + ; utc_timestamp_day + ; utc_timestamp_hour + ; utc_timestamp_minute + ; utc_timestamp_second + ; utc_timestamp_microsec + };; let utctimestamp_milli_opt_to_json = function | None -> `Null @@ -105,20 +105,20 @@ let duration_to_json ( d : fix_duration ) = ;; let duration_decoder : fix_duration Decoders_yojson.Basic.Decode.decoder = - (D.field "dur_years" D.int) >>= (fun dur_years -> - (D.field "dur_months" D.int) >>= (fun dur_months -> - (D.field "dur_days" D.int) >>= (fun dur_days -> - (D.field "dur_hours" D.int) >>= (fun dur_hours -> - (D.field "dur_minutes" D.int) >>= (fun dur_minutes -> - (D.field "dur_seconds" D.int) >>= (fun dur_seconds -> - D.succeed { - dur_years - ; dur_months - ; dur_days - ; dur_hours - ; dur_minutes - ; dur_seconds - }))))));; + (D.field "dur_years" D.int) >>= fun dur_years -> + (D.field "dur_months" D.int) >>= fun dur_months -> + (D.field "dur_days" D.int) >>= fun dur_days -> + (D.field "dur_hours" D.int) >>= fun dur_hours -> + (D.field "dur_minutes" D.int) >>= fun dur_minutes -> + (D.field "dur_seconds" D.int) >>= fun dur_seconds -> + D.succeed { + dur_years + ; dur_months + ; dur_days + ; dur_hours + ; dur_minutes + ; dur_seconds + };; let duration_opt_to_json = function | None -> `Null @@ -135,14 +135,14 @@ let localmktdate_to_json ( d : fix_localmktdate ) = ;; let localmktdate_decoder : fix_localmktdate Decoders_yojson.Basic.Decode.decoder = - (D.field "localmktdate_year" D.int) >>= (fun localmktdate_year -> - (D.field "localmktdate_month" D.int) >>= (fun localmktdate_month -> - (D.field "localmktdate_day" D.int) >>= (fun localmktdate_day -> - D.succeed { - localmktdate_year - ; localmktdate_month - ; localmktdate_day - })));; + (D.field "localmktdate_year" D.int) >>= fun localmktdate_year -> + (D.field "localmktdate_month" D.int) >>= fun localmktdate_month -> + (D.field "localmktdate_day" D.int) >>= fun localmktdate_day -> + D.succeed { + localmktdate_year + ; localmktdate_month + ; localmktdate_day + };; let localmktdate_opt_to_json = function | None -> `Null @@ -159,14 +159,14 @@ let utcdateonly_to_json ( d : fix_utcdateonly ) = ;; let utcdateonly_decoder : fix_utcdateonly Decoders_yojson.Basic.Decode.decoder = - (D.field "utc_dateonly_year" D.int) >>= (fun utc_dateonly_year -> - (D.field "utc_dateonly_month" D.int) >>= (fun utc_dateonly_month -> - (D.field "utc_dateonly_day" D.int) >>= (fun utc_dateonly_day -> - D.succeed { - utc_dateonly_year - ; utc_dateonly_month - ; utc_dateonly_day - })));; + (D.field "utc_dateonly_year" D.int) >>= fun utc_dateonly_year -> + (D.field "utc_dateonly_month" D.int) >>= fun utc_dateonly_month -> + (D.field "utc_dateonly_day" D.int) >>= fun utc_dateonly_day -> + D.succeed { + utc_dateonly_year + ; utc_dateonly_month + ; utc_dateonly_day + };; let utcdateonly_opt_to_json = function | None -> `Null @@ -185,16 +185,16 @@ let utctimeonly_milli_to_json ( d : fix_utctimeonly_milli ) = ;; let utctimeonly_milli_decoder : fix_utctimeonly_milli Decoders_yojson.Basic.Decode.decoder = - (D.field "utc_timeonly_hour" D.int) >>= (fun utc_timeonly_hour -> - (D.field "utc_timeonly_minute" D.int) >>= (fun utc_timeonly_minute -> - (D.field "utc_timeonly_second" D.int) >>= (fun utc_timeonly_second -> - D.maybe (D.field "utc_timeonly_millisec" D.int) >>= (fun utc_timeonly_millisec -> - D.succeed { - utc_timeonly_hour - ; utc_timeonly_minute - ; utc_timeonly_second - ; utc_timeonly_millisec - }))));; + (D.field "utc_timeonly_hour" D.int) >>= fun utc_timeonly_hour -> + (D.field "utc_timeonly_minute" D.int) >>= fun utc_timeonly_minute -> + (D.field "utc_timeonly_second" D.int) >>= fun utc_timeonly_second -> + D.maybe (D.field "utc_timeonly_millisec" D.int) >>= fun utc_timeonly_millisec -> + D.succeed { + utc_timeonly_hour + ; utc_timeonly_minute + ; utc_timeonly_second + ; utc_timeonly_millisec + };; let utctimeonly_milli_opt_to_json = function | None -> `Null @@ -212,16 +212,16 @@ let utctimeonly_micro_to_json ( d : fix_utctimeonly_micro ) = ;; let utctimeonly_micro_decoder : fix_utctimeonly_micro Decoders_yojson.Basic.Decode.decoder = - (D.field "utc_timeonly_hour" D.int) >>= (fun utc_timeonly_hour -> - (D.field "utc_timeonly_minute" D.int) >>= (fun utc_timeonly_minute -> - (D.field "utc_timeonly_second" D.int) >>= (fun utc_timeonly_second -> - D.maybe (D.field "utc_timeonly_microsec" D.int) >>= (fun utc_timeonly_microsec -> - D.succeed { - utc_timeonly_hour - ; utc_timeonly_minute - ; utc_timeonly_second - ; utc_timeonly_microsec - }))));; + (D.field "utc_timeonly_hour" D.int) >>= fun utc_timeonly_hour -> + (D.field "utc_timeonly_minute" D.int) >>= fun utc_timeonly_minute -> + (D.field "utc_timeonly_second" D.int) >>= fun utc_timeonly_second -> + D.maybe (D.field "utc_timeonly_microsec" D.int) >>= fun utc_timeonly_microsec -> + D.succeed { + utc_timeonly_hour + ; utc_timeonly_minute + ; utc_timeonly_second + ; utc_timeonly_microsec + };; let utctimeonly_micro_opt_to_json = function | None -> `Null @@ -238,13 +238,13 @@ let week_to_json = function let week_decoder : fix_week Decoders_yojson.Basic.Decode.decoder = D.string >>= (fun w -> - match w with - | "Week1" -> D.succeed (Week_1) - | "Week2" -> D.succeed (Week_2) - | "Week3" -> D.succeed (Week_3) - | "Week4" -> D.succeed (Week_4) - | "Week5" -> D.succeed (Week_5) - | x -> D.fail (x^" is not a valid Week encoding."));; + match w with + | "Week1" -> D.succeed (Week_1) + | "Week2" -> D.succeed (Week_2) + | "Week3" -> D.succeed (Week_3) + | "Week4" -> D.succeed (Week_4) + | "Week5" -> D.succeed (Week_5) + | x -> D.fail (x^" is not a valid Week encoding."));; let week_opt_to_json = function | None -> `Null @@ -262,16 +262,16 @@ let monthyear_to_json ( d : fix_monthyear) = ;; let monthyear : fix_monthyear Decoders_yojson.Basic.Decode.decoder = - (D.field "monthyear_year" D.int) >>= (fun monthyear_year -> - (D.field "monthyear_month" D.int) >>= (fun monthyear_month -> - D.maybe (D.field "monthyear_day" D.int) >>= (fun monthyear_day -> - D.maybe (D.field "monthyear_week" week_decoder) >>= (fun monthyear_week -> - D.succeed { - monthyear_year - ; monthyear_month - ; monthyear_day - ; monthyear_week - }))));; + (D.field "monthyear_year" D.int) >>= fun monthyear_year -> + (D.field "monthyear_month" D.int) >>= fun monthyear_month -> + D.maybe (D.field "monthyear_day" D.int) >>= fun monthyear_day -> + D.maybe (D.field "monthyear_week" week_decoder) >>= fun monthyear_week -> + D.succeed { + monthyear_year + ; monthyear_month + ; monthyear_day + ; monthyear_week + };; let monthyear_opt_to_json = function | None -> `Null From 103cb42b6dfbace5023ccfc297cadc4ce25ab52d Mon Sep 17 00:00:00 2001 From: Ewen Maclean Date: Wed, 24 Oct 2018 15:50:00 +0100 Subject: [PATCH 5/6] updating names of src-core --- src-core-pp/base_types_json.ml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src-core-pp/base_types_json.ml b/src-core-pp/base_types_json.ml index f74985a3..8b21012e 100644 --- a/src-core-pp/base_types_json.ml +++ b/src-core-pp/base_types_json.ml @@ -24,7 +24,7 @@ let int_opt_to_json : int option -> json = function | Some x -> int_to_json x ;; -let to_int_decoder : int Decoders_yojson.Basic.Decode.decoder = +let int_decoder : int Decoders_yojson.Basic.Decode.decoder = D.int;; @@ -37,7 +37,7 @@ let char_opt_to_json = function | None -> `Null | Some x -> `String x;; -let to_char_decoder : string Decoders_yojson.Basic.Decode.decoder = +let char_decoder : string Decoders_yojson.Basic.Decode.decoder = D.string;; @@ -202,7 +202,7 @@ let float_to_json x = float_6_to_json x;; let float_opt_to_json x = float_6_opt_to_json x;; -let json_to_float_decoder = float_6_decoder;; +let float_decoder = float_6_decoder;; (* FIX_String *) @@ -216,7 +216,7 @@ let string_opt_to_json = function | Some x -> string_to_json x ;; -let json_to_string_decoder : string Decoders_yojson.Basic.Decode.decoder = +let string_decoder : string Decoders_yojson.Basic.Decode.decoder = D.string ;; @@ -234,7 +234,7 @@ let symbol_opt_to_json = function | Some x -> symbol_to_json x ;; -let json_to_symbol_decoder : string Decoders_yojson.Basic.Decode.decoder = +let symbol_decoder : string Decoders_yojson.Basic.Decode.decoder = D.string ;; @@ -248,7 +248,7 @@ let bool_to_json : bool -> json = function | false -> `String "false" ;; -let json_to_bool_decoder : bool Decoders_yojson.Basic.Decode.decoder = +let bool_decoder : bool Decoders_yojson.Basic.Decode.decoder = D.bool;; let bool_opt_to_json : bool option -> json = function From 27568168f42d5bbcc0786426c970dd47932f32c2 Mon Sep 17 00:00:00 2001 From: Ewen Maclean Date: Thu, 25 Oct 2018 13:19:08 +0100 Subject: [PATCH 6/6] fixing error in name for monthyear --- src-core-pp/datetime_json.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src-core-pp/datetime_json.ml b/src-core-pp/datetime_json.ml index 81c4173e..53b93ee0 100644 --- a/src-core-pp/datetime_json.ml +++ b/src-core-pp/datetime_json.ml @@ -261,7 +261,7 @@ let monthyear_to_json ( d : fix_monthyear) = `Assoc list_assoc ;; -let monthyear : fix_monthyear Decoders_yojson.Basic.Decode.decoder = +let monthyear_decoder : fix_monthyear Decoders_yojson.Basic.Decode.decoder = (D.field "monthyear_year" D.int) >>= fun monthyear_year -> (D.field "monthyear_month" D.int) >>= fun monthyear_month -> D.maybe (D.field "monthyear_day" D.int) >>= fun monthyear_day ->