From fa4970bf1b74f3fa0b2f63cbd461d98c251dc0f0 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Fri, 20 Nov 2026 18:47:26 +0100 Subject: Updated the model for representing ocaml types in the application type --- lib/value/data.ml | 22 +++++++++------------- lib/value/data.mli | 42 +++++++++++++++++++----------------------- lib/value/value.ml | 38 ++++++++++++++++++++------------------ lib/value/value.mli | 37 +++++++++++++++++++++++++------------ 4 files changed, 73 insertions(+), 66 deletions(-) (limited to 'lib/value') diff --git a/lib/value/data.ml b/lib/value/data.ml index 7cbc00f..f0e321e 100644 --- a/lib/value/data.ml +++ b/lib/value/data.ml @@ -7,22 +7,18 @@ 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 + 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 = +type ('ocaml, '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 + | V : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, 't) data -let none : ('a, 't) data = None +let none : ('ocaml, 't) data = None -let v : ('a, 't) Model.Typ.t * 'a -> ('a, 't) data = - fun (typ, value) -> V (typ, value) +let v : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, '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 : ('ocaml, 't) data -> 't any [@@unboxed] -type 't any = Any : ('a, 't) data -> 't any [@@unboxed] - -let any : ('a, 't) data -> 't any = fun data -> Any data +let any : ('ocaml, 't) data -> 't any = fun data -> Any data diff --git a/lib/value/data.mli b/lib/value/data.mli index 0cb4856..7b7a5cc 100644 --- a/lib/value/data.mli +++ b/lib/value/data.mli @@ -1,41 +1,37 @@ (** 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 + [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 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. *) + 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 {!Model.Typ.t} stays in the + model package; only the data itself lives here. *) -type ('a, 't) data = private +type ('ocaml, '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]. *) + | V : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, 't) data -val none : ('a, 't) data +val none : ('ocaml, '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 *) +val v : ('db, 'ocaml, 't) Model.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 = private Any : ('a, 't) data -> 't any [@@unboxed] +type 't any = Any : ('ocaml, 't) data -> 't any [@@unboxed] -val any : ('a, 't) data -> 't any +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: diff --git a/lib/value/value.ml b/lib/value/value.ml index 38c2240..3012667 100644 --- a/lib/value/value.ml +++ b/lib/value/value.ml @@ -1,34 +1,36 @@ -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] *) + the data held, built with the smart constructors [Data.none], [Data.v] *) +type value = Model.target Data.any (** 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) +let single : ('db, 'ocaml, Model.target) Model.Typ.t * 'ocaml -> value = + fun data -> Data.any (Data.v data) +type 'a t = { + content : 'a; (** The content or list whose table holds the row *) + id : int64; (** The primary key of the row *) +} (** 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 + 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 + +let id_ref : (Model.target Model.Typ.monoreference) Model.Typ.named = + Model.Typ.named ~name:Model.Identifier.id ~typ:Model.Typ.Integer + +let get : type ocaml. + 'a t -> (('db, ocaml, Model.target) Model.Typ.t) Model.Typ.named -> ocaml = + fun t ref -> + match Model.Typ.provably_equal id_ref ref with + | Some Type.Equal -> Some t.id + | None -> raise Not_found diff --git a/lib/value/value.mli b/lib/value/value.mli index 42482f7..5c493aa 100644 --- a/lib/value/value.mli +++ b/lib/value/value.mli @@ -2,14 +2,11 @@ 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 *) + the data held, built with the smart constructors ([Data.none], [Data.v]); + 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 @@ -18,12 +15,10 @@ type value = Model.target Data.any 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 *) +val single : ('db, 'ocaml, Model.target) Model.Typ.t * 'ocaml -> value +(** [single (typ, data)]: the value for the field, in its code representation + ['ocaml] — a single value, or an [int64 Seq.t] for a multivalued reference — + together with the type [typ] of the field *) type 'a t (** A value: a row of the table of the content or list ['a], identified by its @@ -43,4 +38,22 @@ val content : 'a t -> 'a (** The content or list the value belongs to *) val id : 'a t -> int64 +[@@deprecated + "Use [Value.get v Value.id_ref] to read the primary key as typed data"] (** The primary key of the value in its table *) + +val id_ref : Model.target Model.Typ.monoreference Model.Typ.named +(** The pre-built {!Model.Typ.named} handle of the reserved field ["id"] + ({!Model.Identifier.id}) — an integer field present in every row, content or + list. This is the key to read the primary key of any value. *) + +val get : + 'a t -> ('db, 'ocaml, Model.target) Model.Typ.t Model.Typ.named -> 'ocaml +(** Read a field of the value and return its data directly in its code + representation ['ocaml] — e.g. [get v id_ref : int64 option] is the primary + key of [v], and a multivalued reference field would yield an [int64 Seq.t]. + + The match between the requested named type and the readable field is proved + by {!Model.Typ.provably_equal} on name and type. Only the reserved field + [id_ref] is readable for now; the other fields are read from the database + (not implemented yet) and raise [Not_found]. *) -- cgit v1.2.3