import { isFunction } from '@terascope/utils' ; import { fieldValidationRowExecution , fieldValidationColumnExecution , wholeFieldValidationRowExecution , wholeFieldValidationColumnExecution , recordValidationExecution } from './validators' ; import { fieldTransformColumnExecution , fieldTransformRowExecution , wholeFieldTransformColumnExecution , wholeFieldTransformRowExecution } from './transformers' ; import { FunctionAdapterOptions , RecordFunctionAdapterOperation , FieldFunctionAdapterOperation } from './interfaces' ; import { FieldValidateConfig , FieldTransformConfig , isFieldValidation , isFieldTransform , RecordValidationConfig , RecordTransformConfig , isRecordValidation , ProcessMode , FunctionDefinitionConfig , InitialFunctionContext } from '../../function-configs/interfaces' ; import { validateFunctionArgs } from '../argument-validator' ; import { getDataTypeFieldAndChildren } from '../utils' ; function functionAdapter < T extends Record < string , any > = Record < string , unknown > > ( fnDef , options ? ) ; function functionAdapter < T extends Record < string , any > = Record < string , unknown > > ( fnDef , options ? ) ; function functionAdapter < T extends Record < string , any > = Record < string , unknown > > ( /** The field validation or transform function definition */ fnDef , options = { } ) { const { field , config , preserveNulls = true , preserveEmptyObjects = true , args = { } as T } = options ; if ( ! isFunction ( args ) ) { validateFunctionArgs ( fnDef , args ) ; } const fnConfig = { args , inputConfig : getDataTypeFieldAndChildren ( config , field ) , preserveNulls , preserveEmptyObjects , field } ; if ( isFieldValidation ( fnDef ) ) { if ( fnDef . process_mode === ProcessMode . FULL_VALUES ) { return { rows : wholeFieldValidationRowExecution < T > ( fnDef , fnConfig ) , column : wholeFieldValidationColumnExecution < T > ( fnDef , fnConfig ) } ; } return { rows : fieldValidationRowExecution ( fnDef , fnConfig ) , column : fieldValidationColumnExecution ( fnDef , fnConfig ) } ; } if ( isFieldTransform ( fnDef ) ) { if ( fnDef . process_mode === ProcessMode . FULL_VALUES ) { return { rows : wholeFieldTransformRowExecution ( fnDef , fnConfig ) , column : wholeFieldTransformColumnExecution ( fnDef , fnConfig ) } ; } return { rows : fieldTransformRowExecution ( fnDef , fnConfig ) , column : fieldTransformColumnExecution ( fnDef , fnConfig ) } ; } if ( isRecordValidation ( fnDef ) ) { return { rows : recordValidationExecution ( fnDef , fnConfig ) } ; } throw new Error ( `Function definition ${ JSON . stringify ( fnDef , null , 4 ) } is not currently supported` ) ; }