summaryrefslogtreecommitdiff
path: root/lib/model
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-11-21 11:23:41 +0100
committerSébastien Dailly <sebastien@dailly.me>2026-11-21 11:23:41 +0100
commitce13c93cf14cb71894481b0303a7058a72a6f4ce (patch)
treebd93186a65f6645cb4401f188d266d743cc2fde7 /lib/model
parent172a02b80d8dbdcfe09129d7fa76015bb5ec4d79 (diff)
Load values from the database
Diffstat (limited to 'lib/model')
-rw-r--r--lib/model/data.ml24
-rw-r--r--lib/model/data.mli45
-rw-r--r--lib/model/model.ml5
-rw-r--r--lib/model/model.mli6
4 files changed, 78 insertions, 2 deletions
diff --git a/lib/model/data.ml b/lib/model/data.ml
new file mode 100644
index 0000000..9c44203
--- /dev/null
+++ b/lib/model/data.ml
@@ -0,0 +1,24 @@
+(** The data carried by a field of a value.
+
+ [None] tells the field is empty (or left as is, on update), [V] carries a
+ single value together with the type of its field, and [Seq] carries a
+ sequence of references for a multivalued field.
+
+ The type is parameterized over the type of the data held (['a]: the DB
+ representation of the field, e.g. [string option] or [int64 option]) and
+ over ['t], the target of the field for references — a reference is held as
+ an [int64] (the id of the target row), while ['t] records what it points to.
+ The field type {!Typ.t} stays in the model package; only the data itself
+ lives here. *)
+type ('ocaml, 't) data =
+ | None
+ | V : ('db, 'ocaml, 't) Typ.t * 'ocaml -> ('ocaml, 't) data
+
+let none : ('ocaml, 't) data = None
+
+let v : ('db, 'ocaml, 't) Typ.t * 'ocaml -> ('ocaml, 't) data =
+ fun (typ, value) -> V (typ, value)
+
+type 't any = Any : ('ocaml, 't) data -> 't any [@@unboxed]
+
+let any : ('ocaml, 't) data -> 't any = fun data -> Any data
diff --git a/lib/model/data.mli b/lib/model/data.mli
new file mode 100644
index 0000000..6773589
--- /dev/null
+++ b/lib/model/data.mli
@@ -0,0 +1,45 @@
+(** The data carried by a field of a value.
+
+ [none] tells the field is empty (or left as is, on update), and [v] carries
+ the value of the field together with its type — a single value, or a
+ sequence of references for a multivalued field.
+
+ The type is parameterized over ['ocaml], the in-code representation of the
+ data held (e.g. [string option], [int64 option], or [int64 Seq.t] for a
+ multivalued reference), and over ['t], the target of the field for
+ references. The DB representation ['db] appears only inside the field type
+ carried by the constructors. The field type {!Typ.t} stays in the model
+ package; only the data itself lives here. *)
+
+type ('ocaml, 't) data = private
+ | None
+ | V : ('db, 'ocaml, 't) Typ.t * 'ocaml -> ('ocaml, 't) data
+
+val none : ('ocaml, 't) data
+(** [none] tells the field is empty in the database (or left as is, on update)
+*)
+
+val v : ('db, 'ocaml, 't) Typ.t * 'ocaml -> ('ocaml, 't) data
+(** [v (typ, value)] is the value for the field, in its code representation
+ ['ocaml] — e.g. a single [string option], or an [int64 Seq.t] for a
+ multivalued reference. The field type [typ] is also reported: it ties the
+ payload to the field and determines the result type. Whether the value is a
+ sequence is carried by the type, not by a separate constructor. *)
+
+(** Existential pack over the held type: the data of a field whose type is not
+ known statically (e.g. the data of any field of a content, as returned by a
+ provider function). Matchable from the outside, but built with [any]. *)
+type 't any = Any : ('ocaml, 't) data -> 't any [@@unboxed]
+
+val any : ('ocaml, 't) data -> 't any
+(** Pack a typed [data] into an [any], hiding the type of the data held *)
+
+(** Example for inserting data — build the data of each field, then pack it:
+ [
+ (* Function providing the data of each field to insert in the database *)
+
+ let f : string -> Reference.target any = function
+ | "siret" -> any (v (Typ.Text, Some "siret"))
+ | "name" -> any (v (Typ.Text, Some "name"))
+ | _ -> any none
+ ] *)
diff --git a/lib/model/model.ml b/lib/model/model.ml
index 0f016ad..c3e5b5b 100644
--- a/lib/model/model.ml
+++ b/lib/model/model.ml
@@ -5,6 +5,7 @@
module Identifier = Identifier
module Dsl = Dsl
module Typ = Typ
+module Data = Data
module Reference = Reference
module DataList = Data_list
module Path = Path
@@ -12,9 +13,9 @@ module Field = Field
module Section = Section
module Content = Content
-(** The target of a reference field, resolved: a lazy pointer to a content, or
- a static list. *)
type target = Reference.target
+(** The target of a reference field, resolved: a lazy pointer to a content, or a
+ static list. *)
type list = target Data_list.t
type content = target Content.t
diff --git a/lib/model/model.mli b/lib/model/model.mli
index a8941d9..0326eb2 100644
--- a/lib/model/model.mli
+++ b/lib/model/model.mli
@@ -119,6 +119,12 @@ module Reference = Reference
type reference = target Typ.reference
(** A reference field target: a {!target}, plus optional constraints *)
+module Data = Data
+(** The data carried by a field of a value: {!Data.data} (parameterized over the
+ in-code representation of the data held), built with the smart constructors
+ {!Data.none} and {!Data.v}, and {!Data.any}, the existential pack used when
+ the held type is not known statically. *)
+
module DataList = Data_list
(** A static list declared in the configuration: its entries are loaded by the
data provider and the list can be referenced from fields, but it is not