2021-11-19 18:54:42 +00:00
|
|
|
import {csvDecodeRow} from 'app/common/csvFormat';
|
2021-12-16 13:45:05 +00:00
|
|
|
import {BulkColValues, CellValue, ColValues, UserAction} from 'app/common/DocActions';
|
2021-12-07 22:37:53 +00:00
|
|
|
import {DocData} from 'app/common/DocData';
|
2021-11-19 18:54:42 +00:00
|
|
|
import {DocumentSettings} from 'app/common/DocumentSettings';
|
2021-10-21 18:50:49 +00:00
|
|
|
import * as gristTypes from 'app/common/gristTypes';
|
2021-12-16 13:45:05 +00:00
|
|
|
import {getReferencedTableId, isFullReferencingType} from 'app/common/gristTypes';
|
2021-10-21 18:50:49 +00:00
|
|
|
import * as gutil from 'app/common/gutil';
|
2021-11-19 18:54:42 +00:00
|
|
|
import {safeJsonParse} from 'app/common/gutil';
|
2022-03-03 18:48:25 +00:00
|
|
|
import {NumberFormatOptions} from 'app/common/NumberFormat';
|
2021-10-21 18:50:49 +00:00
|
|
|
import NumberParse from 'app/common/NumberParse';
|
2021-11-24 22:48:37 +00:00
|
|
|
import {parseDateStrict, parseDateTime} from 'app/common/parseDate';
|
2021-12-07 22:37:53 +00:00
|
|
|
import {MetaRowRecord, TableData} from 'app/common/TableData';
|
2021-11-19 18:54:42 +00:00
|
|
|
import {DateFormatOptions, DateTimeFormatOptions, formatDecoded, FormatOptions} from 'app/common/ValueFormatter';
|
2022-01-13 10:04:56 +00:00
|
|
|
import {encodeObject} from 'app/plugin/objtypes';
|
2021-11-19 18:54:42 +00:00
|
|
|
import flatMap = require('lodash/flatMap');
|
2021-12-16 13:45:05 +00:00
|
|
|
import mapValues = require('lodash/mapValues');
|
2021-10-21 18:50:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
export class ValueParser {
|
2021-12-06 12:07:52 +00:00
|
|
|
constructor(public type: string, public widgetOpts: FormatOptions, public docSettings: DocumentSettings) {
|
2021-10-21 18:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public cleanParse(value: string): any {
|
|
|
|
if (!value) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return this.parse(value) ?? value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public parse(value: string): any {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-04 11:13:03 +00:00
|
|
|
class IdentityParser extends ValueParser {
|
|
|
|
}
|
|
|
|
|
2022-01-11 13:33:07 +00:00
|
|
|
/**
|
|
|
|
* Same as basic Value parser, but will return null if a value is an empty string.
|
|
|
|
*/
|
|
|
|
class NullIfEmptyParser extends ValueParser {
|
|
|
|
public cleanParse(value: string): any {
|
|
|
|
if (value === "") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return super.cleanParse(value);
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 18:50:49 +00:00
|
|
|
|
|
|
|
export class NumericParser extends ValueParser {
|
|
|
|
private _parse: NumberParse;
|
|
|
|
|
|
|
|
constructor(type: string, options: NumberFormatOptions, docSettings: DocumentSettings) {
|
|
|
|
super(type, options, docSettings);
|
2022-03-03 18:48:25 +00:00
|
|
|
this._parse = NumberParse.fromSettings(docSettings, options);
|
2021-10-21 18:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public parse(value: string): number | null {
|
2022-03-03 18:48:25 +00:00
|
|
|
return this._parse.parse(value)?.result ?? null;
|
2021-10-21 18:50:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DateParser extends ValueParser {
|
|
|
|
public parse(value: string): any {
|
2021-10-25 17:42:06 +00:00
|
|
|
return parseDateStrict(value, (this.widgetOpts as DateFormatOptions).dateFormat!);
|
2021-10-21 18:50:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 22:48:37 +00:00
|
|
|
class DateTimeParser extends ValueParser {
|
2021-10-21 18:50:49 +00:00
|
|
|
constructor(type: string, widgetOpts: DateTimeFormatOptions, docSettings: DocumentSettings) {
|
|
|
|
super(type, widgetOpts, docSettings);
|
|
|
|
const timezone = gutil.removePrefix(type, "DateTime:") || '';
|
|
|
|
this.widgetOpts = {...widgetOpts, timezone};
|
|
|
|
}
|
2021-11-24 22:48:37 +00:00
|
|
|
|
|
|
|
public parse(value: string): any {
|
|
|
|
return parseDateTime(value, this.widgetOpts);
|
|
|
|
}
|
2021-10-21 18:50:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 18:54:42 +00:00
|
|
|
|
|
|
|
class ChoiceListParser extends ValueParser {
|
|
|
|
public cleanParse(value: string): string[] | null {
|
|
|
|
value = value.trim();
|
|
|
|
const result = (
|
|
|
|
this._parseJson(value) ||
|
|
|
|
this._parseCsv(value)
|
|
|
|
).map(v => v.trim())
|
|
|
|
.filter(v => v);
|
|
|
|
if (!result.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return ["L", ...result];
|
|
|
|
}
|
|
|
|
|
|
|
|
private _parseJson(value: string): string[] | undefined {
|
|
|
|
// Don't parse JSON non-arrays
|
|
|
|
if (value[0] === "[") {
|
|
|
|
const arr: unknown[] | null = safeJsonParse(value, null);
|
|
|
|
return arr
|
|
|
|
// Remove nulls and empty strings
|
|
|
|
?.filter(v => v || v === 0)
|
|
|
|
// Convert values to strings, formatting nested JSON objects/arrays as JSON
|
|
|
|
.map(v => formatDecoded(v));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _parseCsv(value: string): string[] {
|
|
|
|
// Split everything on newlines which are not allowed by the choice editor.
|
|
|
|
return flatMap(value.split(/[\n\r]+/), row => {
|
|
|
|
return csvDecodeRow(row)
|
|
|
|
.map(v => v.trim());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:07:52 +00:00
|
|
|
/**
|
|
|
|
* This is different from other widget options which are simple JSON
|
|
|
|
* stored on the field. These have to be specially derived
|
2021-12-07 22:37:53 +00:00
|
|
|
* for referencing columns. See createParser.
|
2021-12-06 12:07:52 +00:00
|
|
|
*/
|
2022-02-04 11:13:03 +00:00
|
|
|
export interface ReferenceParsingOptions {
|
2021-12-06 12:07:52 +00:00
|
|
|
visibleColId: string;
|
|
|
|
visibleColType: string;
|
|
|
|
visibleColWidgetOpts: FormatOptions;
|
|
|
|
|
|
|
|
// If this is provided and loaded, the ValueParser will look up values directly.
|
|
|
|
// Otherwise an encoded lookup will be produced for the data engine to handle.
|
|
|
|
tableData?: TableData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ReferenceParser extends ValueParser {
|
|
|
|
public widgetOpts: ReferenceParsingOptions;
|
2022-02-04 11:13:03 +00:00
|
|
|
public tableData = this.widgetOpts.tableData;
|
|
|
|
public visibleColParser = createParserRaw(
|
2021-12-06 12:07:52 +00:00
|
|
|
this.widgetOpts.visibleColType,
|
|
|
|
this.widgetOpts.visibleColWidgetOpts,
|
|
|
|
this.docSettings,
|
|
|
|
);
|
|
|
|
|
2022-02-04 11:13:03 +00:00
|
|
|
protected _visibleColId = this.widgetOpts.visibleColId;
|
|
|
|
|
2021-12-06 12:07:52 +00:00
|
|
|
public parse(raw: string): any {
|
2022-02-04 11:13:03 +00:00
|
|
|
const value = this.visibleColParser.cleanParse(raw);
|
|
|
|
return this.lookup(value, raw);
|
|
|
|
}
|
|
|
|
|
|
|
|
public lookup(value: any, raw: string): any {
|
|
|
|
if (value == null || value === "" || !raw) {
|
2021-12-06 12:07:52 +00:00
|
|
|
return 0; // default value for a reference column
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._visibleColId === 'id') {
|
|
|
|
const n = Number(value);
|
|
|
|
if (Number.isInteger(n)) {
|
|
|
|
value = n;
|
|
|
|
// Don't return yet because we need to check that this row ID exists
|
|
|
|
} else {
|
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 11:13:03 +00:00
|
|
|
if (!this.tableData?.isLoaded) {
|
2021-12-06 12:07:52 +00:00
|
|
|
const options: { column: string, raw?: string } = {column: this._visibleColId};
|
|
|
|
if (value !== raw) {
|
|
|
|
options.raw = raw;
|
|
|
|
}
|
|
|
|
return ['l', value, options];
|
|
|
|
}
|
|
|
|
|
2022-02-04 11:13:03 +00:00
|
|
|
return this.tableData.findMatchingRowId({[this._visibleColId]: value}) || raw;
|
2021-12-06 12:07:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ReferenceListParser extends ReferenceParser {
|
|
|
|
public parse(raw: string): any {
|
|
|
|
let values: any[] | null;
|
|
|
|
try {
|
|
|
|
values = JSON.parse(raw);
|
|
|
|
} catch {
|
|
|
|
values = null;
|
|
|
|
}
|
|
|
|
if (!Array.isArray(values)) {
|
|
|
|
// csvDecodeRow should never raise an exception
|
|
|
|
values = csvDecodeRow(raw);
|
|
|
|
}
|
2022-02-04 11:13:03 +00:00
|
|
|
values = values.map(v => typeof v === "string" ? this.visibleColParser.cleanParse(v) : encodeObject(v));
|
2021-12-06 12:07:52 +00:00
|
|
|
|
|
|
|
if (!values.length || !raw) {
|
|
|
|
return null; // null is the default value for a reference list column
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._visibleColId === 'id') {
|
|
|
|
const numbers = values.map(Number);
|
|
|
|
if (numbers.every(Number.isInteger)) {
|
|
|
|
values = numbers;
|
|
|
|
// Don't return yet because we need to check that these row IDs exist
|
|
|
|
} else {
|
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 11:13:03 +00:00
|
|
|
if (!this.tableData?.isLoaded) {
|
2021-12-06 12:07:52 +00:00
|
|
|
const options: { column: string, raw?: string } = {column: this._visibleColId};
|
|
|
|
if (!(values.length === 1 && values[0] === raw)) {
|
|
|
|
options.raw = raw;
|
|
|
|
}
|
|
|
|
return ['l', values, options];
|
|
|
|
}
|
|
|
|
|
|
|
|
const rowIds: number[] = [];
|
|
|
|
for (const value of values) {
|
2022-02-04 11:13:03 +00:00
|
|
|
const rowId = this.tableData.findMatchingRowId({[this._visibleColId]: value});
|
2021-12-06 12:07:52 +00:00
|
|
|
if (rowId) {
|
|
|
|
rowIds.push(rowId);
|
|
|
|
} else {
|
|
|
|
// There's no matching value in the visible column, i.e. this is not a valid reference.
|
|
|
|
// We need to return a string which will become AltText.
|
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ['L', ...rowIds];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const valueParserClasses: { [type: string]: typeof ValueParser } = {
|
2022-01-11 13:33:07 +00:00
|
|
|
Any: NullIfEmptyParser,
|
2021-10-21 18:50:49 +00:00
|
|
|
Numeric: NumericParser,
|
|
|
|
Int: NumericParser,
|
|
|
|
Date: DateParser,
|
|
|
|
DateTime: DateTimeParser,
|
2021-11-19 18:54:42 +00:00
|
|
|
ChoiceList: ChoiceListParser,
|
2021-12-06 12:07:52 +00:00
|
|
|
Ref: ReferenceParser,
|
|
|
|
RefList: ReferenceListParser,
|
2021-10-21 18:50:49 +00:00
|
|
|
};
|
|
|
|
|
2021-12-06 12:07:52 +00:00
|
|
|
/**
|
2022-02-04 11:13:03 +00:00
|
|
|
* Returns a ValueParser which can parse strings into values appropriate for
|
2021-12-06 12:07:52 +00:00
|
|
|
* a specific widget field or table column.
|
|
|
|
* widgetOpts is usually the field/column's widgetOptions JSON
|
|
|
|
* but referencing columns need more than that, see ReferenceParsingOptions above.
|
|
|
|
*/
|
2021-12-07 22:37:53 +00:00
|
|
|
export function createParserRaw(
|
2021-10-21 18:50:49 +00:00
|
|
|
type: string, widgetOpts: FormatOptions, docSettings: DocumentSettings
|
2022-02-04 11:13:03 +00:00
|
|
|
): ValueParser {
|
|
|
|
const cls = valueParserClasses[gristTypes.extractTypeFromColType(type)] || IdentityParser;
|
|
|
|
return new cls(type, widgetOpts, docSettings);
|
2021-10-21 18:50:49 +00:00
|
|
|
}
|
2021-12-07 22:37:53 +00:00
|
|
|
|
|
|
|
/**
|
2022-02-04 11:13:03 +00:00
|
|
|
* Returns a ValueParser which can parse strings into values appropriate for
|
2021-12-07 22:37:53 +00:00
|
|
|
* a specific widget field or table column.
|
|
|
|
*
|
|
|
|
* Pass fieldRef (a row ID of _grist_Views_section_field) to use the settings of that view field
|
|
|
|
* instead of the table column.
|
|
|
|
*/
|
|
|
|
export function createParser(
|
|
|
|
docData: DocData,
|
|
|
|
colRef: number,
|
|
|
|
fieldRef?: number,
|
2022-02-04 11:13:03 +00:00
|
|
|
): ValueParser {
|
|
|
|
return createParserRaw(...createParserOrFormatterArguments(docData, colRef, fieldRef));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns arguments suitable for createParserRaw or createFormatter. Only for internal use.
|
|
|
|
*
|
|
|
|
* Pass fieldRef (a row ID of _grist_Views_section_field) to use the settings of that view field
|
|
|
|
* instead of the table column.
|
|
|
|
*/
|
|
|
|
export function createParserOrFormatterArguments(
|
|
|
|
docData: DocData,
|
|
|
|
colRef: number,
|
|
|
|
fieldRef?: number,
|
|
|
|
): [string, object, DocumentSettings] {
|
2021-12-07 22:37:53 +00:00
|
|
|
const columnsTable = docData.getMetaTable('_grist_Tables_column');
|
|
|
|
const fieldsTable = docData.getMetaTable('_grist_Views_section_field');
|
|
|
|
|
|
|
|
const col = columnsTable.getRecord(colRef)!;
|
|
|
|
let fieldOrCol: MetaRowRecord<'_grist_Tables_column' | '_grist_Views_section_field'> = col;
|
|
|
|
if (fieldRef) {
|
|
|
|
fieldOrCol = fieldsTable.getRecord(fieldRef) || col;
|
|
|
|
}
|
|
|
|
|
2022-02-04 11:13:03 +00:00
|
|
|
return createParserOrFormatterArgumentsRaw(docData, col.type, fieldOrCol.widgetOptions, fieldOrCol.visibleCol);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createParserOrFormatterArgumentsRaw(
|
|
|
|
docData: DocData,
|
|
|
|
type: string,
|
|
|
|
widgetOptions: string,
|
|
|
|
visibleColRef: number,
|
|
|
|
): [string, object, DocumentSettings] {
|
|
|
|
const columnsTable = docData.getMetaTable('_grist_Tables_column');
|
|
|
|
const widgetOpts = safeJsonParse(widgetOptions, {});
|
2021-12-07 22:37:53 +00:00
|
|
|
|
|
|
|
if (isFullReferencingType(type)) {
|
2022-02-04 11:13:03 +00:00
|
|
|
const vcol = columnsTable.getRecord(visibleColRef);
|
2021-12-07 22:37:53 +00:00
|
|
|
widgetOpts.visibleColId = vcol?.colId || 'id';
|
|
|
|
widgetOpts.visibleColType = vcol?.type;
|
|
|
|
widgetOpts.visibleColWidgetOpts = safeJsonParse(vcol?.widgetOptions || '', {});
|
|
|
|
widgetOpts.tableData = docData.getTable(getReferencedTableId(type)!);
|
|
|
|
}
|
|
|
|
|
2022-03-01 12:50:12 +00:00
|
|
|
return [type, widgetOpts, docData.docSettings()];
|
2021-12-07 22:37:53 +00:00
|
|
|
}
|
2021-12-16 13:45:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a copy of `colValues` with string values parsed according to the type and options of each column.
|
|
|
|
* `bulk` should be `true` if `colValues` is of type `BulkColValues`.
|
|
|
|
*/
|
|
|
|
function parseColValues<T extends ColValues | BulkColValues>(
|
|
|
|
tableId: string, colValues: T, docData: DocData, bulk: boolean
|
|
|
|
): T {
|
|
|
|
const columnsTable = docData.getMetaTable('_grist_Tables_column');
|
|
|
|
const tablesTable = docData.getMetaTable('_grist_Tables');
|
|
|
|
const tableRef = tablesTable.findRow('tableId', tableId);
|
|
|
|
if (!tableRef) {
|
|
|
|
return colValues;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapValues(colValues, (values, colId) => {
|
|
|
|
const colRef = columnsTable.findMatchingRowId({colId, parentId: tableRef});
|
|
|
|
if (!colRef) {
|
|
|
|
// Column not found - let something else deal with that
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parser = createParser(docData, colRef);
|
|
|
|
|
|
|
|
// Optimisation: If there's no special parser for this column type, do nothing
|
2022-02-04 11:13:03 +00:00
|
|
|
if (parser instanceof IdentityParser) {
|
2021-12-16 13:45:05 +00:00
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseIfString(val: any) {
|
2022-02-04 11:13:03 +00:00
|
|
|
return typeof val === "string" ? parser.cleanParse(val) : val;
|
2021-12-16 13:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bulk) {
|
|
|
|
if (!Array.isArray(values)) { // in case of bad input
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
// `colValues` is of type `BulkColValues`
|
|
|
|
return (values as CellValue[]).map(parseIfString);
|
|
|
|
} else {
|
|
|
|
// `colValues` is of type `ColValues`, `values` is just one value
|
|
|
|
return parseIfString(values);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseUserAction(ua: UserAction, docData: DocData): UserAction {
|
2022-02-11 13:10:53 +00:00
|
|
|
switch (ua[0]) {
|
|
|
|
case 'AddRecord':
|
|
|
|
case 'UpdateRecord':
|
|
|
|
return _parseUserActionColValues(ua, docData, false);
|
|
|
|
case 'BulkAddRecord':
|
|
|
|
case 'BulkUpdateRecord':
|
|
|
|
case 'ReplaceTableData':
|
|
|
|
return _parseUserActionColValues(ua, docData, true);
|
|
|
|
case 'AddOrUpdateRecord':
|
|
|
|
// Parse `require` (2) and `col_values` (3). The action looks like:
|
|
|
|
// ['AddOrUpdateRecord', table_id, require, col_values, options]
|
|
|
|
// (`col_values` is called `fields` in the API)
|
|
|
|
ua = _parseUserActionColValues(ua, docData, false, 2);
|
|
|
|
ua = _parseUserActionColValues(ua, docData, false, 3);
|
|
|
|
return ua;
|
|
|
|
default:
|
|
|
|
return ua;
|
2021-12-16 13:45:05 +00:00
|
|
|
}
|
2022-02-11 13:10:53 +00:00
|
|
|
}
|
2021-12-16 13:45:05 +00:00
|
|
|
|
2022-02-11 13:10:53 +00:00
|
|
|
// Returns a copy of the user action with one element parsed, by default the last one
|
|
|
|
function _parseUserActionColValues(ua: UserAction, docData: DocData, parseBulk: boolean, index?: number
|
|
|
|
): UserAction {
|
2021-12-16 13:45:05 +00:00
|
|
|
ua = ua.slice();
|
|
|
|
const tableId = ua[1] as string;
|
2022-02-11 13:10:53 +00:00
|
|
|
if (index === undefined) {
|
|
|
|
index = ua.length - 1;
|
|
|
|
}
|
|
|
|
const colValues = ua[index] as ColValues | BulkColValues;
|
|
|
|
ua[index] = parseColValues(tableId, colValues, docData, parseBulk);
|
2021-12-16 13:45:05 +00:00
|
|
|
return ua;
|
|
|
|
}
|