summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2015-09-29 19:11:56 +0600
committerDaniil Baturin <daniil@baturin.org>2015-09-29 19:11:56 +0600
commit0acd427af061b35e613eaa0d80cfc80d1a93faff (patch)
tree5876fc78af72619e20acd9a5fafd26abc6e0644f
parent0e7c85bd1129c698aa65f25ebbbf52b1268b62c8 (diff)
downloadvyconf-0acd427af061b35e613eaa0d80cfc80d1a93faff.tar.gz
vyconf-0acd427af061b35e613eaa0d80cfc80d1a93faff.zip
Add message decoder implementation.
Restrict the Message module interface to the message types and decoder function.
-rw-r--r--src/message.ml45
-rw-r--r--src/message.mli20
2 files changed, 50 insertions, 15 deletions
diff --git a/src/message.ml b/src/message.ml
index ec28922..673d5e8 100644
--- a/src/message.ml
+++ b/src/message.ml
@@ -1,12 +1,49 @@
exception Invalid_operation of string
+exception Invalid_message of string
-type operation = {
+type operation =
+ | Set of string list
+ | Delete of string list
+ | Show of (string list option) * ((string * string) list option)
+
+type message = {
+ session_id: string;
+ ops: operation list
+}
+
+type raw_operation = {
method_name: string;
path: string list option;
options: (string * string) list option
} [@@deriving yojson]
-type message = {
- session_id: string;
- ops: operation list;
+type raw_message = {
+ raw_session_id: string;
+ raw_ops: raw_operation list;
} [@@deriving yojson]
+
+
+let value_of_path p =
+ match p with
+ | Some p -> p
+ | None -> raise (Invalid_operation "Operation requires a path")
+
+let decode_operation op =
+ let op_name = op.method_name in
+ match op_name with
+ | "set" -> Set (value_of_path op.path)
+ | "delete" -> Delete (value_of_path op.path)
+ | "show" -> Show (op.path, op.options)
+ | _ -> raise (Invalid_operation "Invalid operation name")
+
+let decode_message msg =
+ let id = msg.raw_session_id in
+ let ops = List.map decode_operation msg.raw_ops in
+ {session_id = id; ops = ops}
+
+let decode j =
+ let msg = raw_message_of_yojson j in
+ match msg with
+ | `Ok msg -> decode_message msg
+ | `Error str -> raise (Invalid_message str)
+
diff --git a/src/message.mli b/src/message.mli
index af92d67..2688b8c 100644
--- a/src/message.mli
+++ b/src/message.mli
@@ -1,16 +1,14 @@
-type operation = {
- method_name: string;
- path: string list option;
- options: (string * string) list option
-}
+exception Invalid_operation of string
+exception Invalid_message of string
+
+type operation =
+ | Set of string list
+ | Delete of string list
+ | Show of (string list option) * ((string * string) list option)
type message = {
session_id: string;
- ops: operation list;
+ ops: operation list
}
-val operation_to_yojson : operation -> Yojson.Safe.json
-val operation_of_yojson : Yojson.Safe.json -> [ `Error of bytes | `Ok of operation ]
-
-val message_to_yojson : message -> Yojson.Safe.json
-val message_of_yojson : Yojson.Safe.json -> [ `Error of bytes | `Ok of message ]
+val decode : Yojson.Safe.json -> message