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
|
type 'a reference = {
t : 'a;
(** The target of the reference: at the resolved level a
{!Reference.target} (aliased as {!Model.target}). *)
constraints : string list Dsl.t option;
(** Optionally restrict the values allowed for this field, as a DSL
expression read from the optional [["constraints"]] key of the
configuration. Currently carried through to the consumers (e.g. to
filter the proposed values); [None] means no restriction. *)
}
(** A reference from a field to a target (a content or a static list).
The type stays parameterized over ['a] only to break the module dependency
cycle ([Reference] depends on [Content], which depends on [Field]/[Types]);
in practice ['a] is always {!Model.target}. Use the aliases
[Model.reference], [Model.content], [Model.field], ... at the use sites. *)
(** Each of the field types available in the application.
- ['db] is the representation of the data in the database (the column type).
- ['ocaml] is the representation of the data in the code — usually the same
as ['db], except for multivalued references, stored as one row per value
in a dedicated table (['db = int64]) but manipulated as a sequence in code
(['ocaml = int64 Seq.t]).
- ['t] is the type of the target in references fields *)
type ('db, 'ocaml, 't) t =
| Text : (string option, string option, 't) t
| Integer : (int64 option, int64 option, 't) t
| MonoReference : 't reference -> (int64 option, int64 option, 't) t
| MultiReference : 't reference -> (int64, int64 Seq.t, 't) t
(** Multireferences properties allows only one kind of content.
They are not encoded in the content table but in another table *)
type 'target monoreference = (int64 option, int64 option, 'target) t
(** The type of a reference field: the [int64] id of the target row in the
database, an [int64 option] in code. In practice ['target] is always
{!Model.target}. *)
type 'target multireference = (int64, int64 Seq.t, 'target) t
(** The type of a multivalued reference field: one [int64] per row in the
dedicated table (['db]), an [int64 Seq.t] in code (['ocaml]). In practice
['target] is always {!Model.target}. *)
(** Get the value to insert in the database when the field is empty *)
let default_value : type a b c. (a, b, c) t -> a = function
| Integer -> None
| Text -> None
| MonoReference _ -> None
| MultiReference _ -> raise Not_found
(** Constructor-level equality: [eq t1 t2] is [Some Equal] when [t1] and [t2]
have the same constructor. The pair ['a * 'x] is injective, so [Equal]
proves both ['a = 'b] (the database representations) and ['x = 'y] (the code
representations) at once *)
let eq : type a b x y c.
(a, x, c) t -> (b, y, c) t -> (a * x, b * y) Type.eq option =
fun t1 t2 ->
match (t1, t2) with
| MultiReference _, MultiReference _ -> Some Type.Equal
| MonoReference _, MonoReference _ -> Some Type.Equal
| Integer, Integer -> Some Type.Equal
| Text, Text -> Some Type.Equal
| _ -> None
|