diff options
author | Marc Coquand <marc@mccd.space> | 2024-01-12 08:01:25 -0600 |
---|---|---|
committer | Marc Coquand <marc@mccd.space> | 2024-01-12 08:01:25 -0600 |
commit | 20c5abba2f762e917503e3d613729f12607baf88 (patch) | |
tree | ab9eb3b897c60949483ea2db12c8f7ff382d7391 /lib | |
parent | 014feebff76f5bb5e711a3897ed930d8e4f009c1 (diff) | |
download | fixgen-20c5abba2f762e917503e3d613729f12607baf88.tar.gz fixgen-20c5abba2f762e917503e3d613729f12607baf88.tar.bz2 fixgen-20c5abba2f762e917503e3d613729f12607baf88.zip |
Implement support for dates
Diffstat (limited to '')
-rw-r--r-- | lib/ast_types.ml | 15 | ||||
-rw-r--r-- | lib/dune | 2 | ||||
-rw-r--r-- | lib/fixture.ml | 17 | ||||
-rw-r--r-- | lib/lexer.mll | 3 | ||||
-rw-r--r-- | lib/parser.mly | 12 |
5 files changed, 48 insertions, 1 deletions
diff --git a/lib/ast_types.ml b/lib/ast_types.ml index 0a4c05d..95baea5 100644 --- a/lib/ast_types.ml +++ b/lib/ast_types.ml @@ -11,6 +11,9 @@ type ast_row = | List of string * string list * ast_row | Username of string * ast_row | Foreign of string * string * string * ast_row + | Past of string * ast_row + | Future of string * ast_row + | Now of string * ast_row (* parent, row, child_name, row *) | End [@@deriving show, eq] @@ -51,6 +54,15 @@ let rec print_row = function | Email (s, r) -> printf "Email(%s)," s; print_row r + | Past (s, r) -> + printf "Past(%s)," s; + print_row r + | Future (s, r) -> + printf "Future(%s)," s; + print_row r + | Now (s, r) -> + printf "Now(%s)," s; + print_row r | End -> printf "\n" @@ -89,6 +101,9 @@ let rec ast_row_to_fixtures = function | 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 + | Past (s, r) -> PColumn (Fixture.Past s) :: ast_row_to_fixtures r + | Future (s, r) -> PColumn (Fixture.Future s) :: ast_row_to_fixtures r + | Now (s, r) -> PColumn (Fixture.Now s) :: ast_row_to_fixtures r | End -> [] let extend_table_with_row table row = @@ -6,6 +6,8 @@ (library (name fixgen) (libraries + ISO8601 + unix uuidm ) (modules parser lexer ast_types fixture) diff --git a/lib/fixture.ml b/lib/fixture.ml index d34b155..47ae453 100644 --- a/lib/fixture.ml +++ b/lib/fixture.ml @@ -107,6 +107,9 @@ type t = | Increment of string | Const of string * string | Email of string + | Past of string + | Future of string + | Now of string | List of string * string list (* (Name, foreign ids to pick from) *) @@ -121,6 +124,16 @@ let generate_fixture index fixture = | Const (_, value) -> String value | Email _ -> String (random_email str_index ()) | Int (_, min, max) -> Int (Random.int (max - min) + min) + | Past _ -> + let now = Unix.time () in + String (ISO8601.Permissive.string_of_datetimezone (Random.float now, 0.)) + | Future _ -> + let now = Unix.time () in + String + (ISO8601.Permissive.string_of_datetimezone (Random.float (Float.mul now 2.), 0.)) + | Now _ -> + let now = Unix.time () in + String (ISO8601.Permissive.string_of_datetimezone (now, 0.)) | List (_, values) -> String (random_value_in_list values ()) @@ -135,7 +148,6 @@ let%test "random_value_in_list" = (* TODO: Support "hashed" password *) -(* TODO: Support dates *) (* TODO: Support variables using @ *) let rec replicate element n = @@ -160,6 +172,9 @@ let id_of_fixture fixture = | Int (id, _, _) -> id | List (id, _) -> id | Email id -> id + | Past id -> id + | Future id -> id + | Now id -> id in String id diff --git a/lib/lexer.mll b/lib/lexer.mll index 1985c2d..1b24847 100644 --- a/lib/lexer.mll +++ b/lib/lexer.mll @@ -22,6 +22,9 @@ rule read = | "name" { NAME } | "increment" { INCREMENT } | "username" { USERNAME } + | "past" { PAST } + | "future" { FUTURE } + | "now" { NOW } | "int" { INTSYMBOL } | id { IDENTIFIER (Lexing.lexeme lexbuf) } | "(" { LBRACE } diff --git a/lib/parser.mly b/lib/parser.mly index 177daaf..f14550e 100644 --- a/lib/parser.mly +++ b/lib/parser.mly @@ -11,6 +11,9 @@ %token COLON %token DOT %token EOF +%token PAST +%token FUTURE +%token NOW %token INTSYMBOL %token <int> INT %token LBRACE @@ -56,6 +59,15 @@ 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; PAST; COMMA; r = row { Past (row_title, r) } + | row_title = id_or_other; PAST { Past (row_title, End) } + + | row_title = id_or_other; FUTURE; COMMA; r = row { Future (row_title, r) } + | row_title = id_or_other; FUTURE { Future (row_title, End) } + + | row_title = id_or_other; NOW; COMMA; r = row { Now (row_title, r) } + | row_title = id_or_other; NOW { Now (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) } |