The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
refactor(storage): extract schema support helpers into a module
Steven Enamakelafea2be1eef8
diff --git a/crates/openhuman-core/src/storage/schema_support.rs b/crates/openhuman-core/src/storage/schema_support.rsindex a5caf2dfba..c9a4d86aca 100644--- a/crates/openhuman-core/src/storage/schema_support.rs+++ b/crates/openhuman-core/src/storage/schema_support.rs@@ -53,6 +53,83 @@ pub fn key_of(part: &str) -> Result<String, StorageError> {Key::of([part]).map(|key| key.as_str().to_owned())}+/// `camelCase` as `snake_case`: `authorId` -> `author_id`.+#[must_use]+pub fn snake_case(key: &str) -> String {+ let mut out = String::with_capacity(key.len() + 4);+ for ch in key.chars() {+ if ch.is_ascii_uppercase() {+ out.push('_');+ out.push(ch.to_ascii_lowercase());+ } else {+ out.push(ch);+ }+ }+ out+}++/// `snake_case` as `camelCase`: `author_id` -> `authorId`.+#[must_use]+pub fn camel_case(key: &str) -> String {+ let mut out = String::with_capacity(key.len());+ let mut upper = false;+ for ch in key.chars() {+ if ch == '_' {+ upper = true;+ } else if upper {+ out.push(ch.to_ascii_uppercase());+ upper = false;+ } else {+ out.push(ch);+ }+ }+ out+}++/// A copy of `value` with every object key (at any depth) in snake case.+/// `renames` lists `(camel key, snake key)` pairs that take precedence, for+/// fields whose v2 name is not the mechanical one (`createdAtMillis` ->+/// `created_ms`). Array elements are converted; strings and numbers are not+/// touched.+#[must_use]+pub fn snake_keys(value: &Value, renames: &[(&str, &str)]) -> Value {+ match value {+ Value::Object(map) => Value::Object(+ map.iter()+ .map(|(key, inner)| {+ let key = renames+ .iter()+ .find(|(camel, _)| camel == key)+ .map_or_else(|| snake_case(key), |(_, snake)| (*snake).to_owned());+ (key, snake_keys(inner, renames))+ })+ .collect(),+ ),+ Value::Array(items) => Value::Array(items.iter().map(|v| snake_keys(v, renames)).collect()),+ other => other.clone(),+ }+}++/// The inverse of [`snake_keys`] for the same `renames`.+#[must_use]+pub fn camel_keys(value: &Value, renames: &[(&str, &str)]) -> Value {+ match value {+ Value::Object(map) => Value::Object(+ map.iter()+ .map(|(key, inner)| {+ let key = renames+ .iter()+ .find(|(_, snake)| snake == key)+ .map_or_else(|| camel_case(key), |(camel, _)| (*camel).to_owned());+ (key, camel_keys(inner, renames))+ })+ .collect(),+ ),+ Value::Array(items) => Value::Array(items.iter().map(|v| camel_keys(v, renames)).collect()),+ other => other.clone(),+ }+}+/// A body object builder that drops absent values, so a v2 body never carries/// `null` for a field that was never set.#[derive(Default)]diff --git a/crates/openhuman-core/src/storage/schema_support_tests.rs b/crates/openhuman-core/src/storage/schema_support_tests.rsindex c5894a4bca..9083f763ce 100644--- a/crates/openhuman-core/src/storage/schema_support_tests.rs+++ b/crates/openhuman-core/src/storage/schema_support_tests.rs@@ -27,3 +27,23 @@ fn a_natural_key_escapes_separators() {assert_eq!(key_of("a/b").unwrap(), "a%2Fb");assert!(key_of("").is_err());}++#[test]+fn keys_convert_at_every_depth_and_back() {+ let renames = [("createdAtMillis", "created_ms")];+ let camel = json!({+ "id": "a",+ "createdAtMillis": 5,+ "versions": [{ "authorId": "x", "file": { "fileRoot": "r" } }],+ });+ let snake = snake_keys(&camel, &renames);+ assert_eq!(+ snake,+ json!({+ "id": "a",+ "created_ms": 5,+ "versions": [{ "author_id": "x", "file": { "file_root": "r" } }],+ })+ );+ assert_eq!(camel_keys(&snake, &renames), camel);+}