From 830e7c5d27d161b91402414e6ecd49f1ed818bfb Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Fri, 27 Mar 2026 08:48:05 +0100 Subject: Initial commit --- lib/model/content.ml | 30 +++++++++++ lib/model/content.mli | 18 +++++++ lib/model/dsl.ml | 135 +++++++++++++++++++++++++++++++++++++++++++++++ lib/model/dune | 5 ++ lib/model/field.ml | 55 +++++++++++++++++++ lib/model/field.mli | 35 ++++++++++++ lib/model/reference.ml | 4 ++ lib/model/reference.mli | 4 ++ lib/model/section.ml | 7 +++ lib/model/tests/dune | 11 ++++ lib/model/tests/parent.t | 91 ++++++++++++++++++++++++++++++++ lib/model/typ.ml | 62 ++++++++++++++++++++++ 12 files changed, 457 insertions(+) create mode 100644 lib/model/content.ml create mode 100644 lib/model/content.mli create mode 100644 lib/model/dsl.ml create mode 100644 lib/model/dune create mode 100644 lib/model/field.ml create mode 100644 lib/model/field.mli create mode 100644 lib/model/reference.ml create mode 100644 lib/model/reference.mli create mode 100644 lib/model/section.ml create mode 100644 lib/model/tests/dune create mode 100644 lib/model/tests/parent.t create mode 100644 lib/model/typ.ml (limited to 'lib/model') diff --git a/lib/model/content.ml b/lib/model/content.ml new file mode 100644 index 0000000..2760715 --- /dev/null +++ b/lib/model/content.ml @@ -0,0 +1,30 @@ +open StdLabels + +type 'a t = { + id : string; + sections : 'a Section.t list; + subcontents : 'a t list; + parent : 'a t Weak.t; +} + +let v : string -> 'a Section.t list -> 'a t list -> 'a t = + fun id sections subcontents -> + let parent = Weak.create 1 in + let content = { id; sections; subcontents; parent } in + (* Fill the parent for all existing subcontent declared in the type + There is no need to repeat the same for the subcontents as they all are + already created in the same way. + *) + List.iter content.subcontents ~f:(fun v -> Weak.set v.parent 0 (Some content)); + content + +let id t = t.id +let sections t = t.sections +let subcontents t = t.subcontents +let parent t = Weak.get t.parent 0 +let rec root t = match parent t with None -> t | Some t -> root t + +let fields : 'a t -> 'a Field.t Seq.t = + fun t -> + List.to_seq t.sections + |> Seq.flat_map (fun section -> List.to_seq section.Section.fields) diff --git a/lib/model/content.mli b/lib/model/content.mli new file mode 100644 index 0000000..ab8d838 --- /dev/null +++ b/lib/model/content.mli @@ -0,0 +1,18 @@ +type 'a t + +val v : string -> 'a Section.t list -> 'a t list -> 'a t +val id : _ t -> string + +val subcontents : 'a t -> 'a t list +(** Return the direct subcontent *) + +val sections : 'a t -> 'a Section.t list + +val parent : 'a t -> 'a t option +(** Get the parent type for an element, if such exists *) + +val root : 'a t -> 'a t +(** Return the root element, which is the content without any parent *) + +val fields : 'a t -> 'a Field.t Seq.t +(** Iterate over the fields in the content *) diff --git a/lib/model/dsl.ml b/lib/model/dsl.ml new file mode 100644 index 0000000..0f873f3 --- /dev/null +++ b/lib/model/dsl.ml @@ -0,0 +1,135 @@ +open StdLabels + +exception UnknowFunction of string + +type 'a t = + | Empty + | Expr of 'a t + | Literal of string + | Integer of string + | ValuePath of 'a + | ContentPath of 'a + | Nvl of 'a t list + | Join of string * 'a t list + | BOperator of binary_operator * 'a t * 'a t + | GEquality of binary_operator * 'a t * 'a t list + | Function of funct * 'a t list +[@@deriving show, eq, ord] + +and binary_operator = + | Equal + | Different + | Add + | Minus + | Division + | LT + | GT + | LTE + | GTE + | And + | Or +[@@deriving ord] + +and funct = + | Cmp + | Trim + | Ucase + | If + | Concat + | Matches + | Substring + | Length + | Uniq + +let name_of_function : funct -> string = function + | Ucase -> "ucase" + | Trim -> "trim" + | Cmp -> "cmp" + | If -> "if" + | Concat -> "concat" + | Matches -> "matches" + | Substring -> "substring" + | Length -> "length" + | Uniq -> "uniq" + +let function_of_name : 'a t list -> string -> 'a t = + fun param f -> + match (String.lowercase_ascii f, param) with + | "nvl", _ -> Nvl param + | "join", Literal sep :: tl -> Join (sep, tl) + | "join", Empty :: tl -> Join ("", tl) + (* Branch function *) + | "if", [ _; _; _ ] -> Function (If, param) + (* String functions *) + | "matches", _ -> Function (Matches, param) + | "substring", _ -> Function (Substring, param) + | "length", [ _ ] -> Function (Length, param) + | "ucase", [ _ ] -> Function (Ucase, param) + | "trim", [ _ ] -> Function (Trim, param) + | "concat", _ -> Function (Concat, param) + | "uniq", [ _ ] -> Function (Uniq, param) + | _ -> + (*Function (other, param)*) + raise (UnknowFunction f) + +let name_of_operator = function + | Equal -> "=" + | Different -> "<>" + | Add -> "+" + | Minus -> "-" + | Division -> "/" + | LT -> "<" + | GT -> ">" + | LTE -> "<=" + | GTE -> ">=" + | And -> " and " + | Or -> " or " + +let rec cmp : ('a -> 'a -> int) -> 'a t -> 'a t -> int = + fun f e1 e2 -> + match (e1, e2) with + | Empty, Empty -> 0 + | Literal l1, Literal l2 -> String.compare l1 l2 + | Integer l1, Integer l2 -> String.compare l1 l2 + | ValuePath p1, ValuePath p2 -> f p1 p2 + | ContentPath p1, ContentPath p2 -> f p1 p2 + | BOperator (n1, arg11, arg12), BOperator (n2, arg21, arg22) -> ( + match compare_binary_operator n1 n2 with + | 0 -> ( + match cmp f arg11 arg21 with 0 -> cmp f arg12 arg22 | other -> other) + | other -> other) + (* Any other case *) + | other1, other2 -> Stdlib.compare other1 other2 + +let fold_values : f:('b -> 'a -> 'b) -> init:'b -> 'a t -> 'b = + fun ~f ~init expression -> + let rec _f acc = function + | Empty | Literal _ | Integer _ -> acc + | Expr e -> _f acc e + | ValuePath p -> f acc p + | ContentPath p -> f acc p + | Function (_, pp) | Nvl pp | Join (_, pp) -> + List.fold_left ~f:(fun acc a -> _f acc a) ~init:acc pp + | BOperator (_, arg1, arg2) -> _f (_f acc arg1) arg2 + | GEquality (_, arg1, arg2) -> + let eval1 = List.fold_left ~f:(fun acc a -> _f acc a) ~init:acc arg2 in + _f eval1 arg1 + in + _f init expression + +let map : type a b. f:(a -> b) -> a t -> b t = + fun ~f expression -> + let rec map = function + | Expr e -> Expr (map e) + | Empty -> Empty + | Literal s -> Literal s + | Integer i -> Integer i + | ValuePath p -> ValuePath (f p) + | ContentPath p -> ContentPath (f p) + | Function (name, pp) -> Function (name, List.map ~f:map pp) + | Nvl pp -> Nvl (List.map ~f:map pp) + | Join (sep, pp) -> Join (sep, List.map ~f:map pp) + | BOperator (n, arg1, arg2) -> BOperator (n, map arg1, map arg2) + | GEquality (n, arg1, args) -> GEquality (n, map arg1, List.map ~f:map args) + in + map expression diff --git a/lib/model/dune b/lib/model/dune new file mode 100644 index 0000000..12dfa56 --- /dev/null +++ b/lib/model/dune @@ -0,0 +1,5 @@ +(library + (name model) + (libraries) + (preprocess + (pps ppx_deriving.ord ppx_deriving.show ppx_deriving.eq))) diff --git a/lib/model/field.ml b/lib/model/field.ml new file mode 100644 index 0000000..8ea38fd --- /dev/null +++ b/lib/model/field.ml @@ -0,0 +1,55 @@ +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 t = { + name : string; (** 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 *) +} + +let name : 'ref t -> string = 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 create : + 't field_ex -> + textI18n -> + ?placeholder:textI18n -> + ?icon:unit -> + ?required:bool -> + ?validation:validation list -> + ?criteria:string list Dsl.t -> + string -> + 't t = + fun type_ label ?placeholder ?icon ?(required = false) ?(validation = []) + ?criteria name -> + { + name; + label; + placeholder; + icon; + type' = type_; + required; + 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 new file mode 100644 index 0000000..a121871 --- /dev/null +++ b/lib/model/field.mli @@ -0,0 +1,35 @@ +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 'ref t +(** Description of a field *) + +val create : + 'ref field_ex -> + textI18n -> + ?placeholder:textI18n -> + ?icon:unit -> + ?required:bool -> + ?validation:validation list -> + ?criteria:string list Dsl.t -> + string -> + 'ref t + +val name : _ t -> string +(** Name (identifier) for the field*) + +val label : _ t -> textI18n +val placeholder : _ t -> textI18n option +val icon : _ t -> unit option +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/reference.ml b/lib/model/reference.ml new file mode 100644 index 0000000..496eb9d --- /dev/null +++ b/lib/model/reference.ml @@ -0,0 +1,4 @@ +type t = T : t Content.t Lazy.t -> t [@@unboxed] + +let v : (unit -> t Content.t) -> t = fun f -> T (lazy (f ())) +let get : t -> t Content.t = fun (T target) -> Lazy.force target diff --git a/lib/model/reference.mli b/lib/model/reference.mli new file mode 100644 index 0000000..7a9d135 --- /dev/null +++ b/lib/model/reference.mli @@ -0,0 +1,4 @@ +type t + +val v : (unit -> t Content.t) -> t +val get : t -> t Content.t diff --git a/lib/model/section.ml b/lib/model/section.ml new file mode 100644 index 0000000..9e871e3 --- /dev/null +++ b/lib/model/section.ml @@ -0,0 +1,7 @@ +type 'a t = { + label : Field.textI18n; + icon : unit option; + criteria : string list Dsl.t option; + (** Display the section when the criteria is valid*) + fields : 'a Field.t list; +} diff --git a/lib/model/tests/dune b/lib/model/tests/dune new file mode 100644 index 0000000..c5913da --- /dev/null +++ b/lib/model/tests/dune @@ -0,0 +1,11 @@ +; Add the test library in the path + +(env + (_ + (binaries %{project_root}/test/tests.exe))) + +; And depend of it. The binary will be available in the +; cram tests under the name "tests" + +(cram + (deps %{bin:tests})) diff --git a/lib/model/tests/parent.t b/lib/model/tests/parent.t new file mode 100644 index 0000000..8e77578 --- /dev/null +++ b/lib/model/tests/parent.t @@ -0,0 +1,91 @@ + +A root content does not have any parent + + $ tests -parent "entity" < { + > "content": [ + > { + > "id": "entity" + > } + > ] + > } + > EOF + [No parent] + +A root content is the root of itself + + $ tests -parent -root "entity" < { + > "content": [ + > { + > "id": "entity" + > } + > ] + > } + > EOF + "entity" + +The parent of a subcontent shall never been None, and will target the container + + $ tests -parent "details" < { + > "content": [ + > { + > "id": "entity", + > "subcontent": [ + > { + > "id": "details" + > } + > ] + > } + > ] + > } + > EOF + "entity" + +The parent element can be a subcontent itself + + $ tests -parent "level2" < { + > "content": [ + > { + > "id": "entity", + > "subcontent": [ + > { + > "id": "level1", + > "subcontent": [ + > { + > "id": "level2" + > } + > ] + > } + > ] + > } + > ] + > } + > EOF + "level1" + +The root element is the top content + + $ tests -parent -root "level2" < { + > "content": [ + > { + > "id": "entity", + > "subcontent": [ + > { + > "id": "level1", + > "subcontent": [ + > { + > "id": "level2" + > } + > ] + > } + > ] + > } + > ] + > } + > EOF + "entity" + diff --git a/lib/model/typ.ml b/lib/model/typ.ml new file mode 100644 index 0000000..2770885 --- /dev/null +++ b/lib/model/typ.ml @@ -0,0 +1,62 @@ +type 'a reference = { + t : 'a; (** The content targeted by this field. *) + constraints : string list Dsl.t option; + (** Retrict only the values to thoses ones *) +} + +(** 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 option, 't) t + (** Multireferences properties allows only one kind of content. + + 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 + | Text -> None + | MultiReference _ -> None + | MonoReference _ -> None + +let eq : type a b c. (a, c) t -> (b, 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 + +(** Represent the value in the database for a given field. + +Example for inserting data +[ + (* Function providing a value for each field to insert in the database *) + + let f : string -> Model.Reference.t Model.Field.value = function + | "siret" -> V (Model.Field.Text, Some "siret") + | "name" -> V (Model.Field.Text, Some "name") + | _ -> None + + let* id = Query.insert db entity_content f in +] + +*) +type 't value = + | V : ('a option, 't) t * 'a -> 't value + (** V is the value for the field. The field type is also reported in order + to give the compiler the nature of the data *) + | None (** None tell the field is empty in the database *) -- cgit v1.2.3