summaryrefslogtreecommitdiff
path: root/lib/model
diff options
context:
space:
mode:
Diffstat (limited to 'lib/model')
-rw-r--r--lib/model/model.ml2
-rw-r--r--lib/model/model.mli6
-rw-r--r--lib/model/typ.ml76
-rw-r--r--lib/model/typ.mli92
4 files changed, 162 insertions, 14 deletions
diff --git a/lib/model/model.ml b/lib/model/model.ml
index 2e58d0c..0f016ad 100644
--- a/lib/model/model.ml
+++ b/lib/model/model.ml
@@ -22,4 +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 'db typ = ('db, target) Typ.t
+type ('db, 'ocaml) typ = ('db, 'ocaml, target) Typ.t
diff --git a/lib/model/model.mli b/lib/model/model.mli
index 6cf2b11..a8941d9 100644
--- a/lib/model/model.mli
+++ b/lib/model/model.mli
@@ -39,8 +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 (the nature of the data; the data itself lives
- in the value package as [Value.value]).
+ - {!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.
@@ -103,7 +103,7 @@ module Typ = Typ
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
+type ('db, 'ocaml) typ = ('db, 'ocaml, target) Typ.t
(** A field type descriptor ; ['db] is the database representation of the field
(e.g. [string option] for a text field, [int64] for a multivalued reference)
*)
diff --git a/lib/model/typ.ml b/lib/model/typ.ml
index 173f08f..a13653e 100644
--- a/lib/model/typ.ml
+++ b/lib/model/typ.ml
@@ -4,7 +4,7 @@ type 'a reference = {
{!Reference.target} (aliased as {!Model.target}). *)
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
+ 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. *)
}
@@ -17,25 +17,51 @@ type 'a reference = {
(** 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
+ - ['db] is the representation of the data in the database (the column type).
+ - ['ocaml] is the representation of the data in the code — usually the same
+ as ['db], except for multivalued references, stored as one row per value
+ in a dedicated table (['db = int64]) but manipulated as a sequence in code
+ (['ocaml = int64 Seq.t]).
+ - ['t] is the type of the target in references fields *)
+type ('db, 'ocaml, 't) t =
+ | Text : (string option, string option, 't) t
+ | Integer : (int64 option, int64 option, 't) t
+ | MonoReference : 't reference -> (int64 option, int64 option, 't) t
+ | MultiReference : 't reference -> (int64, int64 Seq.t, 't) t
(** Multireferences properties allows only one kind of content.
They are not encoded in the content table but in another table *)
+type 'target monoreference = (int64 option, int64 option, 'target) t
+(** The type of a reference field: the [int64] id of the target row in the
+ database, an [int64 option] in code. In practice ['target] is always
+ {!Model.target}. *)
+
+type 'target multireference = (int64, int64 Seq.t, 'target) t
+(** The type of a multivalued reference field: one [int64] per row in the
+ dedicated table (['db]), an [int64 Seq.t] in code (['ocaml]). In practice
+ ['target] is always {!Model.target}. *)
+
(** Get the value to insert in the database when the field is empty *)
-let default_value : type a. (a, _) t -> a = function
+let default_value : type a b c. (a, b, c) 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 =
+(** Constructor-level equality on the ['db] representation *)
+let eq : type a b x y c. (a, x, c) t -> (b, y, 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
+
+(** Constructor-level equality on the ['ocaml] representation *)
+let eq_ocaml : type a b x y c.
+ (a, x, c) t -> (b, y, c) t -> (x, y) Type.eq option =
fun t1 t2 ->
match (t1, t2) with
| MultiReference _, MultiReference _ -> Some Type.Equal
@@ -44,3 +70,33 @@ let eq : type a b c. (a, c) t -> (b, c) t -> (a, b) Type.eq option =
| Text, Text -> Some Type.Equal
| _ -> None
+type 'typ named = {
+ name : Identifier.t; (** The internal name of the field *)
+ typ : 'typ; (** The type of the field, e.g. a ['target multireference] *)
+}
+(** A named field type: a {!t} together with the field's name.
+
+ Named types are built on the fly with {!named} (e.g. from a field, see
+ {!Field.named_of}) and used locally as typed handles — e.g. to read the data
+ of a field from a value. A pre-built instance exists for the reserved field
+ [["id"]] (see [Value.id_ref]). *)
+
+(** Build a named type from a name and a type — no uniqueness involved, so
+ values built on the fly denote the same field *)
+let named : name:Identifier.t -> typ:'typ -> 'typ named =
+ fun ~name ~typ -> { name; typ }
+
+let name : 'typ named -> Identifier.t = fun n -> n.name
+let typ : 'typ named -> 'typ = fun n -> n.typ
+
+(** [provably_equal n1 n2] is [Some Equal] iff [n1] and [n2] denote the same
+ field — same name — and their types match, which also proves that their
+ ['ocaml] representations are equal *)
+let provably_equal (type a b x y c) (n1 : (a, x, c) t named)
+ (n2 : (b, y, c) t named) : (x, y) Type.eq option =
+ if Identifier.equal n1.name n2.name then eq_ocaml n1.typ n2.typ else None
+
+(** [same n1 n2] is [true] iff [n1] and [n2] denote the same field with matching
+ types *)
+let same : ('a, 'x, 'c) t named -> ('b, 'y, 'c) t named -> bool =
+ fun n1 n2 -> provably_equal n1 n2 <> None
diff --git a/lib/model/typ.mli b/lib/model/typ.mli
new file mode 100644
index 0000000..42350a3
--- /dev/null
+++ b/lib/model/typ.mli
@@ -0,0 +1,92 @@
+(** The field types of the application model.
+
+ A field type describes the nature of the data a field can carry, at two
+ levels:
+
+ - ['db] is the representation in the database (the column type);
+ - ['ocaml] is the representation in the code — usually equal to ['db],
+ except for multivalued references, stored as one row per value in a
+ dedicated table (['db = int64]) but manipulated as a sequence in code
+ (['ocaml = int64 Seq.t]).
+
+ The third parameter ['t] is the target of the field when it is a
+ reference (a content or a static list, i.e. {!Model.target}). *)
+
+type 'a reference = {
+ t : 'a;
+ (** The target of the reference: at the resolved level a
+ {!Reference.target} (aliased as {!Model.target}). *)
+ 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 target (a content or a static list).
+
+ 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.target}. *)
+
+type ('db, 'ocaml, 't) t =
+ | Text : (string option, string option, 't) t
+ | Integer : (int64 option, int64 option, 't) t
+ | MonoReference : 't reference -> (int64 option, int64 option, 't) t
+ | MultiReference : 't reference -> (int64, int64 Seq.t, 't) t
+ (** Multireference fields allow only one kind of target.
+
+ They are not encoded in the content table but in another table *)
+
+type 'target monoreference = (int64 option, int64 option, 'target) t
+(** The type of a reference field: the [int64] id of the target row in the
+ database, an [int64 option] in code. In practice ['target] is always
+ {!Model.target}. *)
+
+type 'target multireference = (int64, int64 Seq.t, 'target) t
+(** The type of a multivalued reference field: one [int64] per row in the
+ dedicated table (['db]), an [int64 Seq.t] in code (['ocaml]). In practice
+ ['target] is always {!Model.target}. *)
+
+val default_value : ('a, 'b, 'c) t -> 'a
+(** The value to insert in the database when the field is empty *)
+
+val eq : ('a, 'x, 'c) t -> ('b, 'y, 'c) t -> ('a, 'b) Type.eq option
+(** Constructor-level equality on the ['db] representation: [eq t1 t2] is
+ [Some Equal] when [t1] and [t2] have the same constructor, which also proves
+ ['a = 'b] *)
+
+val eq_ocaml : ('a, 'x, 'c) t -> ('b, 'y, 'c) t -> ('x, 'y) Type.eq option
+(** Constructor-level equality on the ['ocaml] representation: [eq_ocaml t1 t2]
+ is [Some Equal] when [t1] and [t2] have the same constructor, which also
+ proves ['x = 'y] *)
+
+type 'typ named = {
+ name : Identifier.t; (** The internal name of the field *)
+ typ : 'typ; (** The type of the field, e.g. a ['target multireference] *)
+}
+(** A named field type: a {!t} together with the field's name.
+
+ Named types are built on the fly with {!named} (e.g. from a field, see
+ {!Field.named_of}) and used locally as typed handles — e.g. to read the
+ data of a field from a value. A pre-built instance exists for the reserved
+ field ["id"] (see [Value.id_ref]). *)
+
+val named : name:Identifier.t -> typ:'typ -> 'typ named
+(** Build a named type from a name and a type — no uniqueness involved, so
+ values built on the fly denote the same field *)
+
+val name : 'typ named -> Identifier.t
+(** The name of the field *)
+
+val typ : 'typ named -> 'typ
+(** The type of the named field *)
+
+val provably_equal :
+ ('a, 'x, 'c) t named -> ('b, 'y, 'c) t named -> ('x, 'y) Type.eq option
+(** [provably_equal n1 n2] is [Some Equal] iff [n1] and [n2] denote the same
+ field — same name — and their types match, which also proves that their
+ ['ocaml] representations are equal *)
+
+val same : ('a, 'x, 'c) t named -> ('b, 'y, 'c) t named -> bool
+(** [same n1 n2] is [true] iff [n1] and [n2] denote the same field with matching
+ types *)