import _ from 'lodash' ; import { SavedObject , SavedObjectConfig } from '../../types' ; import { extractSearchSourceReferences , expandShorthand } from '../../../../data/public' ; export function serializeSavedObject ( savedObject , config ) { const mapping = expandShorthand ( config . mapping ) ; const attributes = { } as Record < string , any > ; const references = [ ] ; _ . forOwn ( mapping , ( fieldMapping , fieldName ) => { if ( typeof fieldName !== 'string' ) { return ; } const savedObjectFieldVal = savedObject [ fieldName ] ; if ( savedObjectFieldVal != null ) { attributes [ fieldName ] = fieldMapping . _serialize ? fieldMapping . _serialize ( savedObjectFieldVal ) : savedObjectFieldVal ; } } ) ; if ( savedObject . searchSource ) { const { searchSourceJSON , references : searchSourceReferences , } = savedObject . searchSource . serialize ( ) ; attributes . kibanaSavedObjectMeta = { searchSourceJSON } ; references . push ( ... searchSourceReferences ) ; } if ( savedObject . searchSourceFields ) { const [ searchSourceFields , searchSourceReferences ] = extractSearchSourceReferences ( savedObject . searchSourceFields ) ; const searchSourceJSON = JSON . stringify ( searchSourceFields ) ; attributes . kibanaSavedObjectMeta = { searchSourceJSON } ; references . push ( ... searchSourceReferences ) ; } if ( savedObject . unresolvedIndexPatternReference ) { references . push ( savedObject . unresolvedIndexPatternReference ) ; } return { attributes , references } ; }