summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/configuration/ast.ml85
-rw-r--r--lib/configuration/ast.mli13
-rw-r--r--lib/configuration/graph_tree.ml23
-rw-r--r--lib/configuration/graph_tree.mli15
-rw-r--r--lib/configuration/read_conf.ml116
-rw-r--r--lib/model/field.ml6
-rw-r--r--lib/model/field.mli5
-rw-r--r--lib/model/model.ml20
-rw-r--r--lib/model/model.mli145
-rw-r--r--lib/model/tests/identifier.t2
-rw-r--r--lib/model/typ.ml37
-rw-r--r--lib/query/create.ml12
-rw-r--r--lib/query/encoder.ml2
-rw-r--r--lib/query/insert.ml24
-rw-r--r--lib/query/insert.mli8
-rw-r--r--lib/query/intf.ml5
-rw-r--r--lib/query/query.mli8
-rw-r--r--lib/query/update.ml12
-rw-r--r--test/common.ml6
-rw-r--r--test/insert.ml4
-rw-r--r--test/update.ml4
21 files changed, 310 insertions, 242 deletions
diff --git a/lib/configuration/ast.ml b/lib/configuration/ast.ml
index d874428..607031e 100644
--- a/lib/configuration/ast.ml
+++ b/lib/configuration/ast.ml
@@ -1,81 +1,31 @@
open StdLabels
-
module Splay = Splay.Make (Model.Identifier)
let escaped = String.escaped
type list' = { id : string; source : string }
-
-type setting = {
- mutable contents : Model.Reference.t Model.Content.t Splay.t;
- lists : list' list;
-}
+type setting = { mutable contents : Model.content Splay.t; lists : list' list }
let init : unit -> setting = fun () -> { contents = Splay.empty (); lists = [] }
(** Get the content from the setting. *)
-let get_content :
- setting -> Model.Identifier.t -> Model.Reference.t Model.Content.t option =
+let get_content : setting -> Model.Identifier.t -> Model.content option =
fun settings name -> Splay.find name settings.contents
+(** Check whether a list with the given name is declared in the setting *)
+let has_list : setting -> Model.Identifier.t -> bool =
+ fun setting name ->
+ List.exists setting.lists ~f:(fun { id; _ } ->
+ String.equal id (Model.Identifier.to_string name))
+
exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
exception NotImplemented : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
-(** Convert the content having fields referencing element by name into content
- having fields referencing other contents *)
-let rec convert :
- 'a Type.Id.t ->
- setting ->
- ('a * Model.Identifier.t) Model.Content.t ->
- Model.Reference.t Model.Content.t =
- fun ty setting content ->
- let subcontents =
- List.map (Model.Content.subcontents content) ~f:(convert ty setting)
- in
- let sections =
- List.map
- ~f:(fun section ->
- let section' : Model.Reference.t Model.Section.t =
- {
- section with
- fields =
- List.map section.Model.Section.fields ~f:(fun field ->
- Model.Field.map
- (fun (value, name) ->
- (* Raise a local exception here, which will be checked
- later once the configuration will be completely
- loaded *)
- Model.Reference.v (fun () ->
- match get_content setting name with
- | Some v -> v
- | None ->
- (* Also check from the list catalog *)
- begin match
- List.find_opt setting.lists ~f:(fun { id; _ } ->
- String.equal id
- (Model.Identifier.to_string name))
- with
- | Some v ->
- ignore v;
- raise_notrace
- (NotImplemented (ty, value, name))
- | None ->
- raise_notrace (BrokenLink (ty, value, name))
- end))
- field);
- }
- in
- section')
- (Model.Content.sections content)
- in
- Model.Content.v (Model.Content.id content) sections subcontents
-
(** Register a content and all the associated subcontent in the top level,
- giving the possibility to fetch any content or subcontent by is’s name
+ giving the possibility to fetch any content or subcontent by its name
without traversing the whole tree each time. *)
-let register_content :
- 'a Type.Id.t -> setting -> ('a * Model.Identifier.t) Model.Content.t -> unit =
- fun ty setting content ->
+let register_content : setting -> Model.content -> unit =
+ fun setting content ->
let rec traverse content =
let () =
setting.contents <-
@@ -83,7 +33,7 @@ let register_content :
in
List.iter ~f:traverse (Model.Content.subcontents content)
in
- traverse (convert ty setting content)
+ traverse content
let register_list : setting -> list' -> setting =
fun t l -> { t with lists = l :: t.lists }
@@ -92,9 +42,7 @@ let register_list : setting -> list' -> setting =
content in the application.
Return the content targeted *)
-let get_reference_target :
- Model.Reference.t Model.Field.t -> Model.Reference.t Model.Content.t option
- =
+let get_reference_target : Model.field -> Model.content option =
fun field ->
let (T ty) = Model.Field.type_ field in
match ty with
@@ -106,10 +54,7 @@ let get_reference_target :
(** Extract the other contents pointed by a given content, and the field used
for it. *)
-let targeted_contents :
- Model.Reference.t Model.Content.t ->
- (Model.Reference.t Model.Field.t * Model.Reference.t Model.Content.t) Seq.t
- =
+let targeted_contents : Model.content -> (Model.field * Model.content) Seq.t =
fun t ->
Seq.filter_map
(fun field ->
@@ -145,7 +90,7 @@ let tree : ?required:bool -> setting -> Graph_tree.t =
(** Return all the contents declared in the settings.
The list provide the root contents in the topological order *)
-let root_contents : setting -> Model.Reference.t Model.Content.t list =
+let root_contents : setting -> Model.content list =
fun setting -> Graph_tree.fold (fun a acc -> a :: acc) (tree setting) []
let check : setting -> (unit, exn) result =
diff --git a/lib/configuration/ast.mli b/lib/configuration/ast.mli
index 33d9bc8..298e2d5 100644
--- a/lib/configuration/ast.mli
+++ b/lib/configuration/ast.mli
@@ -3,19 +3,20 @@ type list' = { id : string; source : string }
val init : unit -> setting
-val get_content :
- setting -> Model.Identifier.t -> Model.Reference.t Model.Content.t option
+val get_content : setting -> Model.Identifier.t -> Model.content option
(** Get the content from the setting. *)
-val root_contents : setting -> Model.Reference.t Model.Content.t list
+val has_list : setting -> Model.Identifier.t -> bool
+(** Check whether a list with the given name is declared in the setting *)
+
+val root_contents : setting -> Model.content list
(** Extract all the content in the topological order.
Only the root content are repored in the list *)
-val register_content :
- 'a Type.Id.t -> setting -> ('a * Model.Identifier.t) Model.Content.t -> unit
+val register_content : setting -> Model.content -> unit
(** Register a content and all the associated subcontent in the top level,
- giving the possibility to fetch any content or subcontent by is’s name
+ giving the possibility to fetch any content or subcontent by its name
without traversing the whole tree each time. *)
exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
diff --git a/lib/configuration/graph_tree.ml b/lib/configuration/graph_tree.ml
index 240c1c9..64cef40 100644
--- a/lib/configuration/graph_tree.ml
+++ b/lib/configuration/graph_tree.ml
@@ -7,7 +7,7 @@
cycle analysis. *)
module Vertex = struct
- type t = Model.Reference.t Model.Content.t
+ type t = Model.content
let compare t1 t2 =
Model.Identifier.compare (Model.Content.id t1) (Model.Content.id t2)
@@ -19,7 +19,7 @@ module Vertex = struct
end
module Edge = struct
- type t = Default | Field of Model.Reference.t Model.Field.t
+ type t = Default | Field of Model.field
let compare e1 e2 =
match (e1, e2) with
@@ -52,24 +52,15 @@ type t = G.t
let create : unit -> t = G.create
-let add_edge :
- t ->
- Model.Reference.t Model.Content.t ->
- Model.Reference.t Model.Field.t ->
- Model.Reference.t Model.Content.t ->
- unit =
+let add_edge : t -> Model.content -> Model.field -> Model.content -> unit =
fun t src field dst -> G.add_edge_e t (src, Edge.Field field, dst)
-let add_content : t -> Model.Reference.t Model.Content.t -> unit = G.add_vertex
+let add_content : t -> Model.content -> unit = G.add_vertex
let pp : Format.formatter -> t -> unit = GV_Printer.fprint_graph
+let fold : (Model.content -> 'a -> 'a) -> t -> 'a -> 'a = Topological.fold
+let iter : (Model.content -> unit) -> t -> unit = Topological.iter
-let fold : (Model.Reference.t Model.Content.t -> 'a -> 'a) -> t -> 'a -> 'a =
- Topological.fold
-
-let iter : (Model.Reference.t Model.Content.t -> unit) -> t -> unit =
- Topological.iter
-
-exception Cycle of Model.Reference.t Model.Content.t list
+exception Cycle of Model.content list
let check_cycle : t -> unit =
fun t -> Cycle.iter_cycles (fun vertexes -> raise_notrace (Cycle vertexes)) t
diff --git a/lib/configuration/graph_tree.mli b/lib/configuration/graph_tree.mli
index 31a8f79..381d904 100644
--- a/lib/configuration/graph_tree.mli
+++ b/lib/configuration/graph_tree.mli
@@ -2,24 +2,19 @@ type t
val create : unit -> t
-val add_content : t -> Model.Reference.t Model.Content.t -> unit
+val add_content : t -> Model.content -> unit
(** Add a new content in the tree *)
-val add_edge :
- t ->
- Model.Reference.t Model.Content.t ->
- Model.Reference.t Model.Field.t ->
- Model.Reference.t Model.Content.t ->
- unit
+val add_edge : t -> Model.content -> Model.field -> Model.content -> unit
(** Add a relation between two contents.
The function does not check if the two contents are actually linked together
*)
val pp : Format.formatter -> t -> unit
-val fold : (Model.Reference.t Model.Content.t -> 'a -> 'a) -> t -> 'a -> 'a
-val iter : (Model.Reference.t Model.Content.t -> unit) -> t -> unit
+val fold : (Model.content -> 'a -> 'a) -> t -> 'a -> 'a
+val iter : (Model.content -> unit) -> t -> unit
-exception Cycle of Model.Reference.t Model.Content.t list
+exception Cycle of Model.content list
val check_cycle : t -> unit
diff --git a/lib/configuration/read_conf.ml b/lib/configuration/read_conf.ml
index 4150295..bcd8f8a 100644
--- a/lib/configuration/read_conf.ml
+++ b/lib/configuration/read_conf.ml
@@ -4,20 +4,6 @@ module Make (S : Decoders.Decode.S) = struct
let ( let* ) = S.( let* )
let ( and* ) = S.( and* )
- (** Recursive type declaring a content and the subcontents *)
- type declared_content =
- | C of
- Model.Identifier.t
- * (S.value * Model.Identifier.t) Model.Section.t list
- * declared_content list
-
- let rec content_builder :
- declared_content -> (S.value * Model.Identifier.t) Model.Content.t =
- fun c ->
- let (C (content_id, sections, declared_subcontents)) = c in
- let children = List.map declared_subcontents ~f:content_builder in
- Model.Content.v content_id sections children
-
(** [keep decoder] transform a decoder and keep the initial value with the
decoded value.
@@ -40,10 +26,30 @@ module Make (S : Decoders.Decode.S) = struct
else
match Model.Identifier.of_string_opt id with
| Some id -> S.succeed id
- | None -> S.fail "Expected an identifier with only the accepted characters"
-
- let field_type : (S.value * Model.Identifier.t) Model.Field.field_ex S.decoder =
- let text_type : (S.value * Model.Identifier.t) Model.Field.field_ex S.decoder =
+ | None ->
+ S.fail "Expected an identifier with only the accepted characters"
+
+ type env = { ty : S.value Type.Id.t; settings : Ast.setting }
+ (** The decoders below need the setting being built (to resolve the reference
+ targets) and a type id (to attribute broken links to this parsing pass in
+ the error messages). *)
+
+ (** Build the reference target for a field: a lazy thunk closing over the
+ setting. The thunk is only forced by [Ast.check], once every content has
+ been registered — this is what allows forward and cyclic references
+ between contents. The raw value is kept to report broken links. *)
+ let link (env : env) (value : S.value) (name : Model.Identifier.t) :
+ Model.Reference.t =
+ Model.Reference.v (fun () ->
+ match Ast.get_content env.settings name with
+ | Some content -> content
+ | None ->
+ if Ast.has_list env.settings name then
+ raise_notrace (Ast.NotImplemented (env.ty, value, name))
+ else raise_notrace (Ast.BrokenLink (env.ty, value, name)))
+
+ let field_type (env : env) : Model.field_ex S.decoder =
+ let text_type : Model.field_ex S.decoder =
S.and_then
(fun value ->
match String.lowercase_ascii value with
@@ -51,32 +57,27 @@ module Make (S : Decoders.Decode.S) = struct
| "integer" -> S.succeed (Model.Field.T Integer)
| other -> S.fail other)
S.string
- and content : (S.value * Model.Identifier.t) Model.Field.field_ex S.decoder =
+ and content_ref : Model.field_ex S.decoder =
let* content_t = keep @@ S.field "content" identifier
and* multivaluated = S.field_opt_or ~default:false "multivaluated" S.bool
and* constraints = S.field_opt "constraints" dsl in
-
- let reference = content_t in
-
+ let value, name = content_t in
+ let t = link env value name in
match multivaluated with
- | true ->
- S.succeed
- (Model.Field.T (MultiReference { t = reference; constraints }))
- | false ->
- S.succeed
- (Model.Field.T (MonoReference { t = reference; constraints }))
+ | true -> S.succeed (Model.Field.T (MultiReference { t; constraints }))
+ | false -> S.succeed (Model.Field.T (MonoReference { t; constraints }))
in
- S.one_of [ ("text", text_type); ("content", content) ]
+ S.one_of [ ("text", text_type); ("content", content_ref) ]
let validation : Model.Field.validation S.decoder =
let* rule = S.field "rule" dsl and* message = S.field "message" textI18n in
S.succeed Model.Field.{ rule; message }
- let field : (S.value * Model.Identifier.t) Model.Field.t S.decoder =
+ let field (env : env) : Model.field S.decoder =
let* name = S.field "name" identifier
and* label = S.field "label" textI18n
- and* type' = S.field "type" field_type
+ and* type' = S.field "type" (field_type env)
and* required = S.field_opt_or ~default:false "required" S.bool
and* placeholder = S.field_opt "placeholder" textI18n
and* validation =
@@ -87,43 +88,40 @@ module Make (S : Decoders.Decode.S) = struct
(Model.Field.create type' label ~required ?placeholder ~validation
?criteria ?icon name)
- let section : (S.value * Model.Identifier.t) Model.Section.t S.decoder =
+ let section (env : env) : Model.section S.decoder =
let* label = S.field_opt_or ~default:[] "label" textI18n
and* criteria = S.field_opt "criteria" dsl
- and* fields = S.field "field" (S.list field) in
+ and* fields = S.field "field" (S.list (field env)) in
let icon = None in
S.succeed Model.Section.{ label; criteria; fields; icon }
- let content : (S.value * Model.Identifier.t) Model.Content.t S.decoder =
- (* Build a temporary type without the parent information *)
- let* c =
- S.fix (fun content_decoder ->
- let* id = S.field "id" identifier
- and* sections = S.field_opt_or ~default:[] "section" (S.list section)
- and* subcontents =
- S.field_opt_or ~default:[] "subcontent" (S.list content_decoder)
- in
- let content = C (id, sections, subcontents) in
- S.succeed content)
- in
- S.succeed (content_builder c)
+ let content (env : env) : Model.content S.decoder =
+ S.fix (fun self ->
+ let* id = S.field "id" identifier
+ and* sections =
+ S.field_opt_or ~default:[] "section" (S.list (section env))
+ and* subcontents =
+ S.field_opt_or ~default:[] "subcontent" (S.list self)
+ in
+ S.succeed (Model.Content.v id sections subcontents))
let list' : Ast.list' S.decoder =
let* id = S.field "id" S.string and* source = S.field "source" S.string in
S.succeed Ast.{ id; source }
let t : Ast.setting S.decoder =
- (* Register the data type to fetch it later *)
+ (* Register a type id to attribute broken links to this parsing pass *)
let ty : S.value Type.Id.t = Type.Id.make () in
-
- let settings = Ast.init () in
-
- let* contents = S.field "content" (S.list content)
- (* Register the lists *)
- and* lists = S.field_opt_or ~default:[] "list" (S.list list') in
- let settings = List.fold_left lists ~init:settings ~f:Ast.register_list in
-
- let () = List.iter contents ~f:(Ast.register_content ty settings) in
+ (* Decode the lists first, then the contents: the reference targets are
+ lazy thunks closing over [settings], forced only by [Ast.check] once
+ every content has been registered. *)
+ let* lists = S.field_opt_or ~default:[] "list" (S.list list') in
+ let settings =
+ List.fold_left lists ~init:(Ast.init ()) ~f:Ast.register_list
+ in
+ let env = { ty; settings } in
+ let* contents = S.field "content" (S.list (content env)) in
+ let () = List.iter contents ~f:(Ast.register_content settings) in
(* Ensure the settings are valid in the configuration *)
match Ast.check settings with
@@ -133,7 +131,8 @@ module Make (S : Decoders.Decode.S) = struct
| None -> raise e
| Some Type.Equal ->
let message =
- "Undefined target \"" ^ Model.Identifier.to_string name
+ "Undefined target \""
+ ^ Model.Identifier.to_string name
^ "\". Expected existing list or content"
in
S.fail_with (Decoders.Error.make ~context message))
@@ -142,7 +141,8 @@ module Make (S : Decoders.Decode.S) = struct
| None -> raise e
| Some Type.Equal ->
let message =
- "Unsupported target \"" ^ Model.Identifier.to_string name
+ "Unsupported target \""
+ ^ Model.Identifier.to_string name
^ "\". Lists as target are not implemented yet"
in
S.fail_with (Decoders.Error.make ~context message))
diff --git a/lib/model/field.ml b/lib/model/field.ml
index c7e4667..8242c97 100644
--- a/lib/model/field.ml
+++ b/lib/model/field.ml
@@ -47,9 +47,3 @@ let create :
validation;
criteria;
}
-
-let map : ('a -> 'b) -> 'a t -> 'b t =
- fun f t ->
- let (T field_type) = t.type' in
- let new_type = T (Typ.map f field_type) in
- { t with type' = new_type }
diff --git a/lib/model/field.mli b/lib/model/field.mli
index 0bcb209..caa7b26 100644
--- a/lib/model/field.mli
+++ b/lib/model/field.mli
@@ -28,8 +28,3 @@ val type_ : 'ref t -> 'ref field_ex
val required : _ t -> bool
val validation : _ t -> validation list
val criteria : _ t -> string list Dsl.t option
-
-val map : ('a -> 'b) -> 'a t -> 'b t
-(** Change the type of reference in the field. Used when loading the
- configuration to switch between fields referencing string (the name given in
- the configuration) and actual contents *)
diff --git a/lib/model/model.ml b/lib/model/model.ml
new file mode 100644
index 0000000..ec28b56
--- /dev/null
+++ b/lib/model/model.ml
@@ -0,0 +1,20 @@
+(* Entry point of the [model] library: re-export the submodules, and expose
+ the aliases for the model types with the references resolved to
+ [Reference.t] at the top level, so that use sites can write [Model.content]
+ instead of [Model.content]. *)
+
+module Identifier = Identifier
+module Dsl = Dsl
+module Typ = Typ
+module Reference = Reference
+module Field = Field
+module Section = Section
+module Content = Content
+
+type content = Reference.t Content.t
+type section = Reference.t Section.t
+type field = Reference.t Field.t
+type field_ex = Reference.t Field.field_ex
+type value = Reference.t Typ.value
+type reference = Reference.t Typ.reference
+type 'db typ = ('db, Reference.t) Typ.t
diff --git a/lib/model/model.mli b/lib/model/model.mli
new file mode 100644
index 0000000..afc96f3
--- /dev/null
+++ b/lib/model/model.mli
@@ -0,0 +1,145 @@
+(** {1 The model package}
+
+ This package describes the content model of the application, as declared in
+ the configuration file.
+
+ The model is a tree of {b contents}. Each {!content} has an identifier, a
+ list of {!section}s, and optionally sub-contents (a content with a parent is
+ a subcontent). Each section groups a list of {!field}s. A field has a name,
+ a label translated in several languages, a {!typ} — text, integer, or a
+ reference to another content (single or multivalued) — and optional
+ validation rules expressed in a small expression language (see {!Dsl}).
+
+ For example, this configuration:
+
+ {[
+ { "content": [ {
+ "id": "entity",
+ "section": [ { "field": [
+ { "name": "name",
+ "label": { "en": "Name", "fr": "Nom" },
+ "type": "text",
+ "required": true },
+ { "name": "user_type",
+ "label": {},
+ "type": { "content": "user_type_list", "multivaluated": true } }
+ ] } ]
+ },
+ { "id": "user_type_list" }
+ ] }
+ ]}
+
+ defines two contents: ["entity"], with a required text field ["name"] and a
+ multivalued reference field ["user_type"] pointing to the content
+ ["user_type_list"].
+
+ {2:modules Modules}
+
+ - {!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.
+ - {!Reference} — a link from a reference field to its target content.
+ - {!Dsl} — the expression language used in validation rules and display
+ criteria.
+
+ {2:aliases Resolved types}
+
+ The aliases at the end of this file are the model types with references
+ resolved to {!Reference.t}. They are the types used everywhere outside this
+ package: {!content} is what the configuration loader produces and what the
+ query layer and the HTML renderer operate on. *)
+
+module Identifier = Identifier
+(** Names of contents and fields.
+
+ The type is abstract: the only way to build one is {!Identifier.of_string},
+ which accepts a non-empty sequence of letters, digits, underscores and
+ hyphens (rejecting in particular the double quote and the dot), so an
+ identifier is always safe to embed in a quoted SQL identifier
+ ({!Identifier.quote}). *)
+
+module Content = Content
+(** A content: the unit of the model. It has an {!Identifier.t} [id], a list of
+ {!Section.t}s, a list of sub-contents, and a weak link to its parent. Build
+ one with {!Content.v}; navigate with {!Content.parent} / {!Content.root};
+ iterate over all its fields with {!Content.fields}. *)
+
+type content = Reference.t Content.t
+(** A content : the element produced by the configuration loader and manipulated
+ by the query and rendering layers. *)
+
+module Typ = Typ
+(** The type of a field, and the values a field can carry.
+
+ A field type is one of:
+ - {!Typ.Text} — a string, stored as a nullable column;
+ - {!Typ.Integer} — an integer, stored as a nullable column;
+ - {!Typ.MonoReference} — a reference to a single content, stored as a
+ nullable foreign key;
+ - {!Typ.MultiReference} — a reference to several contents of the same kind,
+ stored in a dedicated table ["content.field"].
+
+ A {!Typ.reference} record carries the target of a reference field (a
+ {!Reference.t}) 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
+ sequence of references, for multivalued fields). *)
+
+type 'db typ = ('db, Reference.t) 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)
+*)
+
+module Reference = Reference
+(** A link from a reference field to its target content.
+
+ The target is a lazy pointer ({!Reference.v} / {!Reference.get}): it is
+ created while the configuration is parsed, before the target content
+ necessarily exists, and resolved once all contents are registered. This is
+ what allows contents to reference each other, including cyclically.
+
+ This module also defines the aliases for the resolved model types
+ ({!Reference.content}, {!Reference.field}, ...), re-exported at the top
+ level of this package as {!content}, {!field}, ... *)
+
+type reference = Reference.t Typ.reference
+(** A reference field target: a lazy pointer to a {!content}, plus optional
+ constraints *)
+
+module Section = Section
+(** A group of fields displayed together, with a per-language [label] and an
+ optional [criteria] controlling when the whole section is displayed. *)
+
+type section = Reference.t Section.t
+(** A section of {!content} *)
+
+type value = Reference.t Typ.value
+(** A value provided for a field when inserting/updating data *)
+
+module Field = Field
+(** A field of a content.
+
+ A field has a {!Identifier.t} name, a [label] as a list of
+ [(language, text)] pairs, a {!Typ.field_ex} type, a [required] flag, and
+ optionally a [placeholder], [validation] rules (a {!Dsl} expression plus a
+ per-language error message) and a [criteria] controlling when the field is
+ displayed. Build one with {!Field.create}. *)
+
+type field = Reference.t Field.t
+(** A field of {!content} *)
+
+type field_ex = Reference.t Field.field_ex
+(** The type descriptor of {!field} *)
+
+module Dsl = Dsl
+(** A small expression language, used for validation rules (["rule"] key of a
+ field) and display criteria (["criteria"] key of a field or section).
+
+ An expression is built from literals, integers, paths to values
+ ({!Dsl.ValuePath}) or to other contents ({!Dsl.ContentPath}), the usual
+ comparison and arithmetic operators ([=], [<>], [<], [>], [<=], [>=], [+],
+ [-], [/], [and], [or]), and functions: [ucase], [trim], [length], [concat],
+ [join], [nvl], [if], [matches], [substring], [uniq]. For example the rule
+ ["length(:) >= 2"] checks that the field value has at least two characters.
+*)
diff --git a/lib/model/tests/identifier.t b/lib/model/tests/identifier.t
index 39460ff..0b737c7 100644
--- a/lib/model/tests/identifier.t
+++ b/lib/model/tests/identifier.t
@@ -34,5 +34,5 @@ A composite name is built from two valid identifiers:
An invalid part is rejected when building a composite name:
$ tests --identifier dot 'bad"name' ref
- Fatal error: exception Model.Identifier.Invalid("bad\"name")
+ Fatal error: exception Model__Identifier.Invalid("bad\"name")
[2]
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
diff --git a/lib/query/create.ml b/lib/query/create.ml
index 05aec0f..a40e722 100644
--- a/lib/query/create.ml
+++ b/lib/query/create.ml
@@ -36,7 +36,7 @@ ADD FOREIGN KEY (best_a_id)
REFERENCES answer(a_id);
*)
-let get_foreign_statement : Model.Identifier.t -> _ Model.Content.t -> string =
+let get_foreign_statement : Model.Identifier.t -> Model.content -> string =
fun property content ->
"FOREIGN KEY("
^ Model.Identifier.quote property
@@ -49,9 +49,9 @@ let get_foreign_statement : Model.Identifier.t -> _ Model.Content.t -> string =
(** Create the dedicated table required for multivaluated fields *)
let create_external_reference_table :
Caqti_lwt.connection ->
- Model.Reference.t Model.Content.t ->
+ Model.content ->
foreign list ->
- Model.Reference.t Model.Field.t ->
+ Model.field ->
foreign list Lwt.t =
fun db content acc field ->
match Model.Field.type_ field with
@@ -98,9 +98,9 @@ let create_external_reference_table :
(** Generate the create table for a content *)
let rec create_table :
Caqti_lwt.connection ->
- foreign list * Model.Reference.t Model.Content.t list ->
- _ Model.Content.t ->
- (foreign list * Model.Reference.t Model.Content.t list) Lwt.t =
+ foreign list * Model.content list ->
+ Model.content ->
+ (foreign list * Model.content list) Lwt.t =
fun db (alter_queries, created) content ->
let module Identifier = Model.Identifier in
(* The foreign keys are added at the end of the query, and need to be
diff --git a/lib/query/encoder.ml b/lib/query/encoder.ml
index 5ee6727..ba5435e 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.Reference.t Model.Typ.value
+ type env = string -> Model.value
let f : type a. env -> a Intf.data -> a t =
fun provider Intf.{ name; content_type } ->
diff --git a/lib/query/insert.ml b/lib/query/insert.ml
index 6e95631..ab0dc2b 100644
--- a/lib/query/insert.ml
+++ b/lib/query/insert.ml
@@ -3,9 +3,9 @@
(** Transform the list of fields from the model into a tuple with the type of
each field associated with *)
let collect_fields :
- ?init_fields:Model.Reference.t Model.Field.t list ->
- ?filter:('a Model.Field.t -> bool) ->
- 'a Model.Content.t ->
+ ?init_fields:Model.field list ->
+ ?filter:(Model.field -> bool) ->
+ Model.content ->
Intf.t =
fun ?(init_fields = []) ?filter content ->
let field_seq =
@@ -25,8 +25,8 @@ module MultiReferencesInsert = struct
type 'a t = {
db : Caqti_lwt.connection;
parent_id : int64;
- content : Model.Reference.t Model.Content.t;
- provider : string -> Model.Reference.t Model.Typ.value;
+ content : Model.content;
+ provider : string -> Model.value;
k : (unit, Caqti.Error.t) result Lwt.t;
}
@@ -105,8 +105,8 @@ module Fold = Encoder.Fold (MultiReferencesInsert)
*)
let insert_returning :
Caqti_lwt.connection ->
- Model.Reference.t Model.Content.t ->
- (string -> Model.Reference.t Model.Typ.value) ->
+ Model.content ->
+ (string -> Model.value) ->
int64 Lwt.t =
fun db content provider ->
let module Db = (val db) in
@@ -174,9 +174,9 @@ let insert_returning :
(** Insert an entry for a specific content *)
let insert :
Caqti_lwt.connection ->
- Model.Reference.t Model.Content.t ->
+ Model.content ->
?parent:int64 ->
- (string -> Model.Reference.t Model.Typ.value) ->
+ (string -> Model.value) ->
int64 Lwt.t =
fun db content ?parent provider ->
let provider =
@@ -200,14 +200,14 @@ let insert :
let insert_references :
Caqti_lwt.connection ->
- Model.Reference.t Model.Content.t ->
+ Model.content ->
Int64.t ->
Model.Identifier.t ->
- Model.Reference.t Model.Typ.value ->
+ Model.value ->
Int64.t Intf.data ->
unit Lwt.t =
fun db content content_id field_name value data ->
- let provider : string -> Model.Reference.t Model.Typ.value = function
+ let provider : string -> Model.value = function
| n when String.equal n (Model.Identifier.to_string field_name) -> value
| _ -> Model.Typ.None
in
diff --git a/lib/query/insert.mli b/lib/query/insert.mli
index 3f39bd5..ef8c5c9 100644
--- a/lib/query/insert.mli
+++ b/lib/query/insert.mli
@@ -2,18 +2,18 @@
val insert :
Caqti_lwt.connection ->
- Model.Reference.t Model.Content.t ->
+ Model.content ->
?parent:int64 ->
- (string -> Model.Reference.t Model.Typ.value) ->
+ (string -> Model.value) ->
int64 Lwt.t
(** See [Query.insert] *)
val insert_references :
Caqti_lwt.connection ->
- Model.Reference.t Model.Content.t ->
+ Model.content ->
Int64.t ->
Model.Identifier.t ->
- Model.Reference.t Model.Typ.value ->
+ Model.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/intf.ml b/lib/query/intf.ml
index 8c7ead5..45fec11 100644
--- a/lib/query/intf.ml
+++ b/lib/query/intf.ml
@@ -1,7 +1,4 @@
-type 'a data = {
- name : Model.Identifier.t;
- content_type : ('a, Model.Reference.t) Model.Typ.t;
-}
+type 'a data = { name : Model.Identifier.t; content_type : 'a Model.typ }
(** The data is the name of the field and the type associated with *)
type t_type = TT : 'a data -> t_type [@@unboxed]
diff --git a/lib/query/query.mli b/lib/query/query.mli
index 24e0591..89b114d 100644
--- a/lib/query/query.mli
+++ b/lib/query/query.mli
@@ -1,8 +1,8 @@
val insert :
Caqti_lwt.connection ->
- Model.Reference.t Model.Content.t ->
+ Model.content ->
?parent:int64 ->
- (string -> Model.Reference.t Model.Typ.value) ->
+ (string -> Model.value) ->
int64 Lwt.t
(** Insert a new element and return the ID.
@@ -24,6 +24,6 @@ val create : Caqti_lwt.connection -> Configuration.Ast.setting -> unit Lwt.t
val update :
Caqti_lwt.connection ->
Int64.t ->
- Model.Reference.t Model.Content.t ->
- (string -> Model.Reference.t Model.Typ.value) ->
+ Model.content ->
+ (string -> Model.value) ->
unit Lwt.t
diff --git a/lib/query/update.ml b/lib/query/update.ml
index d828b6b..2475d40 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.Reference.t Model.Typ.value
+type provider = string -> Model.value
module UpdateTemplate = Encoder.Map (struct
type 'a t = string
@@ -21,8 +21,8 @@ end)
let update_fields :
Caqti_lwt.connection ->
- 'a Model.Content.t ->
- (Intf.t_type * (string * Model.Reference.t Model.Typ.value)) list ->
+ Model.content ->
+ (Intf.t_type * (string * Model.value)) list ->
int64 ->
unit Lwt.t =
fun db content fields id ->
@@ -77,7 +77,7 @@ module ReferencesSet = Set.Make (Int64)
let update_mulivaluated_field :
Caqti_lwt.connection ->
- Model.Reference.t Model.Content.t ->
+ Model.content ->
(Model.Identifier.t * (int64, Model.Reference.t) Model.Typ.t * int64 Seq.t)
list ->
int64 ->
@@ -148,8 +148,8 @@ let update_mulivaluated_field :
let q :
Caqti_lwt.connection ->
Int64.t ->
- Model.Reference.t Model.Content.t ->
- (string -> Model.Reference.t Model.Typ.value) ->
+ Model.content ->
+ (string -> Model.value) ->
unit Lwt.t =
fun db id content provider ->
let module Db = (val db) in
diff --git a/test/common.ml b/test/common.ml
index 98871a3..6223846 100644
--- a/test/common.ml
+++ b/test/common.ml
@@ -18,7 +18,7 @@ let connect : Configuration.Ast.setting -> Caqti_lwt.connection Lwt.t =
(** Load the given content and return the values as a json *)
let content_as_json :
- 'a Model.Content.t -> (int64, string, [ `One ]) Caqti.Template.Request.t =
+ Model.content -> (int64, string, [ `One ]) Caqti.Template.Request.t =
fun content ->
let fields =
Model.Content.fields content
@@ -57,7 +57,7 @@ let content_as_json :
^ Model.Identifier.quote (Model.Content.id content)
^ " where " ^ Model.Identifier.quote (Model.Identifier.id) ^ "=?")
-let query_values (table : Model.Reference.t Model.Content.t)
+let query_values (table : Model.content)
(feature : Model.Identifier.t) =
let open Caqti.Templater in
direct T.(int64 -->* string)
@@ -72,7 +72,7 @@ let query_values (table : Model.Reference.t Model.Content.t)
content. *)
let collect_references :
Caqti_lwt.connection ->
- Model.Reference.t Model.Content.t ->
+ Model.content ->
int64 ->
(string * int64 * string) list Lwt.t =
fun db content id ->
diff --git a/test/insert.ml b/test/insert.ml
index 451d857..b5da90d 100644
--- a/test/insert.ml
+++ b/test/insert.ml
@@ -14,8 +14,8 @@ let pp : Format.formatter -> t -> unit =
(fun formatter (content_id, id, content) ->
Format.fprintf formatter "@[<v 2>%s:%Ld@;%s@]" content_id id content))
-type content = Model.Reference.t Model.Content.t
-type value = Model.Reference.t Model.Typ.value
+type content = Model.content
+type value = Model.value
type properties = (string * value) list
(** [insert_and_get_result db content entry] will insert the value in the
diff --git a/test/update.ml b/test/update.ml
index 6f22466..74208de 100644
--- a/test/update.ml
+++ b/test/update.ml
@@ -6,8 +6,8 @@ type actions =
| Unset of string
| Text of string * string
| Integer of string * int
- | Reference of Model.Reference.t Model.Typ.reference * string * int
- | References of Model.Reference.t Model.Typ.reference * string * int64 Seq.t
+ | Reference of Model.reference * string * int
+ | References of Model.reference * string * int64 Seq.t
let actions : actions list ref = ref []