summaryrefslogtreecommitdiff
path: root/lib/model/typ.ml
blob: 0e94378f0dbcba073a8cf7e536029d881f404310 (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
type 'a reference = {
  t : 'a;
      (** The target of the reference: a [Model.Reference.t], a lazy pointer to
          the content. *)
  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 content.

    The target [t] is a {!Model.Reference.t}: a lazy pointer to the content,
    created while the configuration is parsed (see [Configuration.Read_conf])
    and resolved once all the contents are registered. The laziness is what
    allows contents to reference each other cyclically.

    The type stays parameterized over ['a] only to break the module dependency
    cycle ([Reference] depends on [Content], which depends on [Field]/[Typ]); in
    practice ['a] is always [Model.Reference.t]. Use the aliases
    [Model.reference], [Model.content], [Model.field], ... at the use sites. *)

(** Each of the field types available in the application.

    - The first type parameter match the DB data for the content.
    - The second is the type of the target in references fields *)
type ('db, 't) t =
  | Text : (string option, 't) t
  | Integer : (int64 option, 't) t
  | MonoReference : 't reference -> (int64 option, 't) t
  | MultiReference : 't reference -> (int64, 't) t
      (** Multireferences properties allows only one kind of content.

          They are not encoded in the content table but in another table *)

(** Get the value to insert in the database when the field is empty *)
let default_value : type a. (a, _) t -> a = function
  | Integer -> None
  | Text -> None
  | MonoReference _ -> None
  | MultiReference _ -> raise Not_found

let eq : type a b c. (a, c) t -> (b, c) t -> (a, b) 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

(** Represent the value in the database for a given field.

Example for inserting data
[
  (* Function providing a value for each field to insert in the database *)

  let f : string -> Model.value = function
  | "siret" -> V (Model.Typ.Text, Some "siret")
  | "name" -> V  (Model.Typ.Text, Some "name")
  | _ -> None

  let* id = Query.insert db entity_content f in
]

*)
type 't value =
  | None  (** None tell the field is empty in the database *)
  | V : ('a option, 't) t * 'a option -> 't value
      (** V is the value for the field. The field type is also reported in order
          to give the compiler the nature of the data *)
  | Seq : (int64, 't) t * int64 Seq.t -> 't value
      (** Get a sequence of values for a multivaluated field *)