summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-11-20 16:23:53 +0100
committerSébastien Dailly <sebastien@dailly.me>2026-11-20 16:23:53 +0100
commitb05e2d2be7be28ee6c577671002797cf9abaf177 (patch)
tree0474684d619721024f0a6cb132cf2f4d2e84c344
parentc4ca180fd7c2085e3ce3f8e32aa0c2cb724f4c53 (diff)
New module for working with Values
-rw-r--r--lib/configuration/read_conf.ml11
-rw-r--r--lib/configuration/tests/errors.t35
-rw-r--r--lib/model/model.ml1
-rw-r--r--lib/model/model.mli12
-rw-r--r--lib/model/typ.ml22
-rw-r--r--lib/query/dune1
-rw-r--r--lib/query/encoder.ml22
-rw-r--r--lib/query/insert.ml63
-rw-r--r--lib/query/insert.mli15
-rw-r--r--lib/query/query.mli30
-rw-r--r--lib/query/update.ml23
-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
-rw-r--r--test/dune3
-rw-r--r--test/insert.ml35
-rw-r--r--test/insert_list.ml10
-rw-r--r--test/update.ml23
20 files changed, 337 insertions, 130 deletions
diff --git a/lib/configuration/read_conf.ml b/lib/configuration/read_conf.ml
index 14ee0b7..67703da 100644
--- a/lib/configuration/read_conf.ml
+++ b/lib/configuration/read_conf.ml
@@ -92,7 +92,16 @@ module Make (S : Decoders.Decode.S) = struct
S.succeed Model.Field.{ rule; message }
let field (env : env) : Model.field S.decoder =
- let* name = S.field "name" identifier
+ let* name =
+ S.field "name"
+ (S.and_then
+ (fun name ->
+ if Model.Identifier.equal name Model.Identifier.id then
+ S.fail
+ "the name \"id\" is reserved: it is the primary key of every \
+ row"
+ else S.succeed name)
+ identifier)
and* label = S.field "label" textI18n
and* type' = S.field "type" (field_type env)
and* required = S.field_opt_or ~default:false "required" S.bool
diff --git a/lib/configuration/tests/errors.t b/lib/configuration/tests/errors.t
index b23f237..c4552d3 100644
--- a/lib/configuration/tests/errors.t
+++ b/lib/configuration/tests/errors.t
@@ -334,3 +334,38 @@ A list cannot reference a list declared after it:
"content" decoder:
References in a list can only target a list declared before it, but got
{ "content": "second_list" }
+
+A field cannot be named "id" (reserved: it is the primary key of every row):
+
+ $ tests --configuration <<EOF
+ > {
+ > "content": [
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > {
+ > "name": "id",
+ > "label": {},
+ > "type": "text"
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > EOF
+ in field "content":
+ while decoding a list:
+ element 0:
+ in field "section":
+ while decoding a list:
+ element 0:
+ in field "field":
+ while decoding a list:
+ element 0:
+ in field "name":
+ the name "id" is reserved: it is the primary key of every row, but got
+ "id"
diff --git a/lib/model/model.ml b/lib/model/model.ml
index bd8c010..2e58d0c 100644
--- a/lib/model/model.ml
+++ b/lib/model/model.ml
@@ -22,5 +22,4 @@ type section = target Section.t
type field = target Field.t
type field_ex = target Field.field_ex
type reference = target Typ.reference
-type value = target Typ.value
type 'db typ = ('db, target) Typ.t
diff --git a/lib/model/model.mli b/lib/model/model.mli
index f51386c..6cf2b11 100644
--- a/lib/model/model.mli
+++ b/lib/model/model.mli
@@ -39,7 +39,8 @@
- {!Identifier} — the names of contents and fields, validated so that they
are always safe to embed as SQL identifiers.
- {!Content}, {!Section}, {!Field} — the structure of the model.
- - {!Typ} — the field types, and the values carried by fields.
+ - {!Typ} — the field types (the nature of the data; the data itself lives
+ in the value package as [Value.value]).
- {!Reference} — a link from a reference field to its target content.
- {!DataList} — a static list declared in the configuration: referenceable,
but not editable by the user.
@@ -97,9 +98,9 @@ module Typ = Typ
stored in a dedicated table ["content.field"].
A {!Typ.reference} record carries the target of a reference field (a
- {!target}) plus optional [constraints] restricting the allowed values. A
- {!Typ.value} is the data provided for a field when inserting/updating:
- {!Typ.None} (leave as is), {!Typ.V} (a single value) or {!Typ.Seq} (a
+ {!target}) plus optional [constraints] restricting the allowed values. The
+ data provided for a field when inserting/updating is a [Value.value] (from
+ the value package): [None] (leave as is), [V] (a single value) or [Seq] (a
sequence of references, for multivalued fields). *)
type 'db typ = ('db, target) Typ.t
@@ -136,9 +137,6 @@ module Section = Section
type section = target Section.t
(** A section of {!content} *)
-type value = target Typ.value
-(** A value provided for a field when inserting/updating data *)
-
module Field = Field
(** A field of a content.
diff --git a/lib/model/typ.ml b/lib/model/typ.ml
index 6168c4c..173f08f 100644
--- a/lib/model/typ.ml
+++ b/lib/model/typ.ml
@@ -44,25 +44,3 @@ let eq : type a b c. (a, c) t -> (b, c) t -> (a, b) Type.eq option =
| 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 *)
diff --git a/lib/query/dune b/lib/query/dune
index 307319a..990b572 100644
--- a/lib/query/dune
+++ b/lib/query/dune
@@ -9,4 +9,5 @@
uri
croquette_common
model
+ value
configuration))
diff --git a/lib/query/encoder.ml b/lib/query/encoder.ml
index ba5435e..64464fd 100644
--- a/lib/query/encoder.ml
+++ b/lib/query/encoder.ml
@@ -291,7 +291,7 @@ module DataToCaqtiValues = Map (struct
include DefaultEncoder
type 'a t = 'a
- type env = string -> Model.value
+ type env = string -> Value.value
let f : type a. env -> a Intf.data -> a t =
fun provider Intf.{ name; content_type } ->
@@ -299,13 +299,15 @@ module DataToCaqtiValues = Map (struct
(* Check the value given by the provider and ensure it’s match the
expected type *)
match result with
- | Model.Typ.Seq _ -> raise TypeMismatch
- | Model.Typ.None ->
- (* The provider does not have any value for this property, use the
- default value*)
- Model.Typ.default_value content_type
- | Model.Typ.V (field_type', value) -> (
- match Model.Typ.eq content_type field_type' with
- | Some Equal -> value
- | None -> raise TypeMismatch)
+ | Value.Data.Any d -> (
+ match d with
+ | Value.Data.Seq _ -> raise TypeMismatch
+ | Value.Data.None ->
+ (* The provider does not have any value for this property, use the
+ default value*)
+ Model.Typ.default_value content_type
+ | Value.Data.V (field_type', value) -> (
+ match Model.Typ.eq content_type field_type' with
+ | Some Equal -> value
+ | None -> raise TypeMismatch))
end)
diff --git a/lib/query/insert.ml b/lib/query/insert.ml
index 40887e8..7b7619e 100644
--- a/lib/query/insert.ml
+++ b/lib/query/insert.ml
@@ -25,7 +25,7 @@ module MultiReferencesInsert = struct
parent_id : int64;
table : Model.Identifier.t;
(** The table holding the row: a content table, or a list *)
- provider : string -> Model.value;
+ provider : string -> Value.value;
k : (unit, Caqti.Error.t) result Lwt.t;
}
@@ -66,10 +66,12 @@ module MultiReferencesInsert = struct
| Model.Typ.MultiReference _target -> (
let result = provider (Model.Identifier.to_string name) in
match result with
- | Model.Typ.None -> acc
- | Model.Typ.V _ -> raise Encoder.TypeMismatch
- | Model.Typ.Seq (field_type', values) -> (
- match Model.Typ.eq content_type field_type' with
+ | Value.Data.Any d -> (
+ match d with
+ | Value.Data.None -> acc
+ | Value.Data.V _ -> raise Encoder.TypeMismatch
+ | Value.Data.Seq (field_type', values) -> (
+ match Model.Typ.eq content_type field_type' with
| None -> raise Encoder.TypeMismatch
| Some Equal ->
let q =
@@ -93,7 +95,7 @@ module MultiReferencesInsert = struct
Lwt.return res
in
- { acc with k }))
+ { acc with k })))
| _ -> raise Encoder.TypeMismatch
end
@@ -107,7 +109,7 @@ let insert_row :
table:Model.Identifier.t ->
?init_fields:Model.field list ->
Model.field list ->
- (string -> Model.value) ->
+ (string -> Value.value) ->
int64 Lwt.t =
fun db ~table ?(init_fields = []) fields provider ->
let module Db = (val db) in
@@ -166,8 +168,8 @@ let insert_row :
let insert_returning :
Caqti_lwt.connection ->
Model.content ->
- (string -> Model.value) ->
- int64 Lwt.t =
+ (string -> Value.value) ->
+ Model.content Value.t Lwt.t =
fun db content provider ->
let init_fields =
match Model.Content.parent content with
@@ -176,17 +178,21 @@ let insert_returning :
Model.Field.
[ create (T Integer) [] ~required:true Model.Identifier.parent ]
in
- insert_row db ~table:(Model.Content.id content) ~init_fields
- (List.of_seq (Model.Content.fields content))
- provider
+ let ( let* ) = Lwt.bind in
+ let* id =
+ insert_row db ~table:(Model.Content.id content) ~init_fields
+ (List.of_seq (Model.Content.fields content))
+ provider
+ in
+ Lwt.return (Value.v content id)
(** Insert an entry for a specific content *)
let insert :
Caqti_lwt.connection ->
Model.content ->
- ?parent:int64 ->
- (string -> Model.value) ->
- int64 Lwt.t =
+ ?parent:Model.content Value.t ->
+ (string -> Value.value) ->
+ Model.content Value.t Lwt.t =
fun db content ?parent provider ->
let provider =
match (parent, Model.Content.parent content) with
@@ -197,8 +203,9 @@ let insert :
raise Encoder.TypeMismatch
| Some v, Some _ -> (
(* Otherwise update the provider to give a value for the parent *)
+ let parent_id = Value.id v in
function
- | "parent" -> Model.Typ.(V (Integer, Some v))
+ | "parent" -> Value.single (Model.Typ.Integer, Some parent_id)
| other -> provider other)
in
let module Db = (val db) in
@@ -207,20 +214,26 @@ let insert :
insert_returning db content provider
| _ -> raise Not_found
-(** Insert a row in the dedicated table of a list, and return its id.
+(** Insert a row in the dedicated table of a list, and return it.
Like [insert], the [provider] is called with the name of each field of the
list to get its value. Multivalued fields are inserted in their own
dedicated table, with the parent pointing to the list row. *)
let insert_list :
- Caqti_lwt.connection -> Model.list -> (string -> Model.value) -> int64 Lwt.t
- =
+ Caqti_lwt.connection ->
+ Model.list ->
+ (string -> Value.value) ->
+ Model.list Value.t Lwt.t =
fun db list provider ->
let module Db = (val db) in
match Db.dialect with
- | Caqti.Template.Dialect.(Sqlite _ | Pgsql _) ->
- insert_row db ~table:list.Model.DataList.id list.Model.DataList.fields
- provider
+ | Caqti.Template.Dialect.(Sqlite _ | Pgsql _) -> (
+ let ( let* ) = Lwt.bind in
+ let* id =
+ insert_row db ~table:list.Model.DataList.id list.Model.DataList.fields
+ provider
+ in
+ Lwt.return (Value.v list id))
| _ -> raise Not_found
let insert_references :
@@ -228,13 +241,13 @@ let insert_references :
Model.content ->
Int64.t ->
Model.Identifier.t ->
- Model.value ->
+ Value.value ->
Int64.t Intf.data ->
unit Lwt.t =
fun db content content_id field_name value data ->
- let provider : string -> Model.value = function
+ let provider : string -> Value.value = function
| n when String.equal n (Model.Identifier.to_string field_name) -> value
- | _ -> Model.Typ.None
+ | _ -> Value.none
in
let acc =
MultiReferencesInsert.
diff --git a/lib/query/insert.mli b/lib/query/insert.mli
index 32251ac..583c568 100644
--- a/lib/query/insert.mli
+++ b/lib/query/insert.mli
@@ -3,17 +3,18 @@
val insert :
Caqti_lwt.connection ->
Model.content ->
- ?parent:int64 ->
- (string -> Model.value) ->
- int64 Lwt.t
+ ?parent:Model.content Value.t ->
+ (string -> Value.value) ->
+ Model.content Value.t Lwt.t
(** See [Query.insert] *)
val insert_list :
Caqti_lwt.connection ->
Model.list ->
- (string -> Model.value) ->
- int64 Lwt.t
-(** Insert a row in the dedicated table of a static list, and return its id.
+ (string -> Value.value) ->
+ Model.list Value.t Lwt.t
+(** Insert a row in the dedicated table of a static list, and return it as a
+ value.
The [provider] is called with the name of each field of the list to get its
value, like [insert]. Multivalued fields are inserted in their dedicated
@@ -24,7 +25,7 @@ val insert_references :
Model.content ->
Int64.t ->
Model.Identifier.t ->
- Model.value ->
+ Value.value ->
Int64.t Intf.data ->
unit Lwt.t
(** Insert a list of references for a content. This function is used during the
diff --git a/lib/query/query.mli b/lib/query/query.mli
index d4badd5..cd7287f 100644
--- a/lib/query/query.mli
+++ b/lib/query/query.mli
@@ -1,16 +1,17 @@
val insert :
Caqti_lwt.connection ->
Model.content ->
- ?parent:int64 ->
- (string -> Model.value) ->
- int64 Lwt.t
-(** Insert a new element and return the ID.
+ ?parent:Model.content Value.t ->
+ (string -> Value.value) ->
+ Model.content Value.t Lwt.t
+(** Insert a new element and return it as a value.
- [insert db content ~parent provider] will create the content in the
- database.
+ [insert db content ~parent provider] will create the content in the database
+ and return the inserted row as a [Value.t] (the content together with its
+ new id).
- The parent is required if the content is a subcontent. Not providing the
- parameter will raise the error [TypeMismatch].
+ The parent — itself a value — is required if the content is a subcontent.
+ Not providing the parameter will raise the error [TypeMismatch].
For each field in the content, the function [provider] will be called with
the name for this property. The value returned shall match the expected type
@@ -22,13 +23,14 @@ val insert :
val insert_list :
Caqti_lwt.connection ->
Model.list ->
- (string -> Model.value) ->
- int64 Lwt.t
-(** Insert a row in the dedicated table of a static list, and return its id.
+ (string -> Value.value) ->
+ Model.list Value.t Lwt.t
+(** Insert a row in the dedicated table of a static list, and return it as a
+ value.
The [provider] is called with the name of each field of the list, like
- [insert]. This is the function used to populate the lists from their
- source. *)
+ [insert]. This is the function used to populate the lists from their source.
+*)
val create : Caqti_lwt.connection -> Configuration.Ast.setting -> unit Lwt.t
@@ -36,5 +38,5 @@ val update :
Caqti_lwt.connection ->
Int64.t ->
Model.content ->
- (string -> Model.value) ->
+ (string -> Value.value) ->
unit Lwt.t
diff --git a/lib/query/update.ml b/lib/query/update.ml
index 3c0941c..90db29d 100644
--- a/lib/query/update.ml
+++ b/lib/query/update.ml
@@ -1,6 +1,6 @@
(** Generate the update queries for a content *)
-type provider = string -> Model.value
+type provider = string -> Value.value
module UpdateTemplate = Encoder.Map (struct
type 'a t = string
@@ -22,7 +22,7 @@ end)
let update_fields :
Caqti_lwt.connection ->
Model.content ->
- (Intf.t_type * (string * Model.value)) list ->
+ (Intf.t_type * (string * Value.value)) list ->
int64 ->
unit Lwt.t =
fun db content fields id ->
@@ -141,7 +141,7 @@ let update_mulivaluated_field :
At this step, we can just use the code from the insert module. *)
let remaining = ReferencesSet.(diff values (of_list remaining')) in
- let v = Model.Typ.Seq (t, ReferencesSet.to_seq remaining) in
+ let v = Value.multi (t, ReferencesSet.to_seq remaining) in
Insert.insert_references db content id field_name v data_type)
Lwt.return_unit fields
@@ -149,7 +149,7 @@ let q :
Caqti_lwt.connection ->
Int64.t ->
Model.content ->
- (string -> Model.value) ->
+ (string -> Value.value) ->
unit Lwt.t =
fun db id content provider ->
let module Db = (val db) in
@@ -170,11 +170,16 @@ let q :
let tt = Intf.TT { name; content_type } in
let value = provider (Model.Identifier.to_string name) in
match value with
- | Model.Typ.None -> acc
- | Model.Typ.V _ as value ->
- ( (tt, (Model.Identifier.to_string name, value)) :: fields,
- references )
- | Model.Typ.Seq (t, seq) -> (fields, (name, t, seq) :: references))
+ | Value.Data.Any d -> (
+ match d with
+ | Value.Data.None -> acc
+ | Value.Data.V _ as dv ->
+ ( ( tt,
+ (Model.Identifier.to_string name, Value.Data.any dv) )
+ :: fields,
+ references )
+ | Value.Data.Seq (t, seq) ->
+ (fields, (name, t, seq) :: references)))
([], [])
(Model.Content.fields content)
in
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 *)
diff --git a/test/dune b/test/dune
index 9d4c6d3..3fb548c 100644
--- a/test/dune
+++ b/test/dune
@@ -14,4 +14,5 @@
configuration
model
html
- query))
+ query
+ value))
diff --git a/test/insert.ml b/test/insert.ml
index 217fc83..ae5748e 100644
--- a/test/insert.ml
+++ b/test/insert.ml
@@ -15,7 +15,7 @@ let pp : Format.formatter -> t -> unit =
Format.fprintf formatter "@[<v 2>%s:%Ld@;%s@]" content_id id content))
type content = Model.content
-type value = Model.value
+type value = Value.value
type properties = (string * value) list
(* A subcontent to insert, with one entry (properties) per row *)
@@ -31,17 +31,18 @@ type entry = properties * content_to_insert list
[content] is the type definition for the element we want to insert [entry]
contains all the values to insert in the database. *)
let insert_and_get_result :
- ?parent:int64 ->
+ ?parent:Model.content Value.t ->
Caqti_lwt.connection ->
content ->
properties ->
(string * int64 * string) Lwt.t =
fun ?parent db content entry ->
let ( let* ) = Lwt.bind in
- let* id =
+ let* inserted =
Query.insert ?parent db content (fun v ->
- match List.assoc_opt v entry with Some v -> v | None -> Model.Typ.None)
+ match List.assoc_opt v entry with Some v -> v | None -> Value.none)
in
+ let id = Value.id inserted in
let content_id = Model.Identifier.to_string (Model.Content.id content) in
let module Db = (val db) in
let* json_result = Db.find (Common.content_as_json content) id in
@@ -49,19 +50,19 @@ let insert_and_get_result :
Lwt.return (content_id, id, json)
(** Insert one row and collect the result line, together with the lines of its
- multivalued references. Return the accumulator and the id of the new row. *)
+ multivalued references. Return the accumulator and the inserted value. *)
let insert_one :
Caqti_lwt.connection ->
- ?parent:int64 ->
+ ?parent:Model.content Value.t ->
content ->
properties ->
(string * Int64.t * string) list ->
- ((string * Int64.t * string) list * Int64.t) Lwt.t =
+ ((string * Int64.t * string) list * Model.content Value.t) Lwt.t =
fun db ?parent content entry acc ->
let ( let* ) = Lwt.bind in
let* content_id, id, json = insert_and_get_result ?parent db content entry in
let* ext = Common.collect_references db content id in
- Lwt.return (List.append ext ((content_id, id, json) :: acc), id)
+ Lwt.return (List.append ext ((content_id, id, json) :: acc), Value.v content id)
module S = Configuration.Json5.Decode
@@ -96,14 +97,14 @@ let rec value :
( "numeric",
S.map
(fun v ->
- Lwt.return (key, Model.Typ.(V (Integer, Some (Int64.of_int v)))))
+ Lwt.return (key, Value.single (Model.Typ.Integer, Some (Int64.of_int v))))
S.int );
( "text",
- S.map (fun v -> Lwt.return (key, Model.Typ.(V (Text, Some v)))) S.string
+ S.map (fun v -> Lwt.return (key, Value.single (Model.Typ.Text, Some v))) S.string
);
("reference", reference_value db content key false);
("references", reference_value db content key true);
- ("other", S.succeed (Lwt.return (key, Model.Typ.None)));
+ ("other", S.succeed (Lwt.return (key, Value.none)));
]
(** Decode a reference field: single value or list of values, depending on
@@ -131,7 +132,7 @@ and reference_value :
(fun ids ->
Lwt.return
( key,
- Model.Typ.Seq
+ Value.multi
( Model.Typ.MultiReference reference,
List.to_seq (List.map ~f:Int64.of_int ids) ) ))
(S.list S.int)
@@ -140,7 +141,7 @@ and reference_value :
(fun v ->
Lwt.return
( key,
- Model.Typ.V
+ Value.single
(Model.Typ.MonoReference reference, Some (Int64.of_int v))
))
S.int
@@ -159,7 +160,7 @@ and reference_value :
in
Lwt.return
( key,
- Model.Typ.Seq
+ Value.multi
(Model.Typ.MultiReference reference, List.to_seq ids) ))
else
let** props = properties db target in
@@ -167,7 +168,7 @@ and reference_value :
(let* props = props in
let* _, id, _ = insert_and_get_result db target props in
Lwt.return
- (key, Model.Typ.V (Model.Typ.MonoReference reference, Some id)))
+ (key, Value.single (Model.Typ.MonoReference reference, Some id)))
)
and properties : Caqti_lwt.connection -> content -> properties Lwt.t S.decoder =
@@ -257,14 +258,14 @@ let exec : string list -> t =
Lwt_list.fold_left_s
(fun acc (entry, subcontents) ->
(* Insert the main content *)
- let* acc, id = insert_one db content entry acc in
+ let* acc, parent = insert_one db content entry acc in
(* Insert the subcontents associated *)
Lwt_list.fold_left_s
(fun acc (subcontent, subentries) ->
Lwt_list.fold_left_s
(fun acc entry ->
let* acc, _ =
- insert_one db ~parent:id subcontent entry acc
+ insert_one db ~parent subcontent entry acc
in
Lwt.return acc)
acc subentries)
diff --git a/test/insert_list.ml b/test/insert_list.ml
index 9d4018f..ffc4169 100644
--- a/test/insert_list.ml
+++ b/test/insert_list.ml
@@ -60,7 +60,7 @@ let external_table_as_json :
(** A provider giving a value for each field of the list, depending on its type.
The references point to the row of id 1 of their target list. *)
-let provider_for (list : Model.list) (row : Int64.t) : string -> Model.value =
+let provider_for (list : Model.list) (row : Int64.t) : string -> Value.value =
fun field_name ->
let field =
List.find list.Model.DataList.fields ~f:(fun field ->
@@ -70,14 +70,14 @@ let provider_for (list : Model.list) (row : Int64.t) : string -> Model.value =
let (Model.Field.T ty) = Model.Field.type_ field in
match ty with
| Model.Typ.Text ->
- Model.Typ.V (Text, Some (field_name ^ "-" ^ Int64.to_string row))
- | Model.Typ.Integer -> Model.Typ.V (Integer, Some row)
+ Value.single (Text, Some (field_name ^ "-" ^ Int64.to_string row))
+ | Model.Typ.Integer -> Value.single (Integer, Some row)
| Model.Typ.MonoReference reference ->
- Model.Typ.V (MonoReference reference, Some 1L)
+ Value.single (MonoReference reference, Some 1L)
| Model.Typ.MultiReference reference ->
(* Several values per row, to highlight the multivalued data *)
let values = if Int64.equal row 1L then [ 1L; 2L ] else [ 2L ] in
- Model.Typ.Seq (MultiReference reference, List.to_seq values)
+ Value.multi (MultiReference reference, List.to_seq values)
let insert_rows db (list : Model.list) =
let* _id1 = Query.insert_list db list (provider_for list 1L) in
diff --git a/test/update.ml b/test/update.ml
index 74208de..ec9bf91 100644
--- a/test/update.ml
+++ b/test/update.ml
@@ -141,7 +141,8 @@ let exec : string list -> t =
let* db = Common.connect configuration in
let module Db = (val db) in
(* Insert a content without any value *)
- let* id = Query.insert db content (fun _ -> Model.Typ.None) in
+ let* inserted = Query.insert db content (fun _ -> Value.none) in
+ let id = Value.id inserted in
let group_commit : actions Seq.t Seq.t =
List.rev !actions |> List.to_seq
@@ -168,26 +169,26 @@ let exec : string list -> t =
let (Model.Field.T t) = Model.Field.type_ field in
begin match t with
| Model.Typ.MultiReference _ as e ->
- Some Model.Typ.(Seq (e, Seq.empty))
- | Integer as e -> Some Model.Typ.(V (e, None))
- | Text as e -> Some Model.Typ.(V (e, None))
+ Some (Value.multi (e, Seq.empty))
+ | Integer as e -> Some (Value.single (e, None))
+ | Text as e -> Some (Value.single (e, None))
| MonoReference _ as e ->
- Some Model.Typ.(V (e, None))
+ Some (Value.single (e, None))
end
| Text (k, v) when String.equal k key ->
- Some Model.Typ.(V (Text, Some v))
+ Some (Value.single (Model.Typ.Text, Some v))
| Integer (k, v) when String.equal k key ->
- Some Model.Typ.(V (Integer, Some (Int64.of_int v)))
+ Some (Value.single (Model.Typ.Integer, Some (Int64.of_int v)))
| Reference (target, k, v) when String.equal k key ->
Some
- Model.Typ.(
- V (MonoReference target, Some (Int64.of_int v)))
+ (Value.single
+ (Model.Typ.MonoReference target, Some (Int64.of_int v)))
| References (target, k, v) when String.equal k key ->
- Some Model.Typ.(Seq (MultiReference target, v))
+ Some (Value.multi (Model.Typ.MultiReference target, v))
| _ -> None)
actions
in
- match value with Some v -> v | None -> Model.Typ.None)
+ match value with Some v -> v | None -> Value.none)
in
(* Get the values from the content registered in the database *)
let* json_result = Db.find (Common.content_as_json content) id in