import { ParsedUsageCollection } from './ts_parser' ; export type AllowedSchemaNumberTypes = 'long' | 'integer' | 'short' | 'byte' | 'double' | 'float' ; export type AllowedSchemaTypes = AllowedSchemaNumberTypes | 'keyword' | 'text' | 'boolean' | 'date' ; export function compatibleSchemaTypes ( type ) { switch ( type ) { case 'keyword' : case 'text' : case 'date' : return 'string' ; case 'boolean' : return 'boolean' ; case 'long' : case 'integer' : case 'short' : case 'byte' : case 'double' : case 'float' : return 'number' ; case 'array' : return 'array' ; default : throw new Error ( `Unknown schema type ${ type }` ) ; } } export function isObjectMapping ( entity ) { if ( typeof entity === 'object' ) { if ( typeof entity . type === 'string' && entity . type === 'object' ) { return true ; } if ( typeof entity . type === 'undefined' ) { return true ; } if ( typeof entity . type === 'object' ) { return true ; } } return false ; } function isArrayMapping ( entity ) { return typeof entity === 'object' && entity . type === 'array' && typeof entity . items === 'object' ; } function getValueMapping ( value ) { return isObjectMapping ( value ) ? transformToEsMapping ( value ) : value ; } function transformToEsMapping ( usageMappingValue ) { const fieldMapping = { properties : { } } ; for ( const [ key , value ] of Object . entries ( usageMappingValue ) ) { if ( isArrayMapping ( value ) ) { fieldMapping . properties [ key ] = { ... value , items : getValueMapping ( value . items ) } ; } else { fieldMapping . properties [ key ] = getValueMapping ( value ) ; } } return fieldMapping ; } export function generateMapping ( usageCollections ) { const esMapping = { properties : { } } ; for ( const [ , collecionDetails ] of usageCollections ) { esMapping . properties [ collecionDetails . collectorName ] = transformToEsMapping ( collecionDetails . schema . value ) ; } return esMapping ; }