Commits

camlspotter  committed dfc450e

fix for mc_option_embeded and a small test for it

  • Participants
  • Parent commits 9e9fc4b
  • Branches dev

Comments (0)

Files changed (3)

File json/tests/test.ml

     Format.eprintf "%a@." Json_conv.format j';
     assert (j = j')
 end
+
+module TestOptionEmbeded = struct
+  type a = { a_x : int; a_y : bool }
+  and b = { b_x : float; b_y : char }
+  and t = { a : a mc_embeded;
+            b : b mc_option_embeded;
+            foo : int;
+            bar : int;
+            unknowns : Json.t mc_leftovers;
+          } with conv(json)
+
+  module X = struct
+    type t = { a_x : int; a_y : bool;
+               foo : int; bar : int;
+               b_x : float; (* b_y : char; *) (* b_y missing, thus b mc_option_embeded becomes None *)
+               z : string } with conv(json)
+
+  end
+
+  let () = 
+    let j = X.json_of_t { X.a_x= 1; a_y= false; b_x= 4.2; foo= 42; bar= 100; z= "unknown" } in
+    let t = t_of_json_exn j in
+    Format.eprintf "%a@." Json_conv.format j;
+    let j' = json_of_t t in
+    Format.eprintf "%a@." Json_conv.format j';
+    assert (j = j')
+end
 type ('host, 'target) mc_lazy_t = ('host, 'target Error.t) Result.t lazy_t
 type ('host, 'target) mc_result = ('host, 'target Error.t) Result.t
 type 'a mc_embeded = 'a
+type 'a mc_option_embeded = 'a option

File pa/pa_meta_conv.ml

 
       | { embeded = Some (`Option_embeded, ctyp); _ } -> 
           let f = gen_ctyp (strip_field_flags ctyp) in
-          <:expr< match $f$ $accessor lab_id$ with
+          <:expr< match $accessor lab_id$ with
                   | None -> []
                   | Some v -> 
                       $id:module_path$.Decode.$id: match kind with 
                                                | `Record -> <:ident<record>> 
-                                               | `Object -> <:ident<object_>>$ v
+                                               | `Object -> <:ident<object_>>$ ($f$ v)
           >> :: fields
 
       | { name = s; optional = Some ctyp; _ } ->
                 Meta_conv.Internal.embeded_decoding_helper secondary_fields v
                   ($gen_ctyp ctyp$ ~trace:(`Field $str:name_of_ident lab_id$ :: __t) v) 
               with
-              | `Ok (v, rest_fields)  -> `Ok (Some v, rest_fields)
-              | `Error _ -> `Ok (None, secondary_fields)
+              | `Ok (v, rest_fields)  -> Some v, rest_fields
+              | `Error _ -> None, secondary_fields
             >>
       in
       List.map gen_embeded embeded