@@ -14,6 +14,8 @@ use diesel::sql_types::{Array, BigInt, Binary, Bool, Int8, Integer, Jsonb, Text,
14
14
use diesel:: QuerySource as _;
15
15
use graph:: components:: store:: write:: { EntityWrite , RowGroup , WriteChunk } ;
16
16
use graph:: components:: store:: { Child as StoreChild , DerivedEntityQuery } ;
17
+
18
+ use graph:: data:: graphql:: IntoValue ;
17
19
use graph:: data:: store:: { Id , IdType , NULL } ;
18
20
use graph:: data:: store:: { IdList , IdRef , QueryObject } ;
19
21
use graph:: data:: value:: { Object , Word } ;
@@ -439,6 +441,47 @@ pub fn parse_id(id_type: IdType, json: serde_json::Value) -> Result<Id, StoreErr
439
441
}
440
442
}
441
443
444
+ #[ derive( QueryableByName , Debug ) ]
445
+ pub struct JSONData {
446
+ #[ diesel( sql_type = Jsonb ) ]
447
+ pub data : serde_json:: Value ,
448
+ }
449
+
450
+ impl IntoValue for JSONData {
451
+ fn into_value ( self ) -> r:: Value {
452
+ JSONData :: to_value ( self . data )
453
+ }
454
+ }
455
+
456
+ impl JSONData {
457
+ pub fn to_value ( data : serde_json:: Value ) -> r:: Value {
458
+ match data {
459
+ serde_json:: Value :: Null => r:: Value :: Null ,
460
+ serde_json:: Value :: Bool ( b) => r:: Value :: Boolean ( b) ,
461
+ serde_json:: Value :: Number ( n) => {
462
+ if let Some ( i) = n. as_i64 ( ) {
463
+ r:: Value :: Int ( i)
464
+ } else {
465
+ r:: Value :: Float ( n. as_f64 ( ) . unwrap ( ) )
466
+ }
467
+ }
468
+ serde_json:: Value :: String ( s) => r:: Value :: String ( s) ,
469
+ serde_json:: Value :: Array ( vals) => {
470
+ let vals: Vec < _ > = vals. into_iter ( ) . map ( JSONData :: to_value) . collect :: < Vec < _ > > ( ) ;
471
+ r:: Value :: List ( vals)
472
+ }
473
+ serde_json:: Value :: Object ( map) => {
474
+ let mut m = std:: collections:: BTreeMap :: new ( ) ;
475
+ for ( k, v) in map {
476
+ let value = JSONData :: to_value ( v) ;
477
+ m. insert ( Word :: from ( k) , value) ;
478
+ }
479
+ r:: Value :: object ( m)
480
+ }
481
+ }
482
+ }
483
+ }
484
+
442
485
/// Helper struct for retrieving entities from the database. With diesel, we
443
486
/// can only run queries that return columns whose number and type are known
444
487
/// at compile time. Because of that, we retrieve the actual data for an
0 commit comments