aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration/syntax.ml
blob: 8efdc59d385ddbcf2a99c0a462337eee6e3986bf (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
open StdLabels
module E = ImportExpression.T
module Table = ImportDataTypes.Table
module Path = ImportDataTypes.Path

let toml_of_table Table.{ file; tab; name } =
  let values = [ ("file", Otoml.string file); ("name", Otoml.string name) ] in
  let values =
    match tab with
    | 1 -> values
    | tab -> ("tab", Otoml.integer tab) :: values
  in

  Otoml.table values

type extern = {
  intern_key : Path.t E.t;
  target : Table.t;
  extern_key : Path.column E.t;
  allow_missing : bool;
  match_rule : string option;
}

let toml_of_extern extern =
  let values =
    [
      ( "intern_key",
        Otoml.string
        @@ ImportExpression.Repr.repr ~top:true Path.repr extern.intern_key );
      ( "extern_key",
        Otoml.string
        @@ ImportExpression.Repr.repr ~top:true
             (fun v -> ":" ^ ImportCSV.Csv.column_to_string v)
             extern.extern_key );
      ("file", Otoml.string extern.target.file);
      ("allow_missing", Otoml.boolean extern.allow_missing);
    ]
  in

  let values =
    match extern.target.tab with
    | 1 -> values
    | tab -> ("tab", Otoml.integer tab) :: values
  in

  Otoml.table values

let toml_of_externs externs =
  List.map externs ~f:(fun e -> (e.target.name, toml_of_extern e))
  |> Otoml.table

type t = {
  version : int;
  source : Table.t;
  externals : extern list;
  columns : Path.t E.t list;
  filters : Path.t E.t list;
  sort : Path.t E.t list;
  uniq : Path.t E.t list;
}

let repr t =
  let repr_expression_list l =
    Otoml.array
      (List.map l ~f:(fun v ->
           Otoml.string (ImportExpression.Repr.repr ~top:true Path.repr v)))
  in

  let sheet =
    Otoml.table
      [
        ("columns", repr_expression_list t.columns);
        ("filters", repr_expression_list t.filters);
        ("sort", repr_expression_list t.sort);
        ("uniq", repr_expression_list t.uniq);
      ]
  in

  let values =
    [
      ("version", Otoml.integer t.version);
      ("source", toml_of_table t.source);
      ("externals", toml_of_externs t.externals);
      ("sheet", sheet);
    ]
  in

  Otoml.table values