aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2025-01-07 22:40:19 +0100
committerSébastien Dailly <sebastien@dailly.me>2025-01-22 12:22:26 +0100
commit986bcb796d8b9e1f485baa8da599aa816c56b587 (patch)
tree76a83796a60ec0e30575d3078518c6e2545cd4b0
parent5e15341857e57671a3c617579e3d5dcc89040936 (diff)
Explicitly list all the availables functions
-rw-r--r--lib/configuration/read_conf.ml1
-rw-r--r--lib/errors/importErrors.ml4
-rw-r--r--lib/errors/importErrors.mli1
-rw-r--r--lib/expression/t.ml21
-rw-r--r--lib/expression/t.mli2
-rw-r--r--tests/configuration/simple.toml4
-rw-r--r--tests/configuration_expression.ml19
-rw-r--r--tests/configuration_toml.ml4
8 files changed, 49 insertions, 7 deletions
diff --git a/lib/configuration/read_conf.ml b/lib/configuration/read_conf.ml
index df1a728..52ccb62 100644
--- a/lib/configuration/read_conf.ml
+++ b/lib/configuration/read_conf.ml
@@ -117,6 +117,7 @@ end = struct
Printf.sprintf "Unclosed quote at line %d : \"%s\"" line content
in
Error message
+ | ImportErrors.UnknowFunction _ as e -> Error (ImportErrors.repr_error e)
| e ->
let message = Printexc.to_string e in
Error message
diff --git a/lib/errors/importErrors.ml b/lib/errors/importErrors.ml
index 04f9deb..782291e 100644
--- a/lib/errors/importErrors.ml
+++ b/lib/errors/importErrors.ml
@@ -25,6 +25,7 @@ exception SqlError of Sqlite3.Rc.t
exception MisplacedWindow
exception Unknown_source of string
exception Unknown_extension of string
+exception UnknowFunction of string
exception Cycle of string list
(** Cycle between the dpendencies *)
@@ -53,6 +54,9 @@ let repr_error = function
(ImportDataTypes.Types.string_of_t expected)
expression
| Unknown_extension ext -> Printf.sprintf "Unknown file extension %s" ext
+ | UnknowFunction name ->
+ Printf.sprintf "Unknown function or wrong number of arguments for '%s'"
+ name
| Cycle deps ->
Printf.sprintf "Cycle between the dependencies : %s"
(String.concat ~sep:"," deps)
diff --git a/lib/errors/importErrors.mli b/lib/errors/importErrors.mli
index 7d17a5d..5a2921b 100644
--- a/lib/errors/importErrors.mli
+++ b/lib/errors/importErrors.mli
@@ -11,6 +11,7 @@ exception Cycle of string list
(** Cycle between the dpendencies *)
exception Unknown_extension of string
+exception UnknowFunction of string
exception
JsonError of {
diff --git a/lib/expression/t.ml b/lib/expression/t.ml
index 80cff4f..9ce21b8 100644
--- a/lib/expression/t.ml
+++ b/lib/expression/t.ml
@@ -38,12 +38,13 @@ and funct =
| Trim
| Upper
-let name_of_function = function
+let name_of_function : funct -> string = function
| Upper -> "UPPER"
| Trim -> "TRIM"
| Cmp -> "CMP"
-let function_of_name param f =
+let function_of_name : 'a t list -> string -> 'a t =
+ fun param f ->
match (String.lowercase_ascii f, param) with
| "nvl", _ -> Nvl param
| "join", Literal sep :: tl -> Join (sep, tl)
@@ -51,7 +52,21 @@ let function_of_name param f =
| "upper", _ -> Function' (Upper, param)
| "trim", _ -> Function' (Trim, param)
| "cmp", _ -> Function' (Cmp, param)
- | other, _ -> Function (other, param)
+ (* Branch function *)
+ | ("if" as fn), [ _; _; _ ] -> Function (fn, param)
+ (* Integer functions *)
+ | ("abs" as fn), _
+ | ("int" as fn), _
+ (* String functions *)
+ | ("concat" as fn), _
+ | ("match" as fn), _
+ | ("substring" as fn), _
+ (* Date functions *)
+ | ("date" as fn), _
+ | ("year" as fn), _ -> Function (fn, param)
+ | _ ->
+ (*Function (other, param)*)
+ raise (ImportErrors.UnknowFunction f)
let name_of_operator = function
| Equal -> "="
diff --git a/lib/expression/t.mli b/lib/expression/t.mli
index 4b13ac6..49ef3e7 100644
--- a/lib/expression/t.mli
+++ b/lib/expression/t.mli
@@ -53,4 +53,6 @@ val name_of_window : 'a window -> string
val map_window : f:('a -> 'b) -> 'a window -> 'b window
val window_of_name : string -> 'a option -> 'a window
val name_of_function : funct -> string
+
val function_of_name : 'a t list -> string -> 'a t
+(** Create a function with the given name *)
diff --git a/tests/configuration/simple.toml b/tests/configuration/simple.toml
index d41383a..9ca591a 100644
--- a/tests/configuration/simple.toml
+++ b/tests/configuration/simple.toml
@@ -8,13 +8,13 @@ tab = 1
[externals.target]
extern_key = "\"_B\""
# Here, the values A & B are considered as column, and not litteral
-intern_key = "function(:A, :B)"
+intern_key = "concat(:A, :B)"
allow_missing = true
file = ""
tab = 1
[sheet]
columns = [
- "function(:target.A, :B, 'free\\' text')",
+ "if(:target.A, :B, 'free\\' text')",
"counter([:target.A],[:target.A])"
]
diff --git a/tests/configuration_expression.ml b/tests/configuration_expression.ml
index a5c4755..fc4c0ec 100644
--- a/tests/configuration_expression.ml
+++ b/tests/configuration_expression.ml
@@ -224,6 +224,23 @@ let priority_operator_or =
(Or, BOperator (Different, Integer "1", Integer "1"), Integer "0")))
result
+let unknown_function =
+ "unknown function" >:: fun _ ->
+ let expr = "function()" in
+ let result = ImportConf.expression_from_string expr in
+
+ assert_equal ~printer
+ (Error "Unknown function or wrong number of arguments for 'function'")
+ result
+
+let wrong_arguments =
+ "unknown function" >:: fun _ ->
+ let expr = "if()" in
+ let result = ImportConf.expression_from_string expr in
+
+ assert_equal ~printer
+ (Error "Unknown function or wrong number of arguments for 'if'") result
+
let test_suit =
[
parse_dquoted;
@@ -248,6 +265,8 @@ let test_suit =
priority_equality;
priority_operator_and;
priority_operator_or;
+ unknown_function;
+ wrong_arguments;
]
let tests = "configuration_expression" >::: test_suit
diff --git a/tests/configuration_toml.ml b/tests/configuration_toml.ml
index e58ff1b..67f8d75 100644
--- a/tests/configuration_toml.ml
+++ b/tests/configuration_toml.ml
@@ -17,7 +17,7 @@ let test_suit =
extern_key = Literal "_B";
intern_key =
Function
- ( "function",
+ ( "concat",
[
Path { alias = None; column = 1 };
Path { alias = None; column = 2 };
@@ -44,7 +44,7 @@ let test_suit =
let expected =
[
Function
- ( "function",
+ ( "if",
[
Path { alias = Some "target"; column = 1 };
Path { alias = None; column = 2 };