@@ -184,7 +184,11 @@ impl std::fmt::Display for KeyValue {
184184}
185185
186186impl KeyValue {
187- pub fn from_json ( value : serde_json:: Value , fields_schema : & [ FieldSchema ] ) -> Result < Self > {
187+ /// For export purpose only for now. Will remove after switching export to using FullKeyValue.
188+ pub fn from_json_for_export (
189+ value : serde_json:: Value ,
190+ fields_schema : & [ FieldSchema ] ,
191+ ) -> Result < Self > {
188192 let value = if fields_schema. len ( ) == 1 {
189193 Value :: from_json ( value, & fields_schema[ 0 ] . value_type . typ ) ?
190194 } else {
@@ -464,18 +468,18 @@ impl FullKeyValue {
464468 }
465469
466470 pub fn from_json ( value : serde_json:: Value , schema : & [ FieldSchema ] ) -> Result < Self > {
467- let field_values =
468- if schema . len ( ) == 1 && matches ! ( schema[ 0 ] . value_type. typ, ValueType :: Basic ( _) ) {
469- Box :: from ( [ KeyValue :: from_json ( value , schema ) ? ] )
470- } else {
471- match value {
472- serde_json :: Value :: Array ( arr ) => arr
473- . into_iter ( )
474- . map ( |v| KeyValue :: from_json ( v, schema ) )
475- . collect :: < Result < Box < [ _ ] > > > ( ) ?,
476- _ => anyhow:: bail!( "expected array value, but got {}" , value) ,
477- }
478- } ;
471+ let field_values = if schema . len ( ) == 1
472+ && matches ! ( schema[ 0 ] . value_type. typ, ValueType :: Basic ( _) )
473+ {
474+ Box :: from ( [ KeyValue :: from_json_for_export ( value , schema ) ? ] )
475+ } else {
476+ match value {
477+ serde_json :: Value :: Array ( arr ) => std :: iter :: zip ( arr . into_iter ( ) , schema )
478+ . map ( |( v , s ) | Value :: < ScopeValue > :: from_json ( v, & s . value_type . typ ) ? . into_key ( ) )
479+ . collect :: < Result < Box < [ _ ] > > > ( ) ?,
480+ _ => anyhow:: bail!( "expected array value, but got {}" , value) ,
481+ }
482+ } ;
479483 Ok ( Self ( field_values) )
480484 }
481485
@@ -836,9 +840,7 @@ impl<VS> Value<VS> {
836840 . collect ( ) ,
837841 } ) ,
838842 Value :: UTable ( v) => Value :: UTable ( v. into_iter ( ) . map ( |v| v. into ( ) ) . collect ( ) ) ,
839- Value :: KTable ( v) => {
840- Value :: KTable ( v. into_iter ( ) . map ( |( k, v) | ( k. clone ( ) , v. into ( ) ) ) . collect ( ) )
841- }
843+ Value :: KTable ( v) => Value :: KTable ( v. into_iter ( ) . map ( |( k, v) | ( k, v. into ( ) ) ) . collect ( ) ) ,
842844 Value :: LTable ( v) => Value :: LTable ( v. into_iter ( ) . map ( |v| v. into ( ) ) . collect ( ) ) ,
843845 }
844846 }
0 commit comments