Micheline: versioned serializer for retrocompatibility

This commit is contained in:
Benjamin Canou 2018-07-19 19:25:13 +02:00
parent a7eb0cdf36
commit 70fddce545
5 changed files with 29 additions and 5 deletions

View File

@ -141,7 +141,9 @@ let rec map_node fl fp = function
| Prim (loc, name, seq, annots) -> | Prim (loc, name, seq, annots) ->
Prim (fl loc, fp name, List.map (map_node fl fp) seq, annots) Prim (fl loc, fp name, List.map (map_node fl fp) seq, annots)
let canonical_encoding ~variant prim_encoding = type semantics = V0 | V1
let internal_canonical_encoding ~semantics ~variant prim_encoding =
let open Data_encoding in let open Data_encoding in
let int_encoding = let int_encoding =
obj1 (req "int" z) in obj1 (req "int" z) in
@ -171,7 +173,7 @@ let canonical_encoding ~variant prim_encoding =
(fun args -> Seq (0, args)) in (fun args -> Seq (0, args)) in
let annots_encoding = let annots_encoding =
let split s = let split s =
if s = "" then [] if s = "" && semantics <> V0 then []
else else
let annots = String.split_on_char ' ' s in let annots = String.split_on_char ' ' s in
List.iter (fun a -> List.iter (fun a ->
@ -270,6 +272,13 @@ let canonical_encoding ~variant prim_encoding =
(fun node -> strip_locations node) (fun node -> strip_locations node)
node_encoding node_encoding
let canonical_encoding ~variant prim_encoding =
internal_canonical_encoding ~semantics:V1 ~variant prim_encoding
let canonical_encoding_v1 ~variant prim_encoding =
internal_canonical_encoding ~semantics:V1 ~variant prim_encoding
let canonical_encoding_v0 ~variant prim_encoding =
internal_canonical_encoding ~semantics:V0 ~variant prim_encoding
let table_encoding ~variant location_encoding prim_encoding = let table_encoding ~variant location_encoding prim_encoding =
let open Data_encoding in let open Data_encoding in
conv conv

View File

@ -75,6 +75,13 @@ val canonical_location_encoding : canonical_location Data_encoding.encoding
different primitive encodings are used in the same schema. *) different primitive encodings are used in the same schema. *)
val canonical_encoding : variant:string -> 'l Data_encoding.encoding -> 'l canonical Data_encoding.encoding val canonical_encoding : variant:string -> 'l Data_encoding.encoding -> 'l canonical Data_encoding.encoding
(** Old version of {!canonical_encoding} for retrocompatibility.
Do not use in new code. *)
val canonical_encoding_v0 : variant:string -> 'l Data_encoding.encoding -> 'l canonical Data_encoding.encoding
(** Alias for {!canonical_encoding}. *)
val canonical_encoding_v1 : variant:string -> 'l Data_encoding.encoding -> 'l canonical Data_encoding.encoding
(** Compute the canonical form of an expression. (** Compute the canonical form of an expression.
Drops the concrete locations completely. *) Drops the concrete locations completely. *)
val strip_locations : (_, 'p) node -> 'p canonical val strip_locations : (_, 'p) node -> 'p canonical

View File

@ -38,9 +38,11 @@ type canonical_location = int
val root : 'p canonical -> (canonical_location, 'p) node val root : 'p canonical -> (canonical_location, 'p) node
val canonical_location_encoding : canonical_location Data_encoding.encoding val canonical_location_encoding : canonical_location Data_encoding.encoding
val canonical_encoding : variant:string -> 'l Data_encoding.encoding -> 'l canonical Data_encoding.encoding val canonical_encoding : variant:string -> 'l Data_encoding.encoding -> 'l canonical Data_encoding.encoding
val canonical_encoding_v1 : variant:string -> 'l Data_encoding.encoding -> 'l canonical Data_encoding.encoding
(*
val erased_encoding : variant:string -> 'l -> 'p Data_encoding.encoding -> ('l, 'p) node Data_encoding.encoding val erased_encoding : variant:string -> 'l -> 'p Data_encoding.encoding -> ('l, 'p) node Data_encoding.encoding
val table_encoding : variant:string -> 'l Data_encoding.encoding -> 'p Data_encoding.encoding -> ('l, 'p) node Data_encoding.encoding val table_encoding : variant:string -> 'l Data_encoding.encoding -> 'p Data_encoding.encoding -> ('l, 'p) node Data_encoding.encoding
*)
val location : ('l, 'p) node -> 'l val location : ('l, 'p) node -> 'l
val annotations : ('l, 'p) node -> string list val annotations : ('l, 'p) node -> string list

View File

@ -600,7 +600,11 @@ module Make (Context : CONTEXT) = struct
| Ok v -> Lwt.return (Ok (Some v)) | Ok v -> Lwt.return (Ok (Some v))
end end
module Micheline = Micheline module Micheline = struct
include Micheline
let canonical_encoding_v1 = canonical_encoding_v1
let canonical_encoding = canonical_encoding_v0
end
module Logging = Logging.Make(Param) module Logging = Logging.Make(Param)
module Updater = struct module Updater = struct

View File

@ -35,8 +35,10 @@ type lazy_expr = expr Data_encoding.lazy_t
type node = (location, Michelson_v1_primitives.prim) Micheline.node type node = (location, Michelson_v1_primitives.prim) Micheline.node
let expr_encoding = let expr_encoding =
Micheline.canonical_encoding Micheline.canonical_encoding_v1
~variant:"michelson_v1" ~variant:"michelson_v1"
Michelson_v1_primitives.prim_encoding Michelson_v1_primitives.prim_encoding