Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ppx: add runtime for result #13

Merged
merged 2 commits into from
Aug 30, 2024
Merged

ppx: add runtime for result #13

merged 2 commits into from
Aug 30, 2024

Conversation

jchavarri
Copy link
Member

No description provided.

@@ -65,6 +70,29 @@ module Of_json = struct
let option_of_json v_of_json (json : t) =
if (Obj.magic json : 'a Js.null) == Js.null then None
else Some (v_of_json json)

let result_of_json a_of_json b_of_json (json : t) =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks scary but as I understand you did this by using ppx like this and then massaging the code?

; echo "
type ('ok, 'err) result = ('ok, 'err) Stdlib.result = Ok of 'ok | Error of 'err [@@deriving json]
" | dune exec ./ppx/browser/ppx_deriving_json_js_test.exe -- -impl -
type ('ok, 'err) result = ('ok, 'err) Stdlib.result =
  | Ok of 'ok
  | Error of 'err [@@deriving json]
include
  struct
    let _ = fun (_ : ('ok, 'err) result) -> ()
    [@@@ocaml.warning "-39-11-27"]
    let rec result_of_json ok_of_json err_of_json =
      (fun x ->
         if Js.Array.isArray x
         then
           let array = (Obj.magic x : Js.Json.t array) in
           let len = Js.Array.length array in
           (if Stdlib.(>) len 0
            then
              let tag = Js.Array.unsafe_get array 0 in
              (if Stdlib.(=) (Js.typeof tag) "string"
               then
                 let tag = (Obj.magic tag : string) in
                 (if Stdlib.(=) tag "Ok"
                  then
                    (if Stdlib.(<>) len 2
                     then
                       Ppx_deriving_json_runtime.of_json_error
                         "expected a JSON array of length 2";
                     Ok (ok_of_json (Js.Array.unsafe_get array 1)))
                  else
                    if Stdlib.(=) tag "Error"
                    then
                      (if Stdlib.(<>) len 2
                       then
                         Ppx_deriving_json_runtime.of_json_error
                           "expected a JSON array of length 2";
                       Error (err_of_json (Js.Array.unsafe_get array 1)))
                    else
                      Ppx_deriving_json_runtime.of_json_error "invalid JSON")
               else
                 Ppx_deriving_json_runtime.of_json_error
                   "expected a non empty JSON array with element being a string")
            else
              Ppx_deriving_json_runtime.of_json_error
                "expected a non empty JSON array")
         else
           Ppx_deriving_json_runtime.of_json_error
             "expected a non empty JSON array" : Js.Json.t ->
                                                   ('ok, 'err) result)
    let _ = result_of_json
    [@@@ocaml.warning "-39-11-27"]
    let rec result_to_json ok_to_json err_to_json =
      (fun x ->
         match x with
         | Ok x_0 ->
             (Obj.magic [|(string_to_json "Ok");(ok_to_json x_0)|] :
             Js.Json.t)
         | Error x_0 ->
             (Obj.magic [|(string_to_json "Error");(err_to_json x_0)|] :
             Js.Json.t) : ('ok, 'err) result -> Js.Json.t)
    let _ = result_to_json
  end[@@ocaml.doc "@inline"][@@merlin.hide ]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as I understand you did this by using ppx like this and then massaging the code?

Correct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed that your example had better naming for the function params. I fixed that directly in main.

@jchavarri jchavarri merged commit d099d3e into main Aug 30, 2024
7 checks passed
@jchavarri jchavarri deleted the ppx-add-result-runtime branch August 30, 2024 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants