Skip to content

Commit d0e62f1

Browse files
committed
relationial_queries: Refactor value_from_json as trait
1 parent a8bb117 commit d0e62f1

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

store/postgres/src/relational_queries.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,15 @@ impl ForeignKeyClauses for Column {
171171
}
172172
}
173173

174-
/// Helper struct for retrieving entities from the database. With diesel, we
175-
/// can only run queries that return columns whose number and type are known
176-
/// at compile time. Because of that, we retrieve the actual data for an
177-
/// entity as Jsonb by converting the row containing the entity using the
178-
/// `to_jsonb` function.
179-
#[derive(QueryableByName)]
180-
pub struct EntityData {
181-
#[sql_type = "Text"]
182-
entity: String,
183-
#[sql_type = "Jsonb"]
184-
data: serde_json::Value,
174+
trait FromColumnValue: Sized {
175+
fn from_column_value(
176+
column_type: &ColumnType,
177+
json: serde_json::Value,
178+
) -> Result<Self, StoreError>;
185179
}
186180

187-
impl EntityData {
188-
fn value_from_json(
181+
impl FromColumnValue for graph::prelude::Value {
182+
fn from_column_value(
189183
column_type: &ColumnType,
190184
json: serde_json::Value,
191185
) -> Result<graph::prelude::Value, StoreError> {
@@ -254,15 +248,30 @@ impl EntityData {
254248
(j::Array(values), _) => Ok(g::List(
255249
values
256250
.into_iter()
257-
.map(|v| Self::value_from_json(column_type, v))
251+
.map(|v| Self::from_column_value(column_type, v))
258252
.collect::<Result<Vec<_>, _>>()?,
259253
)),
260254
(j::Object(_), _) => {
261255
unimplemented!("objects as entity attributes are not needed/supported")
262256
}
263257
}
264258
}
259+
}
265260

261+
/// Helper struct for retrieving entities from the database. With diesel, we
262+
/// can only run queries that return columns whose number and type are known
263+
/// at compile time. Because of that, we retrieve the actual data for an
264+
/// entity as Jsonb by converting the row containing the entity using the
265+
/// `to_jsonb` function.
266+
#[derive(QueryableByName)]
267+
pub struct EntityData {
268+
#[sql_type = "Text"]
269+
entity: String,
270+
#[sql_type = "Jsonb"]
271+
data: serde_json::Value,
272+
}
273+
274+
impl EntityData {
266275
pub fn entity_type(&self) -> String {
267276
self.entity.clone()
268277
}
@@ -285,10 +294,12 @@ impl EntityData {
285294
// column; those will be things like the block_range that
286295
// is used internally for versioning
287296
if key == "g$parent_id" {
288-
let value = Self::value_from_json(&ColumnType::String, json)?;
297+
let value =
298+
graph::prelude::Value::from_column_value(&ColumnType::String, json)?;
289299
entity.insert("g$parent_id".to_owned(), value);
290300
} else if let Some(column) = table.column(&SqlName::verbatim(key)) {
291-
let value = Self::value_from_json(&column.column_type, json)?;
301+
let value =
302+
graph::prelude::Value::from_column_value(&column.column_type, json)?;
292303
if value != Value::Null {
293304
entity.insert(column.field.clone(), value);
294305
}

0 commit comments

Comments
 (0)