Skip to content

Commit e02c79b

Browse files
Allow leading comma in redefine constraints (#447)
## Product change and motivation Mirrors the leading-comma sugar already accepted by `define`: `define person, owns name;` parses, but `redefine person, owns name @card(0..10);` did not. Make `redefinable_type` accept an optional COMMA between the label and the constraint so concat-based query generation has the same shape for define and redefine. ## Implementation
1 parent 7029e9f commit e02c79b

5 files changed

Lines changed: 60 additions & 1 deletion

File tree

rust/parser/test/fetch.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,14 @@ fetch {
7676
// .fetch(projections);
7777
assert_valid_eq_repr!(expected, parsed, query);
7878
}
79+
80+
#[test]
81+
fn fetch_with_commas_is_accepted() {
82+
let query = r#"match
83+
$x isa person;
84+
fetch {
85+
"name": $x.name,
86+
"age": $x.age,
87+
};"#;
88+
parse_query(query).unwrap();
89+
}

rust/parser/test/match_queries.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77
use super::assert_valid_eq_repr;
88
use crate::parse_query;
99

10+
#[test]
11+
fn match_with_commas_is_accepted() {
12+
let query = r#"match
13+
$x isa person, has name $n, has age $a,;
14+
($a, $b,) isa friendship;
15+
let $list = [$a, $b,];
16+
let $f = max($a, $b,);
17+
let $u, $v, in [$a, $b,];
18+
select $x, $n, $a,;
19+
sort $a asc, $n desc,;
20+
require $x, $n,;
21+
reduce $count = count, $total = sum($a),;"#;
22+
parse_query(query).unwrap();
23+
}
24+
1025
#[test]
1126
fn test_simple_query() {
1227
let query = r#"match

rust/parser/test/schema_queries.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,28 @@ fn undefine_attribute_type_regex() {
167167
// let expected = undefine!(type_("digit").regex(r"\d"));
168168
assert_valid_eq_repr!(expected, parsed, query);
169169
}
170+
171+
#[test]
172+
fn redefine_with_leading_comma_is_accepted() {
173+
let with_comma = r#"redefine
174+
person, owns name @card(0..10);
175+
entity person, owns email @card(0..1);"#;
176+
parse_query(with_comma).unwrap();
177+
178+
let without_comma = r#"redefine
179+
person owns name @card(0..10);
180+
entity person owns email @card(0..1);"#;
181+
parse_query(without_comma).unwrap();
182+
}
183+
184+
#[test]
185+
fn define_with_commas_is_accepted() {
186+
let query = r#"define
187+
entity person,
188+
owns name,
189+
owns age,;
190+
struct point: x value double, y value double,;
191+
fun greet($p: person, $g: string,) -> string:
192+
match $p has name $n; return first $n;"#;
193+
parse_query(query).unwrap();
194+
}

rust/parser/test/write_queries.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ $x has age 25;"#;
6060
assert_valid_eq_repr!(expected, parsed, query);
6161
}
6262

63+
#[test]
64+
fn insert_with_commas_is_accepted() {
65+
let query = r#"insert
66+
$x isa person, has name "alice", has age 30,;
67+
(parent: $p, child: $c,) isa family;"#;
68+
parse_query(query).unwrap();
69+
}
70+
6371
#[test]
6472
fn test_match_insert_query() {
6573
let query = r#"match

rust/parser/typeql.pest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ struct_destructor_value = { var | struct_destructor }
258258

259259
query_redefine = { REDEFINE ~ ( redefinable )* }
260260
redefinable = { redefinable_type | definition_function }
261-
redefinable_type = { kind? ~ label ~ ( annotations | type_capability ) ~ SEMICOLON }
261+
redefinable_type = { kind? ~ label ~ COMMA? ~ ( annotations | type_capability ) ~ SEMICOLON }
262262
// TODO: add `redefine <person> label <new_person_label>
263263

264264
// UNDEFINE QUERY ==============================================================

0 commit comments

Comments
 (0)