package sihl

  1. Overview
  2. Docs

Source file gen_entity.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
let template =
  {|
type t =
  { id : string
  {{entity_type}}
  ; created_at : Ptime.t
  ; updated_at : Ptime.t
  }
[@@deriving show]

let create {{create_args}} =
  let now = Ptime_clock.now () in
  let id = Uuidm.v `V4 |> Uuidm.to_string in
  { id; {{created_value}} created_at = now; updated_at = now }
;;

let[@warning "-45"] schema
    : (unit, {{ctor_type}} -> t, t) Conformist.t
  =
  Conformist.(
    make
      Field.[
        {{conformist_fields}}
        ]
      create)
;;
|}
;;

let entity_type (schema : Gen_core.schema) =
  schema
  |> List.map (fun (name, type_) ->
       Format.sprintf "%s: %s" name (Gen_core.ocaml_type_of_gen_type type_))
  |> String.concat ";"
  |> Format.sprintf ";%s"
;;

let ctor_type (schema : Gen_core.schema) =
  schema
  |> List.map snd
  |> List.map Gen_core.ocaml_type_of_gen_type
  |> String.concat " -> "
;;

let create_args (schema : Gen_core.schema) =
  schema |> List.map fst |> String.concat " "
;;

let created_value (schema : Gen_core.schema) =
  schema |> List.map fst |> List.map (Format.sprintf "%s;") |> String.concat " "
;;

let conformist_fields (schema : Gen_core.schema) =
  schema
  |> List.map (fun (name, type_) ->
       Format.sprintf
         {|%s "%s"|}
         (Gen_core.conformist_type_of_gen_type type_)
         name)
  |> String.concat "; "
;;

let file (schema : Gen_core.schema) =
  let params =
    [ "entity_type", entity_type schema
    ; "create_args", create_args schema
    ; "created_value", created_value schema
    ; "ctor_type", ctor_type schema
    ; "conformist_fields", conformist_fields schema
    ]
  in
  Gen_core.{ name = "entity.ml"; template; params }
;;
OCaml

Innovation. Community. Security.