如何将Eliom客户端的user_type传递给coservice?

时间:2014-11-21 11:12:38

标签: ocaml json-rpc ocsigen

http://ocsigen.org/eliom/manual/server-params#h5o-3显示了接受用户定义类型的GET服务的示例。我想从客户端调用带有user_type的coservice,在客户端使用相同的类型。看起来它应该是可能的,但我得到了

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

当我尝试

{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" ])))

我也试过使用server_function,但后来我遇到了如何将div作为json返回的问题;做

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

给了我Error: Unbound module Html5_types.Json_div

1 个答案:

答案 0 :(得分:1)

在读取服务器函数后,我偶然向下滚动了一下,发现http://ocsigen.org/eliom/4.1/manual/clientserver-communication#h5o-5代替了

{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)

这可行= D

(虽然我仍然倾向于使用server_function,这似乎更简洁。)