summaryrefslogtreecommitdiff
path: root/lib/value
diff options
context:
space:
mode:
Diffstat (limited to 'lib/value')
-rw-r--r--lib/value/data.ml28
-rw-r--r--lib/value/data.mli49
-rw-r--r--lib/value/dune4
-rw-r--r--lib/value/value.ml34
-rw-r--r--lib/value/value.mli46
5 files changed, 161 insertions, 0 deletions
diff --git a/lib/value/data.ml b/lib/value/data.ml
new file mode 100644
index 0000000..7cbc00f
--- /dev/null
+++ b/lib/value/data.ml
@@ -0,0 +1,28 @@
+(** 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 {!Model.Typ.t} stays in the model package; only the data
+ itself lives here. *)
+type ('a, 't) data =
+ | None
+ | V : ('a, 't) Model.Typ.t * 'a -> ('a, 't) data
+ | Seq : (int64, 't) Model.Typ.t * int64 Seq.t -> (int64 Seq.t, 't) data
+
+let none : ('a, 't) data = None
+
+let v : ('a, 't) Model.Typ.t * 'a -> ('a, 't) data =
+ fun (typ, value) -> V (typ, value)
+
+let seq : (int64, 't) Model.Typ.t * int64 Seq.t -> (int64 Seq.t, 't) data =
+ fun (typ, values) -> Seq (typ, values)
+
+type 't any = Any : ('a, 't) data -> 't any [@@unboxed]
+
+let any : ('a, 't) data -> 't any = fun data -> Any data
diff --git a/lib/value/data.mli b/lib/value/data.mli
new file mode 100644
index 0000000..0cb4856
--- /dev/null
+++ b/lib/value/data.mli
@@ -0,0 +1,49 @@
+(** 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 {!Model.Typ.t} stays in the model package; only the data
+ itself lives here. *)
+
+type ('a, 't) data = private
+ | None
+ | V : ('a, 't) Model.Typ.t * 'a -> ('a, 't) data
+ | Seq : (int64, 't) Model.Typ.t * int64 Seq.t -> (int64 Seq.t, 't) data
+ (** The constructors are private: a [data] can be pattern-matched from the
+ outside, but it can only be built with [none], [v] and [seq]. *)
+
+val none : ('a, 't) data
+(** [none] tells the field is empty in the database (or left as is, on update)
+*)
+
+val v : ('a, 't) Model.Typ.t * 'a -> ('a, 't) data
+(** [v (typ, value)] is the value for the field. The field type [typ] is also
+ reported in order to give the compiler the nature of the data, and the
+ result type records the type of the data held *)
+
+val seq : (int64, 't) Model.Typ.t * int64 Seq.t -> (int64 Seq.t, 't) data
+(** [seq (typ, values)] gets a sequence of values for a multivalued field *)
+
+(** 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 = private Any : ('a, 't) data -> 't any [@@unboxed]
+
+val any : ('a, '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 -> Model.target any = function
+ | "siret" -> any (v (Model.Typ.Text, Some "siret"))
+ | "name" -> any (v (Model.Typ.Text, Some "name"))
+ | _ -> any none
+ ] *)
diff --git a/lib/value/dune b/lib/value/dune
new file mode 100644
index 0000000..bcfb9a8
--- /dev/null
+++ b/lib/value/dune
@@ -0,0 +1,4 @@
+(library
+ (name value)
+ (libraries
+ model))
diff --git a/lib/value/value.ml b/lib/value/value.ml
new file mode 100644
index 0000000..38c2240
--- /dev/null
+++ b/lib/value/value.ml
@@ -0,0 +1,34 @@
+module Fields = Fields
+
+module Data = Data
+(** The data carried by a field: {!Data.data}, parameterized over the type of
+ the data held, built with the smart constructors [Data.none], [Data.v],
+ [Data.seq] *)
+
+(** A resolved field data: the data of a field of a content or a list, whatever
+ the type of the data held *)
+type value = Model.target Data.any
+
+let none : value = Data.any Data.none
+
+let single : ('a, Model.target) Model.Typ.t * 'a -> value =
+ fun data -> Data.any (Data.v data)
+
+let multi : (int64, Model.target) Model.Typ.t * int64 Seq.t -> value =
+ fun data -> Data.any (Data.seq data)
+
+(** A value: one row of a content or list table in the database.
+
+ The type parameter ['a] is the content or the static list whose table
+ holds the row: [Model.content] for contents ([Model.content Value.t]) and
+ [Model.list] for lists ([Model.list Value.t]). *)
+type 'a t = {
+ content : 'a;
+ (** The content or list whose table holds the row *)
+ id : int64;
+ (** The primary key of the row *)
+}
+
+let v : 'a -> int64 -> 'a t = fun content id -> { content; id }
+let content : 'a t -> 'a = fun t -> t.content
+let id : 'a t -> int64 = fun t -> t.id
diff --git a/lib/value/value.mli b/lib/value/value.mli
new file mode 100644
index 0000000..42482f7
--- /dev/null
+++ b/lib/value/value.mli
@@ -0,0 +1,46 @@
+(** Operations on the values stored in the database (fetching, updating, …).
+
+ A value is one row of a content or list table in the database. *)
+
+module Fields = Fields
+(** The fields of a value, each with its associated data *)
+
+module Data = Data
+(** The data carried by a field: {!Data.data}, parameterized over the type of
+ the data held, built with the smart constructors ([Data.none], [Data.v],
+ [Data.seq]); and {!Data.any}, the existential pack used when the held type
+ is not known statically *)
+
+type value = Model.target Data.any
+(** The data of a field of a content or a list, whatever the type of the data
+ held: an existential pack over {!Data.data} *)
+
+val none : value
+(** [none]: the field is empty (or left as is, on update) *)
+
+val single : ('a, Model.target) Model.Typ.t * 'a -> value
+(** [single (typ, data)]: a single value for the field, together with the type
+ [typ] of the field *)
+
+val multi : (int64, Model.target) Model.Typ.t * int64 Seq.t -> value
+(** [multi (typ, ids)]: a sequence of references for a multivalued field *)
+
+type 'a t
+(** A value: a row of the table of the content or list ['a], identified by its
+ [id].
+
+ The parameter ['a] is the content or the static list whose table holds the
+ row: [Model.content] for contents ([Model.content Value.t]) and [Model.list]
+ for lists ([Model.list Value.t]).
+
+ The type is abstract: a value is built with [v] and inspected with [content]
+ and [id]. *)
+
+val v : 'a -> int64 -> 'a t
+(** Build a value from its content or list and its row id *)
+
+val content : 'a t -> 'a
+(** The content or list the value belongs to *)
+
+val id : 'a t -> int64
+(** The primary key of the value in its table *)