summaryrefslogtreecommitdiff
path: root/lib/model/typ.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/model/typ.ml')
-rw-r--r--lib/model/typ.ml37
1 files changed, 11 insertions, 26 deletions
diff --git a/lib/model/typ.ml b/lib/model/typ.ml
index c4acadc..0e94378 100644
--- a/lib/model/typ.ml
+++ b/lib/model/typ.ml
@@ -1,7 +1,7 @@
type 'a reference = {
t : 'a;
- (** The target of the reference: a content name while the configuration is
- being parsed, a [Model.Reference.t] once it is resolved. *)
+ (** The target of the reference: a [Model.Reference.t], a lazy pointer to
+ the content. *)
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
@@ -10,23 +10,15 @@ type 'a reference = {
}
(** A reference from a field to a content.
- The type is parameterized over the representation of the target ['a],
- because the model is built in two phases:
+ The target [t] is a {!Model.Reference.t}: a lazy pointer to the content,
+ created while the configuration is parsed (see [Configuration.Read_conf])
+ and resolved once all the contents are registered. The laziness is what
+ allows contents to reference each other cyclically.
- - while the configuration is being parsed ([Configuration.Read_conf]), the
- targeted content is not known yet: ['a] is the target as written in the
- configuration file (the identifier of the content, paired with its raw
- JSON value for error reporting);
- - once the configuration is loaded and checked
- ([Configuration.Ast.register_content]), [Field.map] rewrites every
- reference so that ['a = Model.Reference.t], a lazy pointer to the actual
- content (see {!Model.Reference}). The laziness is what allows contents to
- reference each other cyclically.
-
- Everywhere else in the application (query generation in [Query], HTML
- rendering in [Html.Builder]), a field has type
- [Model.Reference.t Model.Typ.reference], and the target content is obtained
- with [Model.Reference.get]. *)
+ 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.Reference.t]. Use the aliases
+ [Model.reference], [Model.content], [Model.field], ... at the use sites. *)
(** Each of the field types available in the application.
@@ -41,13 +33,6 @@ type ('db, 't) t =
They are not encoded in the content table but in another table *)
-let map : type a. ('ref1 -> 'ref2) -> (a, 'ref1) t -> (a, 'ref2) t =
- fun f -> function
- | Text -> Text
- | Integer -> Integer
- | MonoReference v -> MonoReference { v with t = f v.t }
- | MultiReference v -> MultiReference { v with t = f v.t }
-
(** Get the value to insert in the database when the field is empty *)
let default_value : type a. (a, _) t -> a = function
| Integer -> None
@@ -70,7 +55,7 @@ Example for inserting data
[
(* Function providing a value for each field to insert in the database *)
- let f : string -> Model.Reference.t Model.Typ.value = function
+ let f : string -> Model.value = function
| "siret" -> V (Model.Typ.Text, Some "siret")
| "name" -> V (Model.Typ.Text, Some "name")
| _ -> None