Browse Source

feat: define function return types - WIP

pull/7268/head
Pranav C 11 months ago
parent
commit
814b1eed1e
  1. 4
      packages/nc-gui/components/smartsheet/column/FormulaOptions.vue
  2. 23
      packages/nc-gui/utils/formulaUtils.ts

4
packages/nc-gui/components/smartsheet/column/FormulaOptions.vue

@ -190,6 +190,7 @@ function parseAndValidateFormula(formula: string) {
} }
function validateAgainstMeta(parsedTree: any, errors = new Set(), typeErrors = new Set()) { function validateAgainstMeta(parsedTree: any, errors = new Set(), typeErrors = new Set()) {
let type: formulaTypes;
if (parsedTree.type === JSEPNode.CALL_EXP) { if (parsedTree.type === JSEPNode.CALL_EXP) {
const calleeName = parsedTree.callee.name.toUpperCase() const calleeName = parsedTree.callee.name.toUpperCase()
// validate function name // validate function name
@ -207,6 +208,7 @@ function validateAgainstMeta(parsedTree: any, errors = new Set(), typeErrors = n
errors.add(t('msg.formula.maxRequiredArgumentsFormula', { maxRequiredArguments: validation.args.max, calleeName })) errors.add(t('msg.formula.maxRequiredArgumentsFormula', { maxRequiredArguments: validation.args.max, calleeName }))
} }
} }
parsedTree.arguments.map((arg: Record<string, any>) => validateAgainstMeta(arg, errors)) parsedTree.arguments.map((arg: Record<string, any>) => validateAgainstMeta(arg, errors))
// validate data type // validate data type
@ -445,7 +447,7 @@ function validateAgainstMeta(parsedTree: any, errors = new Set(), typeErrors = n
} else { } else {
errors.add(t('msg.formula.cantSaveFieldFormulaInvalid')) errors.add(t('msg.formula.cantSaveFieldFormulaInvalid'))
} }
return errors return {errors, type}
} }
function validateAgainstType(parsedTree: any, expectedType: string, func: any, typeErrors = new Set()) { function validateAgainstType(parsedTree: any, expectedType: string, func: any, typeErrors = new Set()) {

23
packages/nc-gui/utils/formulaUtils.ts

@ -435,6 +435,17 @@ const formulas: Record<string, FormulaMeta> = {
return argsTypes[1] return argsTypes[1]
}, },
returnType: (argTypes: formulaTypes[]) => {
if (argTypes.slice(1).includes(formulaTypes.STRING)) {
return formulaTypes.STRING
} else if (argTypes.slice(1).includes(formulaTypes.NUMERIC)) {
return formulaTypes.NUMERIC
} else if (argTypes.slice(1).includes(formulaTypes.BOOLEAN)) {
return formulaTypes.BOOLEAN
}
return argTypes[1]
},
}, },
SWITCH: { SWITCH: {
type: formulaTypes.COND_EXP, type: formulaTypes.COND_EXP,
@ -453,7 +464,17 @@ const formulas: Record<string, FormulaMeta> = {
], ],
// todo: resolve return type based on the args // todo: resolve return type based on the args
returnType: (argTypes: formulaTypes[]) => { returnType: (argTypes: formulaTypes[]) => {
return formulaTypes.STRING const returnArgTypes = argTypes.slice(2).filter((_, i) => i % 2 === 1)
if (returnArgTypes.slice(1).includes(formulaTypes.STRING)) {
return formulaTypes.STRING
} else if (returnArgTypes.slice(1).includes(formulaTypes.NUMERIC)) {
return formulaTypes.NUMERIC
} else if (returnArgTypes.slice(1).includes(formulaTypes.BOOLEAN)) {
return formulaTypes.BOOLEAN
}
return returnArgTypes[1]
}, },
}, },
URL: { URL: {

Loading…
Cancel
Save