diff options
Diffstat (limited to '')
-rw-r--r-- | bin/main.ml | 27 | ||||
-rw-r--r-- | lib/fixture.ml | 20 |
2 files changed, 36 insertions, 11 deletions
diff --git a/bin/main.ml b/bin/main.ml index 29f354f..d644133 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -9,17 +9,20 @@ let parse (s : string) = let fixgen content output_type () = let result = parse content in - if output_type != "csv" - then print_endline "Only csv output is supported" - else ( - match result with - | Some ast -> - Ast_types.compile ast - |> List.iter (fun (name, file) -> - let result = Fixture.csv_of_generated_fixtures file in - print_endline ("FILE NAME: " ^ name); - print_endline result) - | None -> print_endline "error") + let generate_output = + match output_type with + | "csv" -> Fixture.csv_of_generated_fixtures + | "json" -> Fixture.json_of_generated_fixtures + | _ -> failwith "Unsupported output type, supported types are: json, csv" + in + match result with + | Some ast -> + Ast_types.compile ast + |> List.iter (fun (name, file) -> + let result = generate_output file in + print_endline ("FILE NAME: " ^ name); + print_endline result) + | None -> print_endline "error" let file = @@ -27,6 +30,8 @@ let file = Arg.(required & pos 0 (some string) None & info [] ~docv:"PROGRAM" ~doc) +(* TODO: Support option to set output folder *) + let output_type = let doc = "Set output file type, supported=csv" in Arg.(value & opt string "csv" & info [ "f"; "file-type" ] ~docv:"FILE TYPE" ~doc) diff --git a/lib/fixture.ml b/lib/fixture.ml index d5c77fa..d28d4b3 100644 --- a/lib/fixture.ml +++ b/lib/fixture.ml @@ -67,6 +67,10 @@ let id_of_fixture fixture = (* TODO: Support email *) +(* TODO: Support arbitrary string *) +(* TODO: Support "hashed" password *) +(* TODO: Support variables using @ *) +(* TODO: Support JSON export *) let rec replicate element n = match n with @@ -118,3 +122,19 @@ let csv_of_generated_fixtures fixtures = let%test "csv_of_generated_fixtures" = let result = [ [ "id"; "name" ]; [ "1234"; "John" ] ] |> csv_of_generated_fixtures in result = "id,name\n1234,John" + + +let json_of_generated_fixtures fixtures = + let headers = List.hd fixtures in + let rows = List.tl fixtures in + let json_of_row row = + let pairs = List.combine headers row in + let json_of_pair (key, value) = "\"" ^ key ^ "\": \"" ^ value ^ "\"" in + "{" ^ String.concat ", " (List.map json_of_pair pairs) ^ "}" + in + "[" ^ String.concat ", " (List.map json_of_row rows) ^ "]" + + +let%test "json_of_generated_fixtures" = + let result = [ [ "id"; "name" ]; [ "1234"; "John" ] ] |> json_of_generated_fixtures in + result = "[{\"id\": \"1234\", \"name\": \"John\"}]" |