@@ -171,21 +171,15 @@ impl ForeignKeyClauses for Column {
171
171
}
172
172
}
173
173
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 > ;
185
179
}
186
180
187
- impl EntityData {
188
- fn value_from_json (
181
+ impl FromColumnValue for graph :: prelude :: Value {
182
+ fn from_column_value (
189
183
column_type : & ColumnType ,
190
184
json : serde_json:: Value ,
191
185
) -> Result < graph:: prelude:: Value , StoreError > {
@@ -254,15 +248,30 @@ impl EntityData {
254
248
( j:: Array ( values) , _) => Ok ( g:: List (
255
249
values
256
250
. into_iter ( )
257
- . map ( |v| Self :: value_from_json ( column_type, v) )
251
+ . map ( |v| Self :: from_column_value ( column_type, v) )
258
252
. collect :: < Result < Vec < _ > , _ > > ( ) ?,
259
253
) ) ,
260
254
( j:: Object ( _) , _) => {
261
255
unimplemented ! ( "objects as entity attributes are not needed/supported" )
262
256
}
263
257
}
264
258
}
259
+ }
265
260
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 {
266
275
pub fn entity_type ( & self ) -> String {
267
276
self . entity . clone ( )
268
277
}
@@ -285,10 +294,12 @@ impl EntityData {
285
294
// column; those will be things like the block_range that
286
295
// is used internally for versioning
287
296
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) ?;
289
299
entity. insert ( "g$parent_id" . to_owned ( ) , value) ;
290
300
} 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) ?;
292
303
if value != Value :: Null {
293
304
entity. insert ( column. field . clone ( ) , value) ;
294
305
}
0 commit comments