-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and organize
public/utils
(#184)
Signed-off-by: Tyler Ohlsen <[email protected]> (cherry picked from commit 43b89da)
- Loading branch information
1 parent
147df5b
commit 0b51252
Showing
10 changed files
with
843 additions
and
783 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { FormikValues } from 'formik'; | ||
import { | ||
WorkflowConfig, | ||
WorkflowFormValues, | ||
IngestConfig, | ||
SearchConfig, | ||
ProcessorsConfig, | ||
IndexConfig, | ||
IProcessorConfig, | ||
ConfigFieldType, | ||
ConfigFieldValue, | ||
ModelFormValue, | ||
} from '../../common'; | ||
|
||
/* | ||
**************** Config -> formik utils ********************** | ||
*/ | ||
|
||
export function uiConfigToFormik(config: WorkflowConfig): WorkflowFormValues { | ||
const formikValues = {} as WorkflowFormValues; | ||
formikValues['ingest'] = ingestConfigToFormik(config.ingest); | ||
formikValues['search'] = searchConfigToFormik(config.search); | ||
return formikValues; | ||
} | ||
|
||
function ingestConfigToFormik( | ||
ingestConfig: IngestConfig | undefined | ||
): FormikValues { | ||
let ingestFormikValues = {} as FormikValues; | ||
if (ingestConfig) { | ||
ingestFormikValues['enrich'] = processorsConfigToFormik( | ||
ingestConfig.enrich | ||
); | ||
ingestFormikValues['index'] = indexConfigToFormik(ingestConfig.index); | ||
} | ||
return ingestFormikValues; | ||
} | ||
|
||
function processorsConfigToFormik( | ||
processorsConfig: ProcessorsConfig | ||
): FormikValues { | ||
let formValues = {} as FormikValues; | ||
processorsConfig.processors.forEach((processorConfig) => { | ||
formValues[processorConfig.id] = processorConfigToFormik(processorConfig); | ||
}); | ||
return formValues; | ||
} | ||
|
||
export function processorConfigToFormik( | ||
processorConfig: IProcessorConfig | ||
): FormikValues { | ||
const fieldValues = {} as FormikValues; | ||
processorConfig.fields.forEach((field) => { | ||
fieldValues[field.id] = field.value || getInitialValue(field.type); | ||
}); | ||
return fieldValues; | ||
} | ||
|
||
function indexConfigToFormik(indexConfig: IndexConfig): FormikValues { | ||
let formValues = {} as FormikValues; | ||
formValues['name'] = | ||
indexConfig.name.value || getInitialValue(indexConfig.name.type); | ||
return formValues; | ||
} | ||
|
||
function searchConfigToFormik( | ||
searchConfig: SearchConfig | undefined | ||
): FormikValues { | ||
let searchFormikValues = {} as FormikValues; | ||
if (searchConfig) { | ||
// TODO: implement for request | ||
searchFormikValues['request'] = {}; | ||
searchFormikValues['enrichRequest'] = processorsConfigToFormik( | ||
searchConfig.enrichRequest | ||
); | ||
searchFormikValues['enrichResponse'] = processorsConfigToFormik( | ||
searchConfig.enrichResponse | ||
); | ||
} | ||
return searchFormikValues; | ||
} | ||
|
||
// Helper fn to get an initial value based on the field type | ||
export function getInitialValue(fieldType: ConfigFieldType): ConfigFieldValue { | ||
switch (fieldType) { | ||
case 'string': { | ||
return ''; | ||
} | ||
case 'select': { | ||
return ''; | ||
} | ||
case 'model': { | ||
return { | ||
id: '', | ||
category: undefined, | ||
algorithm: undefined, | ||
} as ModelFormValue; | ||
} | ||
case 'map': { | ||
return []; | ||
} | ||
case 'json': { | ||
return {}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { Schema, ObjectSchema } from 'yup'; | ||
import * as yup from 'yup'; | ||
import { | ||
WorkflowConfig, | ||
WorkflowSchema, | ||
IngestConfig, | ||
SearchConfig, | ||
ProcessorsConfig, | ||
WorkflowSchemaObj, | ||
IConfigField, | ||
IndexConfig, | ||
} from '../../common'; | ||
|
||
/* | ||
**************** Schema / validation utils ********************** | ||
*/ | ||
|
||
export function uiConfigToSchema(config: WorkflowConfig): WorkflowSchema { | ||
const schemaObj = {} as WorkflowSchemaObj; | ||
schemaObj['ingest'] = ingestConfigToSchema(config.ingest); | ||
schemaObj['search'] = searchConfigToSchema(config.search); | ||
return yup.object(schemaObj) as WorkflowSchema; | ||
} | ||
|
||
function ingestConfigToSchema( | ||
ingestConfig: IngestConfig | undefined | ||
): ObjectSchema<any> { | ||
const ingestSchemaObj = {} as { [key: string]: Schema }; | ||
if (ingestConfig) { | ||
// TODO: implement for the other sub-categories | ||
ingestSchemaObj['enrich'] = processorsConfigToSchema(ingestConfig.enrich); | ||
ingestSchemaObj['index'] = indexConfigToSchema(ingestConfig.index); | ||
} | ||
return yup.object(ingestSchemaObj); | ||
} | ||
|
||
function processorsConfigToSchema(processorsConfig: ProcessorsConfig): Schema { | ||
const processorsSchemaObj = {} as { [key: string]: Schema }; | ||
processorsConfig.processors.forEach((processorConfig) => { | ||
const processorSchemaObj = {} as { [key: string]: Schema }; | ||
processorConfig.fields.forEach((field) => { | ||
processorSchemaObj[field.id] = getFieldSchema(field); | ||
}); | ||
processorsSchemaObj[processorConfig.id] = yup.object(processorSchemaObj); | ||
}); | ||
|
||
return yup.object(processorsSchemaObj); | ||
} | ||
|
||
function indexConfigToSchema(indexConfig: IndexConfig): Schema { | ||
const indexSchemaObj = {} as { [key: string]: Schema }; | ||
indexSchemaObj['name'] = getFieldSchema(indexConfig.name); | ||
return yup.object(indexSchemaObj); | ||
} | ||
|
||
// TODO: implement this | ||
function searchConfigToSchema( | ||
searchConfig: SearchConfig | undefined | ||
): ObjectSchema<any> { | ||
const searchSchemaObj = {} as { [key: string]: Schema }; | ||
|
||
return yup.object(searchSchemaObj); | ||
} | ||
|
||
/* | ||
**************** Yup (validation) utils ********************** | ||
*/ | ||
|
||
function getFieldSchema(field: IConfigField): Schema { | ||
let baseSchema: Schema; | ||
switch (field.type) { | ||
case 'string': | ||
case 'select': { | ||
baseSchema = yup.string().min(1, 'Too short').max(70, 'Too long'); | ||
break; | ||
} | ||
case 'model': { | ||
baseSchema = yup.object().shape({ | ||
id: yup.string().min(1, 'Too short').max(70, 'Too long').required(), | ||
category: yup.string().required(), | ||
}); | ||
break; | ||
} | ||
case 'map': { | ||
baseSchema = yup.array().of( | ||
yup.object().shape({ | ||
key: yup.string().min(1, 'Too short').max(70, 'Too long').required(), | ||
value: yup | ||
.string() | ||
.min(1, 'Too short') | ||
.max(70, 'Too long') | ||
.required(), | ||
}) | ||
); | ||
break; | ||
} | ||
case 'json': { | ||
baseSchema = yup.object().json(); | ||
break; | ||
} | ||
} | ||
|
||
// TODO: make optional schema if we support optional fields in the future | ||
// return field.optional | ||
// ? baseSchema.optional() | ||
// : baseSchema.required('Required'); | ||
|
||
return baseSchema.required('Required'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.