Skip to content

Commit ef435ef

Browse files
committed
move tupleN to parent module and add empty_list
1 parent 2fd0d17 commit ef435ef

File tree

2 files changed

+62
-53
lines changed

2 files changed

+62
-53
lines changed

src/decode.ml

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,38 @@ module Make (Decodeable : Decodeable) :
329329
(fail "Expected a list").dec value
330330

331331

332+
let ( >>=:: ) fst rest = uncons rest fst
333+
334+
let empty_list =
335+
Decoder.of_decode_fun
336+
@@ fun value ->
337+
match Decodeable.get_list value with
338+
| Some [] ->
339+
Ok ()
340+
| Some _ ->
341+
(fail "Expected an empty list").dec value
342+
| None ->
343+
(fail "Expected a list").dec value
344+
345+
346+
let tuple2 d0 d1 =
347+
d0 >>=:: fun arg0 -> d1 >>=:: fun arg1 -> succeed (arg0, arg1)
348+
349+
350+
let tuple3 d0 d1 d2 =
351+
d0
352+
>>=:: fun arg0 ->
353+
d1 >>=:: fun arg1 -> d2 >>=:: fun arg2 -> succeed (arg0, arg1, arg2)
354+
355+
356+
let tuple4 d0 d1 d2 d3 =
357+
d0
358+
>>=:: fun arg0 ->
359+
d1
360+
>>=:: fun arg1 ->
361+
d2 >>=:: fun arg2 -> d3 >>=:: fun arg3 -> succeed (arg0, arg1, arg2, arg3)
362+
363+
332364
let rec at : string list -> 'a decoder -> 'a decoder =
333365
fun path decoder ->
334366
match path with
@@ -471,26 +503,5 @@ module Make (Decodeable : Decodeable) :
471503
custom (optional_decoder (at path value) val_decoder default) next
472504
end
473505

474-
module TupleHelper = struct
475-
let ( >>=:: ) fst rest = uncons rest fst
476-
477-
let tuple2 d0 d1 =
478-
d0 >>=:: fun arg0 -> d1 >>=:: fun arg1 -> succeed (arg0, arg1)
479-
480-
481-
let tuple3 d0 d1 d2 =
482-
d0
483-
>>=:: fun arg0 ->
484-
d1 >>=:: fun arg1 -> d2 >>=:: fun arg2 -> succeed (arg0, arg1, arg2)
485-
486-
487-
let tuple4 d0 d1 d2 d3 =
488-
d0
489-
>>=:: fun arg0 ->
490-
d1
491-
>>=:: fun arg1 ->
492-
d2 >>=:: fun arg2 -> d3 >>=:: fun arg3 -> succeed (arg0, arg1, arg2, arg3)
493-
end
494-
495506
include Infix
496507
end

src/sig.ml

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,36 @@ module type S = sig
117117
118118
(If you squint, the uncons operator [>>=::] kind of looks like the cons
119119
operator [::].)
120-
*)
120+
*)
121+
122+
val empty_list : unit decoder
123+
124+
val tuple2 : 'a decoder -> 'b decoder -> ('a * 'b) decoder
125+
(** Decode a collection into an OCaml 2-tuple.
126+
127+
For example, to decode this json:
128+
129+
{[
130+
[true, "string"]
131+
]}
132+
133+
we can use this decoder:
134+
135+
{[
136+
tuple2 bool string
137+
]}
138+
*)
139+
140+
val tuple3 : 'a decoder -> 'b decoder -> 'c decoder -> ('a * 'b * 'c) decoder
141+
(** Decode a collection into an OCaml 3-tuple. *)
142+
143+
val tuple4 :
144+
'a decoder
145+
-> 'b decoder
146+
-> 'c decoder
147+
-> 'd decoder
148+
-> ('a * 'b * 'c * 'd) decoder
149+
(** Decode a collection into an OCaml 4-tuple. *)
121150

122151
(** {1 Object primitives} *)
123152

@@ -296,37 +325,6 @@ module type S = sig
296325

297326
val custom : 'a decoder -> ('a -> 'b) decoder -> 'b decoder
298327
end
299-
300-
(** Helpers for decoding tuples which are encoded as lists *)
301-
module TupleHelper : sig
302-
val tuple2 : 'a decoder -> 'b decoder -> ('a * 'b) decoder
303-
(** Decode a collection into an OCaml 2-tuple.
304-
305-
For example, to decode this json:
306-
307-
{[
308-
[true, "string"]
309-
]}
310-
311-
we can use this decoder:
312-
313-
{[
314-
tuple2 bool string
315-
]}
316-
*)
317-
318-
val tuple3 :
319-
'a decoder -> 'b decoder -> 'c decoder -> ('a * 'b * 'c) decoder
320-
(** Decode a collection into an OCaml 3-tuple. *)
321-
322-
val tuple4 :
323-
'a decoder
324-
-> 'b decoder
325-
-> 'c decoder
326-
-> 'd decoder
327-
-> ('a * 'b * 'c * 'd) decoder
328-
(** Decode a collection into an OCaml 4-tuple. *)
329-
end
330328
end
331329

332330
(** Signature of things that can be decoded. *)

0 commit comments

Comments
 (0)