diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-03-30 17:56:42 +0200 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-04-01 09:49:08 +0200 |
| commit | 4af7d2f6ff8b38365c97a89c295710f5ed3266f5 (patch) | |
| tree | 8b641d4992eac896223c5169a8b7301205685d85 /lib/model | |
| parent | 830e7c5d27d161b91402414e6ecd49f1ed818bfb (diff) | |
Manage insertion for multireferences fields
Diffstat (limited to 'lib/model')
| -rw-r--r-- | lib/model/content.ml | 13 | ||||
| -rw-r--r-- | lib/model/content.mli | 3 | ||||
| -rw-r--r-- | lib/model/field.mli | 2 | ||||
| -rw-r--r-- | lib/model/typ.ml | 10 |
4 files changed, 23 insertions, 5 deletions
diff --git a/lib/model/content.ml b/lib/model/content.ml index 2760715..4fe8e92 100644 --- a/lib/model/content.ml +++ b/lib/model/content.ml @@ -28,3 +28,16 @@ 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) + +let find_target : 'a t -> string -> 'a option = + fun t name -> + let feature_opt = + Seq.find (fun f -> String.equal (Field.name f) name) (fields t) + in + + let ( let* ) = Option.bind in + let* feature : 'a Field.t = feature_opt in + let (T feature_type) = Field.type_ feature in + match feature_type with + | MonoReference t | MultiReference t -> Some t.Typ.t + | Integer | Text -> None diff --git a/lib/model/content.mli b/lib/model/content.mli index ab8d838..e972b83 100644 --- a/lib/model/content.mli +++ b/lib/model/content.mli @@ -16,3 +16,6 @@ val root : 'a t -> 'a t val fields : 'a t -> 'a Field.t Seq.t (** Iterate over the fields in the content *) + +val find_target : 'a t -> string -> 'a option +(** Get the content type pointed by the given field name *) diff --git a/lib/model/field.mli b/lib/model/field.mli index a121871..8e1432a 100644 --- a/lib/model/field.mli +++ b/lib/model/field.mli @@ -19,7 +19,7 @@ val create : 'ref t val name : _ t -> string -(** Name (identifier) for the field*) +(** Name (identifier) for the field *) val label : _ t -> textI18n val placeholder : _ t -> textI18n option diff --git a/lib/model/typ.ml b/lib/model/typ.ml index 2770885..d736be0 100644 --- a/lib/model/typ.ml +++ b/lib/model/typ.ml @@ -12,7 +12,7 @@ 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 + | MultiReference : 't reference -> (int64, 't) t (** Multireferences properties allows only one kind of content. They are not encoded in the content table but in another table *) @@ -28,8 +28,8 @@ let map : type a. ('ref1 -> 'ref2) -> (a, 'ref1) t -> (a, 'ref2) t = let default_value : type a. (a, _) t -> a = function | Integer -> None | Text -> None - | MultiReference _ -> None | MonoReference _ -> None + | MultiReference _ -> raise Not_found let eq : type a b c. (a, c) t -> (b, c) t -> (a, b) Type.eq option = fun t1 t2 -> @@ -56,7 +56,9 @@ Example for inserting data *) type 't value = - | V : ('a option, 't) t * 'a -> 't value + | None (** None tell the field is empty in the database *) + | V : ('a option, 't) t * 'a option -> '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 *) + | Seq : (int64, 't) t * int64 Seq.t -> 't value + (** Get a sequence of values for a multivaluated field *) |
