aboutsummaryrefslogtreecommitdiff
path: root/lib/analysers/headers.ml
blob: 916dfeed01cd05836807564e18704e012513faf3 (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
open StdLabels
module I = ImportConf
module E = ImportExpression.T
module Syntax = ImportConf.Syntax
module Table = ImportDataTypes.Table
module Path = ImportDataTypes.Path

module SheeetMap = Map.Make (struct
  type t = Table.t

  (** We are sure we can’t have the same name for two different table. *)
  let compare v1 v2 = String.compare (Table.name v1) (Table.name v2)
end)

type content = string array

type t = content SheeetMap.t
(** The map associate a line of headers for each table. 

    The header are always in string. *)

(** Get the headers. The function has to be called after reading each document,
    and will reformat the first line with the values from the cell. The
    functions will not be evaluated (instead they will be displayed "as is".

    When there is no value for this path, return empty string.
 *)
let columns : Syntax.t -> t -> string list =
 fun conf t ->
  (* We build here a custom printer which search in the array for the column
     name.

     This function will be given as argument in the expression printer. *)
  let f : Path.t -> Buffer.t -> unit =
   fun path b ->
    let source = I.get_table_for_name conf path.alias in

    match SheeetMap.find_opt source t with
    | None -> ()
    | Some arr -> (
        try Buffer.add_string b (Array.get arr (path.column - 1)) with
        | _ ->
            prerr_endline
            @@ Printf.sprintf "No header found for :%s.%s"
                 (Option.value ~default:(I.root_table conf).Table.name
                    path.alias)
                 (ImportCSV.Csv.column_to_string path.column))
  in

  List.map conf.Syntax.columns ~f:(fun column ->
      let b = Buffer.create 4 in

      ImportExpression.Headers.headers_of_expression b f column;

      Buffer.contents b)