summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-11-20 18:57:38 +0100
committerSébastien Dailly <sebastien@dailly.me>2026-11-20 18:57:38 +0100
commit172a02b80d8dbdcfe09129d7fa76015bb5ec4d79 (patch)
treecb8613d53ce8051e39937ff05f8dc9b0fe80320e /lib
parentfa4970bf1b74f3fa0b2f63cbd461d98c251dc0f0 (diff)
Refactoring
Diffstat (limited to 'lib')
-rw-r--r--lib/model/field.ml77
-rw-r--r--lib/model/field.mli9
-rw-r--r--lib/query/insert.ml58
-rw-r--r--lib/query/update.ml51
-rw-r--r--lib/value/value.ml4
5 files changed, 110 insertions, 89 deletions
diff --git a/lib/model/field.ml b/lib/model/field.ml
index 8242c97..feb712d 100644
--- a/lib/model/field.ml
+++ b/lib/model/field.ml
@@ -2,28 +2,33 @@ type textI18n = (string * string) list
(** A label is just an association list*)
type validation = { rule : string list Dsl.t; message : textI18n }
-type 't field_ex = T : ('a, 't) Typ.t -> 't field_ex
+type 't field_ex = T : ('db, 'ocaml, 't) Typ.t -> 't field_ex
-type 't t = {
- name : Identifier.t; (** The internal name *)
- label : textI18n;
- placeholder : textI18n option;
- icon : unit option;
- type' : 't field_ex;
- required : bool;
- validation : validation list;
- criteria : string list Dsl.t option;
- (** Display the field when the criteria is valid *)
-}
+type 't t =
+ | F : {
+ named : ('db, 'ocaml, 't) Typ.t Typ.named;
+ (** Name and type of the field, tied together *)
+ label : textI18n;
+ placeholder : textI18n option;
+ icon : unit option;
+ required : bool;
+ validation : validation list;
+ criteria : string list Dsl.t option;
+ (** Display the field when the criteria is valid *)
+ }
+ -> 't t
+ (** The record is a GADT so that [named] keeps its existential data type
+ ['a] — this is what lets {!named_of} re-stamp it with a known type.
+ The type stays abstract in the interface. *)
-let name : 'ref t -> Identifier.t = fun t -> t.name
-let label : 'ref t -> textI18n = fun t -> t.label
-let placeholder : 'ref t -> textI18n option = fun t -> t.placeholder
-let icon : 'ref t -> unit option = fun t -> t.icon
-let type_ : 'ref t -> 'ref field_ex = fun t -> t.type'
-let required : 'ref t -> bool = fun t -> t.required
-let validation : 'ref t -> validation list = fun t -> t.validation
-let criteria : 'ref t -> string list Dsl.t option = fun t -> t.criteria
+let name : 'ref t -> Identifier.t = fun (F f) -> Typ.name f.named
+let label : 'ref t -> textI18n = fun (F f) -> f.label
+let placeholder : 'ref t -> textI18n option = fun (F f) -> f.placeholder
+let icon : 'ref t -> unit option = fun (F f) -> f.icon
+let type_ : 'ref t -> 'ref field_ex = fun (F f) -> T (Typ.typ f.named)
+let required : 'ref t -> bool = fun (F f) -> f.required
+let validation : 'ref t -> validation list = fun (F f) -> f.validation
+let criteria : 'ref t -> string list Dsl.t option = fun (F f) -> f.criteria
let create :
't field_ex ->
@@ -35,15 +40,25 @@ let create :
?criteria:string list Dsl.t ->
Identifier.t ->
't t =
- fun type_ label ?placeholder ?icon ?(required = false) ?(validation = [])
+ fun (T typ) label ?placeholder ?icon ?(required = false) ?(validation = [])
?criteria name ->
- {
- name;
- label;
- placeholder;
- icon;
- type' = type_;
- required;
- validation;
- criteria;
- }
+ F
+ {
+ named = Typ.named ~name ~typ;
+ label;
+ placeholder;
+ icon;
+ required;
+ validation;
+ criteria;
+ }
+
+let named_of : type db ocaml ref.
+ ref t -> (db, ocaml, ref) Typ.t -> ((db, ocaml, ref) Typ.t) Typ.named option =
+ fun (F f) typ ->
+ match Typ.eq typ f.named.typ with
+ | None -> None
+ | Some Type.Equal -> (
+ match Typ.eq_ocaml typ f.named.typ with
+ | None -> None
+ | Some Type.Equal -> Some f.named)
diff --git a/lib/model/field.mli b/lib/model/field.mli
index caa7b26..0619f33 100644
--- a/lib/model/field.mli
+++ b/lib/model/field.mli
@@ -2,11 +2,18 @@ type textI18n = (string * string) list
(** A label is just an association list*)
type validation = { rule : string list Dsl.t; message : textI18n }
-type 't field_ex = T : ('a, 't) Typ.t -> 't field_ex
+type 't field_ex = T : ('db, 'ocaml, 't) Typ.t -> 't field_ex
type 'ref t
(** Description of a field *)
+val named_of :
+ 'ref t -> ('db, 'ocaml, 'ref) Typ.t -> (('db, 'ocaml, 'ref) Typ.t) Typ.named option
+(** [named_of field typ] is [Some named] — the field's {!Typ.named} type,
+ stamped with ['db] and ['ocaml] — when [typ] matches the declared type of
+ [field] (checked with {!Typ.eq} and {!Typ.eq_ocaml}); [None] on type
+ mismatch *)
+
val create :
'ref field_ex ->
textI18n ->
diff --git a/lib/query/insert.ml b/lib/query/insert.ml
index 01b2dae..703d5a9 100644
--- a/lib/query/insert.ml
+++ b/lib/query/insert.ml
@@ -64,37 +64,35 @@ module MultiReferencesInsert = struct
and target = Identifier.quote Identifier.target in
match content_type with
| Model.Typ.MultiReference _target -> (
- let result = provider (Model.Identifier.to_string name) in
- match result with
- | Value.Data.Any d -> (
- match d with
- | Value.Data.None -> acc
- | Value.Data.V (field_type', values) -> (
- match Model.Typ.eq_ocaml content_type field_type' with
- | None -> raise Encoder.TypeMismatch
- | Some Equal ->
- let q =
- Croquette_common.Format.format_string (fun format ->
- Format.fprintf format
- "INSERT INTO %s (%s, %s) VALUES (?, ?)"
- (Identifier.quote (Identifier.dot table name))
- parent target)
- in
- let request : request =
- Caqti.Templater.(dynamic T.(t2 int64 int64 -->. unit) q)
- in
- let k =
- (* Successively insert all the values in the dedicated table *)
- let* res =
- Seq.fold_left (accumulate db request parent_id) k values
- in
- let* () = Caqti_lwt.or_fail res in
- let* res = Db.deallocate request in
- let* () = Caqti_lwt.or_fail res in
- Lwt.return res
- in
+ let (Value.Data.Any d) = provider (Model.Identifier.to_string name) in
+ match d with
+ | Value.Data.None -> acc
+ | Value.Data.V (field_type', values) -> (
+ match Model.Typ.eq_ocaml content_type field_type' with
+ | None -> raise Encoder.TypeMismatch
+ | Some Equal ->
+ let q =
+ Croquette_common.Format.format_string (fun format ->
+ Format.fprintf format
+ "INSERT INTO %s (%s, %s) VALUES (?, ?)"
+ (Identifier.quote (Identifier.dot table name))
+ parent target)
+ in
+ let request : request =
+ Caqti.Templater.(dynamic T.(t2 int64 int64 -->. unit) q)
+ in
+ let k =
+ (* Successively insert all the values in the dedicated table *)
+ let* res =
+ Seq.fold_left (accumulate db request parent_id) k values
+ in
+ let* () = Caqti_lwt.or_fail res in
+ let* res = Db.deallocate request in
+ let* () = Caqti_lwt.or_fail res in
+ Lwt.return res
+ in
- { acc with k })))
+ { acc with k }))
| _ -> raise Encoder.TypeMismatch
end
diff --git a/lib/query/update.ml b/lib/query/update.ml
index ec1510b..5b53cb7 100644
--- a/lib/query/update.ml
+++ b/lib/query/update.ml
@@ -78,7 +78,8 @@ module ReferencesSet = Set.Make (Int64)
let update_mulivaluated_field :
Caqti_lwt.connection ->
Model.content ->
- (Model.Identifier.t * Model.target Model.Typ.multireference * int64 Seq.t) list ->
+ (Model.Identifier.t * Model.target Model.Typ.multireference * int64 Seq.t)
+ list ->
int64 ->
unit Lwt.t =
fun db content fields id ->
@@ -165,33 +166,33 @@ let q :
Seq.fold_left
(fun ((fields, references) as acc :
(Intf.t_type * (string * Value.value)) list
- * (Model.Identifier.t * Model.target Model.Typ.multireference * int64 Seq.t) list)
- field ->
+ * (Model.Identifier.t
+ * Model.target Model.Typ.multireference
+ * int64 Seq.t)
+ list) field ->
let name = Model.Field.name field in
let (Model.Field.T content_type) = Model.Field.type_ field in
let tt = Intf.TT { name; content_type } in
- let value = provider (Model.Identifier.to_string name) in
- match value with
- | Value.Data.Any d -> (
- match content_type with
- | Model.Typ.MultiReference _ -> (
- match d with
- | Value.Data.None -> acc
- | Value.Data.V (t, seq) -> (
- match
- ( Model.Typ.eq content_type t,
- Model.Typ.eq_ocaml content_type t )
- with
- | Some Type.Equal, Some Type.Equal ->
- (fields, (name, t, seq) :: references)
- | _ -> raise Encoder.TypeMismatch))
- | _ -> (
- match d with
- | Value.Data.None -> acc
- | Value.Data.V _ as dv ->
- ( (tt, (Model.Identifier.to_string name, Value.Data.any dv))
- :: fields,
- references ))))
+ let (Value.Data.Any d) = provider (Model.Identifier.to_string name) in
+ match content_type with
+ | Model.Typ.MultiReference _ -> (
+ match d with
+ | Value.Data.None -> acc
+ | Value.Data.V (t, seq) -> (
+ match
+ ( Model.Typ.eq content_type t,
+ Model.Typ.eq_ocaml content_type t )
+ with
+ | Some Type.Equal, Some Type.Equal ->
+ (fields, (name, t, seq) :: references)
+ | _ -> raise Encoder.TypeMismatch))
+ | _ -> (
+ match d with
+ | Value.Data.None -> acc
+ | Value.Data.V _ as dv ->
+ ( (tt, (Model.Identifier.to_string name, Value.Data.any dv))
+ :: fields,
+ references )))
([], [])
(Model.Content.fields content)
in
diff --git a/lib/value/value.ml b/lib/value/value.ml
index 3012667..84bed4c 100644
--- a/lib/value/value.ml
+++ b/lib/value/value.ml
@@ -25,11 +25,11 @@ 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 =
+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 =
+ '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