From e80ce1e8d55aaa842a3e1b19e162d9d23793b32f Mon Sep 17 00:00:00 2001 From: Benjamin Canou Date: Fri, 9 Sep 2016 17:02:43 +0200 Subject: [PATCH] Shell: add assoc combinator to Data_encoding. --- src/proto/environment/data_encoding.mli | 2 ++ src/utils/data_encoding.ml | 4 ++++ src/utils/data_encoding.mli | 2 ++ 3 files changed, 8 insertions(+) diff --git a/src/proto/environment/data_encoding.mli b/src/proto/environment/data_encoding.mli index c9fda6c94..32320d469 100644 --- a/src/proto/environment/data_encoding.mli +++ b/src/proto/environment/data_encoding.mli @@ -147,6 +147,8 @@ val merge_tups : 'a1 encoding -> 'a2 encoding -> ('a1 * 'a2) encoding val array : 'a encoding -> 'a array encoding val list : 'a encoding -> 'a list encoding +val assoc : 'a encoding -> (string * 'a) list encoding + type 't case val case : ?tag:int -> 'a encoding -> ('t -> 'a option) -> ('a -> 't) -> 't case diff --git a/src/utils/data_encoding.ml b/src/utils/data_encoding.ml index 9e601a3fc..e1c0f6798 100644 --- a/src/utils/data_encoding.ml +++ b/src/utils/data_encoding.ml @@ -719,6 +719,10 @@ module Encoding = struct `Variable in make @@ Mu (kind, name, self) + let assoc enc = + let json = Json_encoding.assoc (Json.get_json enc) in + let binary = list (tup2 string enc) in + raw_splitted ~json ~binary end include Encoding diff --git a/src/utils/data_encoding.mli b/src/utils/data_encoding.mli index 130ff4066..176d9edf1 100644 --- a/src/utils/data_encoding.mli +++ b/src/utils/data_encoding.mli @@ -155,6 +155,8 @@ val merge_tups : 'a1 encoding -> 'a2 encoding -> ('a1 * 'a2) encoding val array : 'a encoding -> 'a array encoding val list : 'a encoding -> 'a list encoding +val assoc : 'a encoding -> (string * 'a) list encoding + type 't case val case : ?tag:int -> 'a encoding -> ('t -> 'a option) -> ('a -> 't) -> 't case