fix(apb): .id() now works on json strings too

This commit is contained in:
əlemi 2024-04-13 06:34:56 +02:00
parent 9c9b5ec380
commit 87d802fa2e
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 19 additions and 12 deletions

View file

@ -32,8 +32,21 @@ impl Base for String {
#[cfg(feature = "unstructured")] #[cfg(feature = "unstructured")]
impl Base for serde_json::Value { impl Base for serde_json::Value {
crate::getter! { id -> &str } fn base_type(&self) -> Option<BaseType> {
crate::getter! { base_type -> type BaseType } if self.is_string() {
Some(BaseType::Link(LinkType::Link))
} else {
self.get("type")?.as_str()?.try_into().ok()
}
}
fn id(&self) -> Option<&str> {
if self.is_string() {
self.as_str()
} else {
self.get("id").map(|x| x.as_str())?
}
}
} }
#[cfg(feature = "unstructured")] #[cfg(feature = "unstructured")]

View file

@ -37,16 +37,10 @@ impl Link for String {
impl Link for serde_json::Value { impl Link for serde_json::Value {
// TODO this can fail, but it should never do! // TODO this can fail, but it should never do!
fn href(&self) -> &str { fn href(&self) -> &str {
match self { if self.is_string() {
serde_json::Value::String(x) => x, self.as_str().unwrap_or("")
serde_json::Value::Object(map) => } else {
map.get("href") self.get("href").map(|x| x.as_str().unwrap_or("")).unwrap_or("")
.map(|h| h.as_str().unwrap_or(""))
.unwrap_or(""),
_ => {
tracing::error!("failed getting href on invalid json Link object");
""
},
} }
} }