From 5994b98069cec82f0b18a6a1629cc9973fc9e0bf Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Tue, 26 Dec 2023 16:22:42 -0600 Subject: Add support for email --- README.md | 8 ++++++++ lib/ast_types.ml | 5 +++++ lib/fixture.ml | 38 ++++++++++++++++++++++++++++++++++++++ lib/lexer.mll | 1 + lib/parser.mly | 5 +++++ test/test_parser.ml | 1 + 6 files changed, 58 insertions(+) diff --git a/README.md b/README.md index a7e094f..b01d77d 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,14 @@ MXN MXN ``` +- email + +```sh +$ fixgen 'user (1): email email' +email +judy@gmail.com +``` + - increment ```sh diff --git a/lib/ast_types.ml b/lib/ast_types.ml index fdcafd2..0cbdb4f 100644 --- a/lib/ast_types.ml +++ b/lib/ast_types.ml @@ -6,6 +6,7 @@ type ast_row = | Name of string * ast_row | Int of string * int * int * ast_row | Increment of string * ast_row + | Email of string * ast_row | Const of string * string * ast_row | List of string * string list * ast_row | Foreign of string * string * string * ast_row @@ -43,6 +44,9 @@ let rec print_row = function | Name (s, r) -> printf "Name(%s)," s; print_row r + | Email (s, r) -> + printf "Email(%s)," s; + print_row r | End -> printf "\n" @@ -79,6 +83,7 @@ let rec ast_row_to_fixtures = function | Const (s, v, r) -> PColumn (Fixture.Const (s, v)) :: ast_row_to_fixtures r | List (s, l, r) -> PColumn (Fixture.List (s, l)) :: ast_row_to_fixtures r | Increment (s, r) -> PColumn (Fixture.Increment s) :: ast_row_to_fixtures r + | Email (s, r) -> PColumn (Fixture.Email s) :: ast_row_to_fixtures r | End -> [] let extend_table_with_row table row = diff --git a/lib/fixture.ml b/lib/fixture.ml index 4985624..dbd6297 100644 --- a/lib/fixture.ml +++ b/lib/fixture.ml @@ -19,6 +19,37 @@ let names = ] +let email_handles = + [ "alice" + ; "bob" + ; "charlie" + ; "dave" + ; "eve" + ; "frank" + ; "grace" + ; "heidi" + ; "ivan" + ; "judy" + ; "mallory" + ; "oscar" + ; "peggy" + ; "sybil" + ; "trent" + ; "victor" + ; "walter" + ] + + +let email_hosts = + [ "gmail.com" + ; "yahoo.com" + ; "hotmail.com" + ; "fastmail.fr" + ; "mccd.space" + ; "protonmail.com" + ] + + let random_value_in_list values = List.nth values (Random.int (List.length values)) type compiled = @@ -36,6 +67,10 @@ let show_compiled_csv fixture = | Float f -> string_of_float f +let random_email () = + random_value_in_list email_handles ^ "@" ^ random_value_in_list email_hosts + + let random_name () = List.nth names (Random.int (List.length names)) let%test "random_name" = List.mem (random_name ()) names @@ -62,6 +97,7 @@ type t = | Int of string * int * int | Increment of string | Const of string * string + | Email of string | List of string * string list (* (Name, foreign ids to pick from) *) @@ -72,6 +108,7 @@ let generate_fixture index fixture = | Foreign (_, reference) -> random_value_in_list reference | Increment _ -> Int (index + 1) | Const (_, value) -> String value + | Email _ -> String (random_email ()) | Int (_, min, max) -> Int (Random.int (max - min) + min) | List (_, values) -> String (random_value_in_list values) @@ -111,6 +148,7 @@ let id_of_fixture fixture = | Const (id, _) -> id | Int (id, _, _) -> id | List (id, _) -> id + | Email id -> id in String id diff --git a/lib/lexer.mll b/lib/lexer.mll index 0fb7f7c..ad3be3c 100644 --- a/lib/lexer.mll +++ b/lib/lexer.mll @@ -18,6 +18,7 @@ let many_newline = newline+ rule read = parse | "uuidv4" { UUIDV4 } + | "email" { EMAIL } | "name" { NAME } | "increment" { INCREMENT } | "int" { INTSYMBOL } diff --git a/lib/parser.mly b/lib/parser.mly index 6172963..e6bcb6d 100644 --- a/lib/parser.mly +++ b/lib/parser.mly @@ -15,6 +15,7 @@ %token INT %token LBRACE %token RBRACE +%token EMAIL %token STRING %start prog %% @@ -28,6 +29,7 @@ id_or_other: | NAME { "name" } | UUIDV4 { "uuidv4" } | INCREMENT { "increment" } + | EMAIL { "email" } expr: @@ -49,6 +51,9 @@ row: | row_title = id_or_other; NAME; COMMA; r = row { Name (row_title, r) } | row_title = id_or_other; NAME { Name (row_title, End) } + | row_title = id_or_other; EMAIL; COMMA; r = row { Email (row_title, r) } + | row_title = id_or_other; EMAIL { Email (row_title, End) } + | row_title = id_or_other; INCREMENT; COMMA; r = row { Increment (row_title, r) } | row_title = id_or_other; INCREMENT { Increment (row_title, End) } diff --git a/test/test_parser.ml b/test/test_parser.ml index db18540..41fea3d 100644 --- a/test/test_parser.ml +++ b/test/test_parser.ml @@ -28,6 +28,7 @@ let rows_suite = , test_parse "purchases (5): name int(1,20)" ) ; "Supports underscore tables", `Quick, test_parse "purchases_new (5): name int(1,20)" ; "Supports incremental index", `Quick, test_parse "purchases_new (5): id increment" + ; "Supports email", `Quick, test_parse "user (2): email email" ] -- cgit v1.2.3