Skip to content

Instantly share code, notes, and snippets.

@smilu97
Last active June 29, 2020 06:17
Show Gist options
  • Save smilu97/6258f8e9ffc2383dc67d84af7d90565f to your computer and use it in GitHub Desktop.
Save smilu97/6258f8e9ffc2383dc67d84af7d90565f to your computer and use it in GitHub Desktop.
Find Many-to-One related entities and merge them.
use serde_json::{Value, json};
use superslice::*;
pub fn find_with_keys<T: serde::Serialize, U: Ord>(
items: &Vec<T>,
keys: &Vec<U>,
key: U
) -> Value {
let lo = keys.lower_bound(&key);
let up = keys.upper_bound(&key);
json!((lo..up).into_iter().map(|i| {
json!(items[i])
}).collect::<Value>())
}
pub fn join_association<'r, A: serde::Serialize, B: Ord, C: serde::Serialize>(
parents: &Vec<A>,
id_extractor: fn(x: &A) -> B,
children: &mut Vec<C>,
pid_extractor: fn(x: &C) -> B,
name: &'r str
) -> Value {
children.sort_unstable_by_key(pid_extractor);
let pids: Vec<B> = children.into_iter().map(|x| { pid_extractor(x) }).collect();
json!(parents.into_iter().map(|x| -> Value {
let mut j_parent = json!(x);
j_parent[name] = find_with_keys(children, &pids, id_extractor(x));
j_parent
}).collect::<Value>())
}
// let json_parents = utils::json::join_association(
// &parents,
// |x| { x.id }, // parent id extractor from parent
// &mut children, // mutable, would be sorted with `parent_id`
// |x| { x.parent_id }, // parent id extractor from child
// "descriptions" // field name for children
// );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment