module Kojson_pattern: sig
.. end
Pattern Construction.
Pattern Construction.
module K: sig
.. end
Patterns for Matching JSON Trees.
module Ka: sig
.. end
Patterns for Matching Labelled Collections of JSON Trees.
val (^:) : string -> (Kojson.jin -> Kojson.jain -> 'a) -> Kojson.jain -> 'a
l^: f
matches an association with a label
l
such that
f
matches
the mapping of
l
and returns a pattern which matches the remainder.
This combinator can be chained into patterns matching a complete
association:
open Unprime
type connspec = {uri : string; timeout : int}
let decode_connspec =
let open Kojson in
K.assoc begin
"uri"^: K.string *> fun uri ->
"timeout"^: K.int *> fun timeout ->
Ka.stop {uri; timeout}
end
The above
*>
is the reversed composition operator
(fun f g x -> g (f
x))
.
val (^?:) : string -> (Kojson.jin option -> Kojson.jain -> 'a) -> Kojson.jain -> 'a
l^?: f
matches an association which either has no mapping for
l
and is
matched by
f None
or which maps
l
to some
v
with remainder
lvs
such that
f (Some v) lvs
verifies a match for both
v
and
lvs
. An
Option.map
function is convenient for constructing patterns on options:
let decode_certspec =
let open Kojson in
K.assoc begin
"certkey"^: K.string *> fun certkey ->
"cert"^?: Option.map K.string *> Option.get_or certkey *> fun cert ->
Ka.drop ["comment"] *>
Ka.stop (certkey, cert)
end