@@ -14,6 +14,8 @@ use diesel::sql_types::{Array, BigInt, Binary, Bool, Int8, Integer, Jsonb, Text,
1414use diesel:: QuerySource as _;
1515use graph:: components:: store:: write:: { EntityWrite , RowGroup , WriteChunk } ;
1616use graph:: components:: store:: { Child as StoreChild , DerivedEntityQuery } ;
17+
18+ use graph:: data:: graphql:: IntoValue ;
1719use graph:: data:: store:: { Id , IdType , NULL } ;
1820use graph:: data:: store:: { IdList , IdRef , QueryObject } ;
1921use graph:: data:: value:: { Object , Word } ;
@@ -439,6 +441,47 @@ pub fn parse_id(id_type: IdType, json: serde_json::Value) -> Result<Id, StoreErr
439441 }
440442}
441443
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+
442485/// Helper struct for retrieving entities from the database. With diesel, we
443486/// can only run queries that return columns whose number and type are known
444487/// at compile time. Because of that, we retrieve the actual data for an
0 commit comments