Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjbray committed Jun 15, 2022
1 parent 6112f8d commit 2a034e8
Showing 1 changed file with 59 additions and 58 deletions.
117 changes: 59 additions & 58 deletions src/decode.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(** Functors for creating Decoders. *)

open Decoders_util
module U = Decoders_util
open U

type ('good, 'bad) result = ('good, 'bad) My_result.t =
| Ok of 'good
Expand Down Expand Up @@ -228,6 +229,63 @@ module Make (Decodeable : Decodeable) :
e )


let keys' : 'k decoder -> 'k list decoder =
fun key_decoder value ->
match Decodeable.get_key_value_pairs value with
| Some assoc ->
assoc
|> List.map (fun (key, _) -> key_decoder key)
|> combine_errors
|> My_result.map_err
(Error.tag_group "Failed while decoding the keys of an object")
| None ->
(fail "Expected an object") value


let keys = keys' string

let key_value_pairs' : 'k decoder -> 'v decoder -> ('k * 'v) list decoder =
fun key_decoder value_decoder value ->
match Decodeable.get_key_value_pairs value with
| Some assoc ->
assoc
|> List.map
My_result.Infix.(
fun (key_val, value_val) ->
key_decoder key_val
>>= fun key ->
value_decoder value_val >|= fun value -> (key, value))
|> combine_errors
|> My_result.map_err
(Error.tag_group "Failed while decoding key-value pairs")
| None ->
(fail "Expected an object") value


let key_value_pairs value_decoder = key_value_pairs' string value_decoder

let key_value_pairs_seq' : 'k decoder -> ('k -> 'v decoder) -> 'v list decoder
=
fun key_decoder value_decoder value ->
match Decodeable.get_key_value_pairs value with
| Some assoc ->
assoc
|> List.map
My_result.Infix.(
fun (key_val, value_val) ->
key_decoder key_val
>>= fun key -> (value_decoder key) value_val)
|> combine_errors
|> My_result.map_err
(Error.tag_group "Failed while decoding key-value pairs")
| None ->
(fail "Expected an object") value


let key_value_pairs_seq value_decoder =
key_value_pairs_seq' string value_decoder


let field : string -> 'a decoder -> 'a decoder =
fun key value_decoder t ->
let value =
Expand Down Expand Up @@ -343,63 +401,6 @@ module Make (Decodeable : Decodeable) :
fail "Must provide at least one key to 'at'"


let keys' : 'k decoder -> 'k list decoder =
fun key_decoder value ->
match Decodeable.get_key_value_pairs value with
| Some assoc ->
assoc
|> List.map (fun (key, _) -> key_decoder key)
|> combine_errors
|> My_result.map_err
(Error.tag_group "Failed while decoding the keys of an object")
| None ->
(fail "Expected an object") value


let keys = keys' string

let key_value_pairs' : 'k decoder -> 'v decoder -> ('k * 'v) list decoder =
fun key_decoder value_decoder value ->
match Decodeable.get_key_value_pairs value with
| Some assoc ->
assoc
|> List.map
My_result.Infix.(
fun (key_val, value_val) ->
key_decoder key_val
>>= fun key ->
value_decoder value_val >|= fun value -> (key, value))
|> combine_errors
|> My_result.map_err
(Error.tag_group "Failed while decoding key-value pairs")
| None ->
(fail "Expected an object") value


let key_value_pairs value_decoder = key_value_pairs' string value_decoder

let key_value_pairs_seq' : 'k decoder -> ('k -> 'v decoder) -> 'v list decoder
=
fun key_decoder value_decoder value ->
match Decodeable.get_key_value_pairs value with
| Some assoc ->
assoc
|> List.map
My_result.Infix.(
fun (key_val, value_val) ->
key_decoder key_val
>>= fun key -> (value_decoder key) value_val)
|> combine_errors
|> My_result.map_err
(Error.tag_group "Failed while decoding key-value pairs")
| None ->
(fail "Expected an object") value


let key_value_pairs_seq value_decoder =
key_value_pairs_seq' string value_decoder


let decode_value (decoder : 'a decoder) (input : value) : ('a, error) result =
decoder input

Expand Down

0 comments on commit 2a034e8

Please sign in to comment.