How can I pass a user_type from an Eliom client to a coservice?

138 views Asked by At

http://ocsigen.org/eliom/manual/server-params#h5o-3 shows an example of a GET service that accepts a user-defined type. I'd like to call a coservice with a user_type from the client side, with the same type on the client side. It seems like it should be possible, but I get

ocsigenserver: main: Exn during page generation: Failure("User service parameters 'foo' type not supported client side.") (sending 500)

when I try

{shared{
  open Eliom_lib
  open Eliom_content
  open Html5.D

  type foo = A | B
  let foo_of_string = function "a" -> A | "b" -> B | _ -> raise (Failure "foo_of_string: unsupported foo")
  let string_of_foo = function A -> "a" | B -> "b"
}}

let foo_to_div foo : Html5_types.div Html5.elt Lwt.t = match foo with A -> Lwt.return (div [pcdata "aye"]) | B -> Lwt.return (div [pcdata "bee"])
let foo_service =
  Eliom_registration.Ocaml.register_post_coservice'
    ~post_params:Eliom_parameter.(user_type foo_of_string string_of_foo "foo")
    (fun () foo -> foo_to_div foo)

{client{
  let test () =
    Eliom_client.call_ocaml_service ~service:%foo_service () A
 }}


(* Boilerplate from eliom-distillery: *)
module Testing_app = Eliom_registration.App
  (struct let application_name = "testing" end)
let main_service =
  Eliom_service.App.service ~path:[] ~get_params:Eliom_parameter.unit ()
let () =
  Testing_app.register ~service:main_service
    (fun () () -> Lwt.return (Eliom_tools.F.html ~title:"foo"
                                Html5.F.(body [ pcdata "bar" ])))

I also tried using a server_function, but then I ran into the problem of how to get a div back as json; doing

type div_elt = Html5_types.div Eliom_content.Html5.elt
let div_elt_json = Json.t<div_elt>

gives me Error: Unbound module Html5_types.Json_div

1

There are 1 answers

0
unhammer On

By chance I scrolled down a bit after reading on server functions and found http://ocsigen.org/eliom/4.1/manual/clientserver-communication#h5o-5 which says to instead do

{shared{ 
  type foo = A | B deriving (Json)
  type foo_json = Json.t<foo>
}}
let foo_service =
  Eliom_registration.Ocaml.register_post_coservice'
    ~post_params:Eliom_parameter.(ocaml "param" foo_json)
    (fun () foo -> foo_to_div foo)

And this works =D

(Although I still would've preferred to use server_function, which seems a bit more concise.)