From 3948ef84deeef3d131d1cb503ae93be1a93631fe Mon Sep 17 00:00:00 2001
From: Bruno Guillaume <Bruno.Guillaume@loria.fr>
Date: Thu, 27 Oct 2022 10:40:49 +0200
Subject: [PATCH] review "load_grs"

---
 src_ocaml/grewpy.ml | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/src_ocaml/grewpy.ml b/src_ocaml/grewpy.ml
index f4e4ab8..ebc4a8a 100644
--- a/src_ocaml/grewpy.ml
+++ b/src_ocaml/grewpy.ml
@@ -74,14 +74,28 @@ let run_command request =
     (* ======================= load_grs ======================= *)
     | Some "load_grs" ->
       begin
-        match json |> member "filename" |> to_string_option with
-        | Some filename ->
+        let dict = json |> to_assoc in
+        match List.assoc_opt "filename" dict with
+        | Some `String filename -> 
           let grs = Grs.load ~config filename in
           let index = Global.grs_add grs in
-          (* let json = Grs.to_json grs in *)
           let data = `Assoc [("index", `Int index)] in
           Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", data)])
-        | None -> json_error "no 'filename' in 'load_grs' command"
+        | _ -> 
+          match List.assoc_opt "str" dict with
+          | Some `String str ->  
+            let grs = Grs.parse ~config str in
+            let index = Global.grs_add grs in
+            let data = `Assoc [("index", `Int index)] in
+            Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", data)])
+          | _ -> 
+            match List.assoc_opt "json" dict with
+            | Some data ->
+              let grs = json |> member "json" |> Grs.of_json ~config in
+              let index = Global.grs_add grs in
+              let data = `Assoc [("index", `Int index)] in
+              Yojson.Basic.to_string (`Assoc [("status", `String "OK"); ("data", data)])
+          | _ -> json_error "fail in 'load_grs' command"
       end
 
     (* ======================= load_corpus ======================= *)
-- 
GitLab