Skip to content

Commit

Permalink
fix: Change type signature for MapValue to allow for empty fields
Browse files Browse the repository at this point in the history
Modifies the `MapValue` type signature to allow the `fields` property to
be an `Option` type. This commit adjusts usage in `Dto` (Data Transfer
Object) to match accordingly

Signed-off-by: Evan Reeves <[email protected]>
  • Loading branch information
JadedEvan authored and davidgraeff committed Dec 23, 2020
1 parent d20b776 commit 810c636
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ pub struct BatchGetDocumentsRequest {

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
pub struct MapValue {
pub fields: HashMap<String, Value>,
pub fields: Option<HashMap<String, Value>>,
}

#[derive(Default, Clone, Debug, Serialize, Deserialize)]
Expand Down
15 changes: 11 additions & 4 deletions src/firebase_rest_to_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ pub(crate) fn firebase_value_to_serde_value(v: &dto::Value) -> serde_json::Value
}
} else if let Some(map_value) = v.map_value.as_ref() {
let mut map: Map<String, serde_json::value::Value> = Map::new();
for (map_key, map_v) in &map_value.fields {
map.insert(map_key.clone(), firebase_value_to_serde_value(&map_v));
match &map_value.fields {
Some(map_fields) => {
for (map_key, map_v) in map_fields {
map.insert(map_key.clone(), firebase_value_to_serde_value(&map_v));
}
},
None => {
// No values to insert into map
}
}
return Value::Object(map);
} else if let Some(string_value) = v.string_value.as_ref() {
Expand Down Expand Up @@ -80,7 +87,7 @@ pub(crate) fn serde_value_to_firebase_value(v: &serde_json::Value) -> dto::Value
map.insert(map_key.to_owned(), serde_value_to_firebase_value(&map_v));
}
return dto::Value {
map_value: Some(dto::MapValue { fields: map }),
map_value: Some(dto::MapValue { fields: Some(map) }),
..Default::default()
};
} else if let Some(string_value) = v.as_str() {
Expand Down Expand Up @@ -156,7 +163,7 @@ where
{
let v = serde_json::to_value(pod)?;
Ok(dto::Document {
fields: Some(serde_value_to_firebase_value(&v).map_value.unwrap().fields),
fields: serde_value_to_firebase_value(&v).map_value.unwrap().fields,
..Default::default()
})
}
Expand Down

0 comments on commit 810c636

Please sign in to comment.