diff options
Diffstat (limited to 'lib/model/typ.ml')
| -rw-r--r-- | lib/model/typ.ml | 62 |
1 files changed, 62 insertions, 0 deletions
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 *) |
