kv: refactor and get rid of useless wrapper type

This commit is contained in:
Peter Cai 2021-04-04 17:15:23 +08:00
parent 4f608c8e36
commit 48a0b80145
1 changed files with 13 additions and 29 deletions

View File

@ -50,29 +50,6 @@ pub struct KvGetOptions {
data_type: String, data_type: String,
} }
// Wrapper over JsKvGetMetadata
// response of KV.getWithMetadata
pub struct KvGetMetadata {
value: Option<JsValue>,
// Metadata should always be pure JSON object; not class instances or buffers
metadata: Option<serde_json::Value>,
}
impl KvGetMetadata {
fn wrap(inner: JsKvGetMetadata) -> KvGetMetadata {
let value = inner.value();
let metadata = inner.metadata();
KvGetMetadata {
value: if value.is_null() { None } else { Some(value) },
metadata: if metadata.is_null() {
None
} else {
Some(metadata.into_serde().unwrap())
},
}
}
}
pub struct KvNamespace { pub struct KvNamespace {
inner: JsKvNamespace, inner: JsKvNamespace,
} }
@ -109,6 +86,8 @@ impl KvNamespace {
} }
} }
// Get a buffer value from KV with its metadata
// we assume that all metadata are pure JSON objects (Deserialize)
pub async fn get_buf_metadata<T: for<'de> Deserialize<'de>>( pub async fn get_buf_metadata<T: for<'de> Deserialize<'de>>(
&self, &self,
key: &str, key: &str,
@ -129,14 +108,19 @@ impl KvNamespace {
return (None, None); return (None, None);
} }
let value = KvGetMetadata::wrap(obj.into()); let obj: JsKvGetMetadata = obj.into();
( (
value.value.map(|v| Uint8Array::new(&v).to_vec()), if obj.value().is_null() {
value None
.metadata } else {
.map(|v| serde_json::from_value(v).ok()) Some(Uint8Array::new(&obj.value()).to_vec())
.flatten(), },
if obj.metadata().is_null() {
None
} else {
obj.metadata().into_serde().ok()
},
) )
} }
} }