Skip to content

Commit

Permalink
Text format: allow empty list syntax in fields (#143)
Browse files Browse the repository at this point in the history
* Add tests for parsing empty repeated message lists

* Support parsing empty repeated value lists
  • Loading branch information
rossng authored Feb 6, 2025
1 parent a6c81bf commit 55a5e14
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion prost-reflect-tests/src/text_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ fn parse_aliased_enum() {

#[test]
fn parse_array() {
let value: ScalarArrays = from_text("double: [1.1, 2f] , float: 3 ; float: inf");
let value: ScalarArrays = from_text("double: [1.1, 2f] , float: 3 ; float: inf ; int32: [ ]");
assert_eq!(
value,
ScalarArrays {
Expand Down Expand Up @@ -557,6 +557,7 @@ fn parse_complex_type() {
my_enum: [DEFAULT, FOO]
my_enum: [2, BAR]
my_enum: NEG
my_enum: []
optional_enum: FOO
",
);
Expand Down
11 changes: 10 additions & 1 deletion prost-reflect/src/dynamic/text_format/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,16 @@ impl<'a> Parser<'a> {
Some((Token::LeftBracket, _)) => {
let start = self.bump();

let mut result = vec![self.parse_value(kind)?.0];
let mut result = Vec::new();

// Check for empty list first
if let Some((Token::RightBracket, _)) = self.peek()? {
let end = self.bump();
return Ok((Value::List(result), join_span(start, end)));
}

result.push(self.parse_value(kind)?.0);

loop {
match self.peek()? {
Some((Token::Comma, _)) => {
Expand Down

0 comments on commit 55a5e14

Please sign in to comment.