-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Cache Streamer Message from Block Streamer (#582)
- Loading branch information
1 parent
761a9a9
commit 78a83ae
Showing
6 changed files
with
193 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ mod redis; | |
mod rules; | ||
mod s3_client; | ||
mod server; | ||
mod utils; | ||
|
||
#[cfg(test)] | ||
mod test_utils; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
pub fn snake_to_camel(value: &mut serde_json::value::Value) { | ||
match value { | ||
serde_json::value::Value::Object(map) => { | ||
for key in map.keys().cloned().collect::<Vec<String>>() { | ||
let new_key = key | ||
.split('_') | ||
.enumerate() | ||
.map(|(i, str)| { | ||
if i > 0 { | ||
return str[..1].to_uppercase() + &str[1..]; | ||
} | ||
str.to_owned() | ||
}) | ||
.collect::<Vec<String>>() | ||
.join(""); | ||
|
||
if let Some(mut val) = map.remove(&key) { | ||
snake_to_camel(&mut val); | ||
map.insert(new_key, val); | ||
} | ||
} | ||
} | ||
serde_json::value::Value::Array(vec) => { | ||
for val in vec { | ||
snake_to_camel(val); | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use serde_json::json; | ||
|
||
#[test] | ||
fn flat() { | ||
let mut value = json!({ | ||
"first_name": "John", | ||
"last_name": "Doe" | ||
}); | ||
|
||
snake_to_camel(&mut value); | ||
|
||
assert_eq!( | ||
value, | ||
json!({ | ||
"firstName": "John", | ||
"lastName": "Doe" | ||
}) | ||
); | ||
} | ||
|
||
#[test] | ||
fn nested() { | ||
let mut value = json!({ | ||
"user_details": { | ||
"first_name": "John", | ||
"last_name": "Doe" | ||
} | ||
}); | ||
|
||
snake_to_camel(&mut value); | ||
|
||
assert_eq!( | ||
value, | ||
json!({ | ||
"userDetails": { | ||
"firstName": "John", | ||
"lastName": "Doe" | ||
} | ||
}) | ||
); | ||
} | ||
|
||
#[test] | ||
fn array() { | ||
let mut value = json!([{ | ||
"first_name": "John", | ||
"last_name": "Doe" | ||
}, { | ||
"first_name": "Jane", | ||
"last_name": "Doe" | ||
}]); | ||
|
||
snake_to_camel(&mut value); | ||
|
||
assert_eq!( | ||
value, | ||
json!([{ | ||
"firstName": "John", | ||
"lastName": "Doe" | ||
}, { | ||
"firstName": "Jane", | ||
"lastName": "Doe" | ||
}]) | ||
); | ||
} | ||
|
||
#[test] | ||
fn nested_and_array() { | ||
let mut value = json!({ | ||
"user_details": { | ||
"personal_info": { | ||
"first_name": "John", | ||
"last_name": "Doe" | ||
}, | ||
"address": { | ||
"city_name": "Some City", | ||
"country_name": "Some Country" | ||
} | ||
}, | ||
"user_education": [{ | ||
"school_name": "XYZ High School", | ||
"degree": "High School Diploma" | ||
}, { | ||
"university_name": "ABC University", | ||
"degree": "Bachelor's" | ||
}] | ||
}); | ||
|
||
snake_to_camel(&mut value); | ||
|
||
assert_eq!( | ||
value, | ||
json!({ | ||
"userDetails": { | ||
"personalInfo": { | ||
"firstName": "John", | ||
"lastName": "Doe" | ||
}, | ||
"address": { | ||
"cityName": "Some City", | ||
"countryName": "Some Country" | ||
} | ||
}, | ||
"userEducation": [{ | ||
"schoolName": "XYZ High School", | ||
"degree": "High School Diploma" | ||
}, { | ||
"universityName": "ABC University", | ||
"degree": "Bachelor's" | ||
}] | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters