aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration/importConf.ml
blob: 586be3cc28487c8b6be6dcf22668cf1e458d41d8 (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
89
90
open StdLabels
module Syntax = Syntax
module Table = ImportDataTypes.Table
module Path = ImportDataTypes.Path
module T = Read_conf
module Expression = ImportExpression.T

let current_syntax = 1

let t_of_yojson : Yojson.Safe.t -> Syntax.t =
 fun json ->
  let keys = Yojson.Safe.Util.keys json in
  let version =
    match List.find_opt keys ~f:(String.equal "version") with
    | None ->
        Printf.printf
          "No version given. Your setup may break in the future.\n\
           Please add « \"version\":%d » in your configuration.\n\n"
          current_syntax;
        `Int 1
    | Some _ -> Yojson.Safe.Util.member "version" json
  in

  match version with
  | `Int 1 -> Of_json.t_of_yojson json
  | other ->
      Printf.eprintf "Unsuported version : %s\n" (Yojson.Safe.to_string other);
      exit 1

module TomlReader = Read_conf.Make (Helpers.Toml.Decode)

let t_of_toml : Otoml.t -> (Syntax.t, string) result =
 fun toml ->
  let version =
    Otoml.find toml (Otoml.get_integer ~strict:false) [ "version" ]
  in
  match version with
  | 1 -> TomlReader.read toml
  | _ ->
      Printf.eprintf "Unsuported version : %d\n" version;
      exit 1

let dummy_conf =
  Syntax.
    {
      source = { file = ""; tab = 0; name = "" };
      version = 1;
      externals = [];
      columns = [];
      filters = [];
      sort = [];
      uniq = [];
    }

let get_table_for_name : Syntax.t -> string option -> Table.t =
 fun conf name ->
  match name with
  | None -> conf.source
  | Some name ->
      if String.equal name conf.source.name then conf.source
      else
        let ext =
          List.find conf.externals ~f:(fun (ext : Syntax.extern) ->
              String.equal name ext.target.name)
        in
        ext.target

let root_table : Syntax.t -> Table.t = fun conf -> conf.source

let get_dependancies_for_table : Syntax.t -> Table.t -> Syntax.extern list =
 fun conf source ->
  let is_root = source = conf.source in

  List.filter conf.externals ~f:(fun (ext : Syntax.extern) ->
      (* Enumerate the intern_key and check the source pointed by each column *)
      Expression.fold_values ext.intern_key ~init:false ~f:(fun acc expr ->
          if acc then acc
          else
            match expr.Syntax.Path.alias with
            | Some v -> String.equal v source.name
            | None -> is_root))

let print_path_expression t = ImportExpression.Repr.repr Path.repr t

let print_extern t =
  let toml = Syntax.toml_of_extern t in
  Otoml.Printer.to_string toml

let expression_from_string s =
  Read_conf.ExpressionParser.of_string Read_conf.ExpressionParser.path s