From 2a664a47c73e232b6ff0d261833e2561d4abc4d0 Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Tue, 26 Dec 2023 10:46:11 -0600 Subject: Support list --- lib/ast_types.ml | 7 +++++++ lib/fixture.ml | 6 ++++-- lib/parser.mly | 7 +++++++ 3 files changed, 18 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/ast_types.ml b/lib/ast_types.ml index c278d59..c192c60 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 | Const of string * string * ast_row + | List of string * string list * ast_row | Foreign of string * string * string * ast_row (* parent, row, child_name, row *) | End @@ -15,11 +16,16 @@ type ast_table = | Table of string * int * ast_row * ast_table | End [@@deriving show, eq] +(** In hindsight, this could've been represented as a list of tables instead using separated list command + TODO: Rewrite this to use a list of tables instead of a tree *) let rec print_row = function | Uuidv4 (s, r) -> printf "UUIDv4(%s)," s; print_row r + | List (s, l, r) -> + printf "List(%s, %s)," s (String.concat ", " l); + print_row r | Const (s, v, r) -> printf "Const(%s, %s)," s v; print_row r @@ -65,6 +71,7 @@ let rec ast_row_to_fixtures = function | Name (s, r) -> PRow (Fixture.Name s) :: ast_row_to_fixtures r | Int (s, min, max, r) -> PRow (Fixture.Int (s, min, max)) :: ast_row_to_fixtures r | Const (s, v, r) -> PRow (Fixture.Const (s, v)) :: ast_row_to_fixtures r + | List (s, l, r) -> PRow (Fixture.List (s, l)) :: ast_row_to_fixtures r | End -> [] diff --git a/lib/fixture.ml b/lib/fixture.ml index 2b17a02..d5c77fa 100644 --- a/lib/fixture.ml +++ b/lib/fixture.ml @@ -25,9 +25,10 @@ let%test "random_name" = List.mem (random_name ()) names type t = | Name of string | Uuidv4 of string - | Foreign of (string * string list) + | Foreign of string * string list | Int of string * int * int | Const of string * string + | List of string * string list (* (Name, foreign ids to pick from) *) let add_name name fixtures = Name name :: fixtures @@ -48,6 +49,7 @@ let generate_fixture fixture = | Foreign (_, reference) -> random_value_in_list reference | Const (_, value) -> value | Int (_, min, max) -> Random.int (max - min) + min |> string_of_int + | List (_, values) -> random_value_in_list values let%test "generate_uuid" = @@ -61,10 +63,10 @@ let id_of_fixture fixture = | Foreign (id, _) -> id | Const (id, _) -> id | Int (id, _, _) -> id + | List (id, _) -> id (* TODO: Support email *) -(* TODO: Support list *) let rec replicate element n = match n with diff --git a/lib/parser.mly b/lib/parser.mly index 6e21480..8a666c6 100644 --- a/lib/parser.mly +++ b/lib/parser.mly @@ -35,6 +35,9 @@ row: | row_title = IDENTIFIER; UUIDV4; COMMA; r = row { Uuidv4 (row_title, r) } | row_title = IDENTIFIER; UUIDV4 { Uuidv4 (row_title, End) } + | row_title = IDENTIFIER; LBRACE; union = union_fields; RBRACE; COMMA; r = row { List (row_title, union, r) } + | row_title = IDENTIFIER; LBRACE; union = union_fields; RBRACE { List (row_title, union, End) } + | row_title = IDENTIFIER; NAME; COMMA; r = row { Name (row_title, r) } | row_title = IDENTIFIER; NAME { Name (row_title, End) } @@ -44,3 +47,7 @@ row: | row_title = IDENTIFIER; INTSYMBOL;LBRACE;min = INT;COMMA;max = INT;RBRACE;COMMA; r = row { Int (row_title,min,max,r) } | row_title = IDENTIFIER; INTSYMBOL;LBRACE;min = INT;COMMA;max = INT;RBRACE; { Int (row_title,min,max,End) } ; + +union_fields: + vl = separated_list(COMMA, STRING) { vl } + ; -- cgit v1.2.3