import { reduce } from 'lodash' ; import { difference , flattenKeys , pickDeep } from './utils' ; import { ParsedUsageCollection } from './ts_parser' ; import { generateMapping , compatibleSchemaTypes } from './manage_schema' ; import { kindToDescriptorName } from './serializer' ; export function checkMatchingMapping ( UsageCollections , esMapping ) { const generatedMapping = generateMapping ( UsageCollections ) ; return difference ( generatedMapping , esMapping ) ; } interface IncompatibleDescriptor { diff : Record < string , number > ; collectorPath : string ; message : string [ ] ; } export function checkCompatibleTypeDescriptor ( usageCollections ) { const results = usageCollections . map ( ( [ collectorPath , collectorDetails ] ) => { const typeDescriptorTypes = flattenKeys ( pickDeep ( collectorDetails . fetch . typeDescriptor , 'kind' ) ) ; const typeDescriptorKinds = reduce ( typeDescriptorTypes , ( acc , type , key ) => { key = key . replace ( /'/g , '' ) ; try { acc [ key ] = kindToDescriptorName ( type ) ; } catch ( err ) { throw Error ( `Unrecognized type (${ key }: ${ type }) in ${ collectorPath }` ) ; } return acc ; } , { } as any ) ; const schemaTypes = flattenKeys ( pickDeep ( collectorDetails . schema . value , 'type' ) ) ; const transformedMappingKinds = reduce ( schemaTypes , ( acc , type , key ) => { key = key . replace ( /'/g , '' ) ; try { acc [ key . replace ( /.type$/ , '.kind' ) ] = compatibleSchemaTypes ( type as any ) ; } catch ( err ) { throw Error ( `Unrecognized type (${ key }: ${ type }) in ${ collectorPath }` ) ; } return acc ; } , { } as any ) ; const diff = difference ( typeDescriptorKinds , transformedMappingKinds ) ; const diffEntries = Object . entries ( diff ) ; if ( ! diffEntries . length ) { return false ; } return { diff , collectorPath , message : diffEntries . map ( ( [ key ] ) => { const interfaceKey = key . replace ( '.kind' , '' ) ; try { const expectedDescriptorType = JSON . stringify ( transformedMappingKinds [ key ] , null , 2 ) ; const actualDescriptorType = JSON . stringify ( typeDescriptorKinds [ key ] , null , 2 ) ; return `incompatible Type key (${ collectorDetails . fetch . typeName }.${ interfaceKey }): expected (${ expectedDescriptorType }) got (${ actualDescriptorType }).` ; } catch ( err ) { throw Error ( `Error converting ${ key } in ${ collectorPath }.\n${ err }` ) ; } } ) , } ; } ) ; return results . filter ( ( entry ) : entry is IncompatibleDescriptor => entry !== false ) ; } export function checkCollectorIntegrity ( UsageCollections , esMapping ) { return UsageCollections ; }