blob: 3f9e2b9a9d6a97c393e59c3b956c0f9f7076795b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
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)
| GetValues of string list
| Commit
type request = {
session_id: string;
operations: operation list;
}
type response = {
errors: string list;
warnings: string list;
data: string list;
} [@@deriving yojson]
type raw_operation = {
method_name: string;
path: string list option;
options: (string * string) list option
} [@@deriving yojson]
type raw_request = {
raw_session_id: string;
raw_operations: 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)
| "get_values" ->
(match op.path with
| Some path -> GetValues path
| None -> raise (Invalid_operation "Operation requires a path"))
| "commit" -> Commit
| _ -> raise (Invalid_operation "Invalid operation name")
let encode_raw_operation op =
match op with
| Set path -> {method_name = "set"; path = Some path; options = None}
| Delete path -> {method_name = "delete"; path = Some path; options = None}
| Show (path, options) -> {method_name = "show"; path = path; options = options}
| Commit -> {method_name = "commit"; path = None; options = None}
| _ -> raise (Invalid_operation "Unimplemented")
let decode_request j =
let req = raw_request_of_yojson j in
match req with
| `Ok req -> {session_id=req.raw_session_id; operations=(List.map decode_operation req.raw_operations)}
| `Error str -> raise (Invalid_message str)
let encode_request req =
let raw_req = {raw_session_id=req.session_id; raw_operations=(List.map encode_raw_operation req.operations)} in
raw_request_to_yojson raw_req
let encode_response = response_to_yojson
let decode_response j =
let result = response_of_yojson j in
match result with
| `Ok response -> response
| `Error str -> raise (Invalid_message str)
|