Commits

camlspotter committed 52c6dea

update

  • Participants
  • Parent commits 1543496

Comments (0)

Files changed (10)

 
 ``t mc_embeded``
 
+``t mc_option_embeded``
+
+
 Writing encoders and decoders for a new target type
 ================================================================
 
 Modules and values to be accessible
 -------------------------------------
 
-For actual example, see an example in ``meta_conv/json`` or ``meta_conv/ocaml`` directory.
+For actual examples, see ``meta_conv/json`` or ``meta_conv/ocaml`` directory.
 
-``Meta_conv``
+Link with ``meta_conv``
 
-    ``Meta_conv`` packed module library must be linked with programs which use meta_conv.
+    ``meta_conv`` packed module library must be linked with programs which use meta_conv.
 
 Conversion module
 

File json/json_conv.ml

   f
 
 let lazy_t_of_json d = generic_lazy_t_of (fun e -> raise (Error e)) d
+
+let json_of_mc_lazy_t = generic_of_mc_lazy_t
+  
 let mc_lazy_t_of_json (d : 'a decoder) = (generic_mc_lazy_t_of d : ('a, Json.t) mc_lazy_t decoder)
 
 let json_of_mc_fields enc xs = Object (List.map (fun (name, a) -> (name, enc a)) xs)

File json/json_conv.mli

 val json_of_array     : 'a encoder -> 'a array encoder
 val json_of_option    : 'a encoder -> 'a option encoder
 val json_of_lazy_t    : 'a encoder -> 'a Lazy.t encoder
-(* val json_of_mc_lazy_t : 'a encoder -> ('a, target) mc_lazy_t encoder *)
+val json_of_mc_lazy_t : 'a encoder -> ('a, target) mc_lazy_t encoder
 val json_of_mc_fields : 'a encoder -> (string * 'a) list encoder
 
 val int_of_json       : int decoder

File lib/OMakefile

 LIBFILES[] =
    result
    error
+   open
    types
    internal
-   open
 
 LIB = meta_conv
 

File lib/internal.ml

 open Types
 open Error
 open Result
+open Open
 
 (** { 6 Misc functions } *)
 
     | `Error e -> errorf e
   ))
 
+let generic_of_mc_lazy_t e = fun v ->
+  match Lazy.force v with
+  | `Ok a -> e a
+  | `Error (_, a, _) -> a
+
 let generic_mc_lazy_t_of f ?trace:_ v = 
   `Ok (lazy (f ?trace:None v)) (* trace is reset, to avoid leak *)
 
   let format_error ppf (desc,_,_) = Error.format_desc ppf desc
   let format_full_error = Error.format A.format
 
+  let format_with encoder ppf t = format ppf (encoder t)
+
   (** Hashtbl coders via list *)
 
   let generic_of_hashtbl of_list of_a of_b tbl =
     List.iter (fun (k,v) -> Hashtbl.add tbl k v) abs;
     `Ok tbl
 
-
-  let format_with encoder ppf t = format ppf (encoder t)
-
 end

File lib/internal.mli

     normally it should raises <Target_conv>.Error exception.
 *)
 
+val generic_of_mc_lazy_t :
+  ('host, 'target) Encoder.t
+  -> (('host, 'target) mc_lazy_t, 'target) Encoder.t
+(** typical <targe_type>_of_mc_lazy_t *)
+
 val generic_mc_lazy_t_of : 
   ('host, 'target) Decoder.t
-  -> ?trace: 'target Error.trace 
-  -> 'target
-  -> [> `Ok of ('host, 'target Error.t) Result.t lazy_t ]
+  -> (('host, 'target) mc_lazy_t, 'target) Decoder.t
 (** typical mc_lazy_t_of_<targe_type> 
     The decoding is done lazily. 
     Error at the lazy decoding is reported by the result monad.
 type ('host, 'target) mc_result = ('host, 'target Error.t) Result.t
 type 'a mc_embeded = 'a
 type 'a mc_option_embeded = 'a option
+
+type ('a, 'b) hashtbl = ('a, 'b) Hashtbl.t

File lib/template.mli

+open Meta_conv.Types
+open Meta_conv.Open
+open Foobar 
+(** You may want to open the module for the target data. *)
+
+(** The module Foobar *must* define the target type as [t]. *)
+
+include S with type target = Foobar.t
+(** You must define the primitive coding module of type [S with type target = Foobar.t] 
+
+    This is created by
+
+    [include Meta_conv.Internal.Make(struct
+      ...
+      end)]
+
+    where the argument module has the module type [Types.Min with type target = Foobar.t].
+*)
+
+(** Encoders of primitive types. 
+    You do not need to implemente all of them, 
+    but auto-generation of derived type encoders depending 
+    on unimplemented primitve encoders fail.
+
+    There are helper functions in Internal to implement these encoders,
+    see [generic_<tycon>_of] functions.
+*)
+val foobar_of_int       : int encoder
+val foobar_of_nativeint : nativeint encoder
+val foobar_of_unit      : unit encoder
+val foobar_of_bool      : bool encoder
+val foobar_of_int32     : int32 encoder
+val foobar_of_int64     : int64 encoder
+val foobar_of_float     : float encoder
+val foobar_of_char      : char encoder
+val foobar_of_string    : string encoder
+val foobar_of_list      : 'a encoder -> 'a list encoder
+val foobar_of_array     : 'a encoder -> 'a array encoder
+val foobar_of_option    : 'a encoder -> 'a option encoder
+val foobar_of_lazy_t    : 'a encoder -> 'a Lazy.t encoder
+
+(** Decoders of primitive types. 
+    You do not need to implemente all of them, 
+    but auto-generation of derived type decoders depending 
+    on unimplemented primitve decoders fail.
+
+    There are helper functions in Internal to implement these decoders,
+    see [generic_of_<tycon>] functions.
+*)
+val int_of_foobar       : int decoder
+val nativeint_of_foobar : nativeint decoder
+val unit_of_foobar      : unit decoder
+val bool_of_foobar      : bool decoder
+val int32_of_foobar     : int32 decoder
+val int64_of_foobar     : int64 decoder
+val float_of_foobar     : float decoder
+val char_of_foobar      : char decoder
+val string_of_foobar    : string decoder
+val list_of_foobar      : 'a decoder -> 'a list decoder
+val array_of_foobar     : 'a decoder -> 'a array decoder
+val option_of_foobar    : 'a decoder -> 'a option decoder
+val lazy_t_of_foobar    : 'a decoder -> 'a lazy_t decoder
+
+(** Encoder and Decoder of Hashtbl.t 
+
+    They can be defined using the helper functions [generic_of_hashtbl] 
+    and [generic_hashtbl_of] using [('a * 'b) list] as actual encoding/decoding. 
+    The helpers are created by [Internal.Make(A)].
+
+    Note that you must use the type name [hashtbl] instead of [Hashtbl.t],
+    unless somehow you extend [Hashtbl] module with [Hashtbl.foobar_of_t]
+    and [Hashtbl.t_of_foobar]. The alias of [hashtbl] is available in module [Open].
+*)
+val foobar_of_hashtbl   : 'a encoder -> 'b encoder -> ('a, 'b) Hashtbl.t encoder
+val hashtbl_of_foobar   : 'a decoder -> 'b decoder -> ('a, 'b) Hashtbl.t decoder
+
+(** Encoders of special types *)
+val foobar_of_mc_lazy_t : 'a encoder -> ('a, foobar) mc_lazy_t encoder
+val foobar_of_mc_fields : 'a encoder -> (string * 'a) list encoder
+
+(** Decoders of special types *)
+val mc_lazy_t_of_foobar : 'a decoder -> ('a, foobar) mc_lazy_t decoder
+val mc_fields_of_foobar : 'a decoder -> (string * 'a) list decoder

File lib/types.ml

   val from_Ok : [< ('a, target Error.t) Result.t ] -> 'a
   (** If the argument is [`Error e], raises [Error e]. *)
 
-  (** Hashtbl coders via list *)    
-
-  val generic_of_hashtbl : 
-    (target encoder -> target list encoder) (** target_of_list *)
-    -> 'a encoder (** key encoder *)
-    -> 'b encoder (** value encoder *)
-    -> ('a,'b) Hashtbl.t encoder
-
-  val generic_hashtbl_of : 
-    (('a * 'b) decoder -> ('a * 'b) list decoder) (** list_of_target *)
-    -> 'a decoder (** key encoder *)
-    -> 'b decoder (** value encoder *)
-    -> ('a, 'b) Hashtbl.t decoder
-
   open Format
 
   val format_error : formatter -> target Error.t -> unit
 
   val format_with : ('host -> target) -> Format.formatter -> 'host -> unit
   (** Format host data using its encoder *)    
+
+  (** Hashtbl coders via list *)    
+  
+  val generic_of_hashtbl : 
+    (target encoder -> target list encoder) (** target_of_list *)
+    -> 'a encoder (** key encoder *)
+    -> 'b encoder (** value encoder *)
+    -> ('a,'b) Hashtbl.t encoder
+
+  val generic_hashtbl_of : 
+      (('a * 'b) decoder -> ('a * 'b) list decoder) (** list_of_target *)
+      -> 'a decoder (** key encoder *)
+      -> 'b decoder (** value encoder *)
+      -> ('a, 'b) Hashtbl.t decoder
 end

File ocaml/ocaml_conv.ml

 
 include (val arch)
 
-let ocaml_of_hashtbl x = generic_of_hashtbl ocaml_of_list x
+let ocaml_of_hashtbl x = AutoGenerated.of_hashtbl ocaml_of_list x
 
-let hashtbl_of_ocaml x = generic_hashtbl_of list_of_ocaml x
+let hashtbl_of_ocaml x = AutoGenerated.hashtbl_of list_of_ocaml x