Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parse_ruby_hash): drop initial colon in key parsing #1050

Merged
merged 5 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1050.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The `parse_ruby_hash` parser is extended to match Datadog implementation. Previously it would parse the key in `{:key => "value"}` as `:key`, now it will parse it as `key`.
29 changes: 20 additions & 9 deletions src/parsing/ruby_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn parse_colon_key<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
char(':'),
alt((parse_str('"'), parse_str('\''), parse_symbol_key)),
),
|res| (String::from(":") + res).into(),
KeyString::from,
)(input)
}

Expand Down Expand Up @@ -215,13 +215,24 @@ mod tests {
}

#[test]
fn test_parse_symbol_value() {
let result = parse_ruby_hash(r#"{ "key" => :foo }"#).unwrap();
fn test_parse_symbol_key() {
let result = parse_ruby_hash(r#"{ :key => "foo", :number => 500 }"#).unwrap();
assert!(result.is_object());
let result = result.as_object().unwrap();
let value = result.get("key").unwrap();
assert!(value.is_bytes());
assert_eq!(value.as_bytes().unwrap(), ":foo");
assert_eq!(value.as_bytes().unwrap(), "foo");
assert!(result.get("number").unwrap().is_float());
}

#[test]
fn test_parse_symbol_colon_separator() {
let result = parse_ruby_hash(r#"{ key: "foo" }"#).unwrap();
assert!(result.is_object());
let result = result.as_object().unwrap();
let value = result.get("key").unwrap();
assert!(value.is_bytes());
assert_eq!(value.as_bytes().unwrap(), "foo");
}

#[test]
Expand Down Expand Up @@ -257,17 +268,17 @@ mod tests {
.unwrap();
assert!(result.is_object());
let result = result.as_object().unwrap();
assert!(result.get(":colon").unwrap().is_bytes());
assert!(result.get(":double").unwrap().is_bytes());
assert!(result.get(":simple").unwrap().is_bytes());
assert!(result.get("colon").unwrap().is_bytes());
assert!(result.get("double").unwrap().is_bytes());
assert!(result.get("simple").unwrap().is_bytes());
}

#[test]
fn test_parse_arrow_object_key_underscore() {
let result = parse_ruby_hash(r#"{ :with_underscore => "hello world" }"#).unwrap();
assert!(result.is_object());
let result = result.as_object().unwrap();
assert!(result.get(":with_underscore").unwrap().is_bytes());
assert!(result.get("with_underscore").unwrap().is_bytes());
}

#[test]
Expand Down Expand Up @@ -320,7 +331,7 @@ mod tests {
parse_ruby_hash(r#"{:hello=>"world",'number'=>42,"weird"=>'format\'here'}"#).unwrap();
assert!(result.is_object());
let result = result.as_object().unwrap();
assert!(result.get(":hello").unwrap().is_bytes());
assert!(result.get("hello").unwrap().is_bytes());
assert!(result.get("number").unwrap().is_float());
}

Expand Down
Loading