From 664b34059624d525d38da3e9e452a4bfc190a585 Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Tue, 26 Dec 2023 18:26:40 -0600 Subject: Add support for username --- lib/ast_types.ml | 5 +++++ lib/fixture.ml | 17 ++++++++++++----- lib/lexer.mll | 1 + lib/parser.mly | 5 +++++ 4 files changed, 23 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/ast_types.ml b/lib/ast_types.ml index 0cbdb4f..0a4c05d 100644 --- a/lib/ast_types.ml +++ b/lib/ast_types.ml @@ -9,6 +9,7 @@ type ast_row = | Email of string * ast_row | Const of string * string * ast_row | List of string * string list * ast_row + | Username of string * ast_row | Foreign of string * string * string * ast_row (* parent, row, child_name, row *) | End @@ -26,6 +27,9 @@ let rec print_row = function | Uuidv4 (s, r) -> printf "UUIDv4(%s)," s; print_row r + | Username (s, r) -> + printf "User(%s)," s; + print_row r | List (s, l, r) -> printf "List(%s, %s)," s (String.concat ", " l); print_row r @@ -84,6 +88,7 @@ let rec ast_row_to_fixtures = function | 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 + | Username (s, r) -> PColumn (Fixture.Username 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 18e2ff6..d127405 100644 --- a/lib/fixture.ml +++ b/lib/fixture.ml @@ -50,7 +50,7 @@ let email_hosts = ] -let random_value_in_list values = List.nth values (Random.int (List.length values)) +let random_value_in_list values () = List.nth values (Random.int (List.length values)) type compiled = | Int of int @@ -68,7 +68,10 @@ let show_compiled_csv fixture = let random_email index () = - random_value_in_list email_handles ^ index ^ "@" ^ random_value_in_list email_hosts + random_value_in_list email_handles () + ^ index + ^ "@" + ^ random_value_in_list email_hosts () let random_name () = List.nth names (Random.int (List.length names)) @@ -92,6 +95,7 @@ let key_of_compiled key = type t = | Name of string + | Username of string | Uuidv4 of string | Foreign of string * compiled list | Int of string * int * int @@ -105,13 +109,14 @@ let generate_fixture index fixture = let str_index = string_of_int index in match fixture with | Name _ -> String (random_name () ^ str_index) + | Username _ -> String (random_value_in_list email_handles () ^ str_index) | Uuidv4 _ -> String (Uuidm.v `V4 |> Uuidm.to_string) - | Foreign (_, reference) -> random_value_in_list reference + | Foreign (_, reference) -> random_value_in_list reference () | Increment _ -> Int (index + 1) | Const (_, value) -> String value | Email _ -> String (random_email str_index ()) | Int (_, min, max) -> Int (Random.int (max - min) + min) - | List (_, values) -> String (random_value_in_list values) + | List (_, values) -> String (random_value_in_list values ()) let add_name name fixtures = Name name :: fixtures @@ -120,13 +125,14 @@ let add_foreign id values fixtures = Foreign (id, values) :: fixtures let%test "random_value_in_list" = let values = [ "a"; "b"; "c" ] in - let result = random_value_in_list values in + let result = random_value_in_list values () in List.mem result values (* TODO: Support "hashed" password *) (* TODO: Support dates *) (* TODO: Support variables using @ *) +(* TODO: Support username *) let rec replicate element n = match n with @@ -143,6 +149,7 @@ let id_of_fixture fixture = match fixture with | Name id -> id | Uuidv4 id -> id + | Username id -> id | Increment id -> id | Foreign (id, _) -> id | Const (id, _) -> id diff --git a/lib/lexer.mll b/lib/lexer.mll index ad3be3c..1985c2d 100644 --- a/lib/lexer.mll +++ b/lib/lexer.mll @@ -21,6 +21,7 @@ rule read = | "email" { EMAIL } | "name" { NAME } | "increment" { INCREMENT } + | "username" { USERNAME } | "int" { INTSYMBOL } | id { IDENTIFIER (Lexing.lexeme lexbuf) } | "(" { LBRACE } diff --git a/lib/parser.mly b/lib/parser.mly index e6bcb6d..177daaf 100644 --- a/lib/parser.mly +++ b/lib/parser.mly @@ -17,6 +17,7 @@ %token RBRACE %token EMAIL %token STRING +%token USERNAME %start prog %% @@ -30,6 +31,7 @@ id_or_other: | UUIDV4 { "uuidv4" } | INCREMENT { "increment" } | EMAIL { "email" } + | USERNAME { "username" } expr: @@ -54,6 +56,9 @@ row: | 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; USERNAME; COMMA; r = row { Username (row_title, r) } + | row_title = id_or_other; USERNAME { Username (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) } -- cgit v1.2.3