Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cell response enum work #2051

Open
wants to merge 1 commit into
base: data-tables-v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion quadratic-client/src/app/quadratic-core-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type CellBorderLine = "line1" | "line2" | "line3" | "dotted" | "dashed" |
export interface CellFormatSummary { bold: boolean | null, italic: boolean | null, commas: boolean | null, textColor: string | null, fillColor: string | null, align: CellAlign | null, verticalAlign: CellVerticalAlign | null, wrap: CellWrap | null, dateTime: string | null, cellType: CellType | null, underline: boolean | null, strikeThrough: boolean | null, }
export interface CellRef { sheet: string | null, x: CellRefCoord, y: CellRefCoord, }
export type CellRefCoord = { "type": "Relative", "coord": bigint } | { "type": "Absolute", "coord": bigint };
export type CellValueType = "Blank" | "Text" | "Number" | "Boolean" | "DateTime" | "Date" | "Time" | "Duration" | "Error" | "Html" | "Code" | "Image" | "Import";
export type CellVerticalAlign = "top" | "middle" | "bottom";
export type CellWrap = "overflow" | "wrap" | "clip";
export type CodeCellLanguage = "Python" | "Formula" | { "Connection": { kind: ConnectionKind, id: string, } } | "Javascript" | "Import";
Expand All @@ -30,7 +31,7 @@ export interface JsClipboard { plainText: string, html: string, }
export interface JsCodeCell { x: bigint, y: bigint, code_string: string, language: CodeCellLanguage, std_out: string | null, std_err: string | null, evaluation_result: string | null, spill_error: Array<Pos> | null, return_info: JsReturnInfo | null, cells_accessed: Array<SheetRect> | null, }
export interface JsCodeResult { transaction_id: string, success: boolean, std_out: string | null, std_err: string | null, line_number: number | null, output_value: Array<string> | null, output_array: Array<Array<Array<string>>> | null, output_display_type: string | null, cancel_compute: boolean | null, }
export interface JsDataTableColumn { name: string, display: boolean, valueIndex: number, }
export interface JsGetCellResponse { x: bigint, y: bigint, value: string, type_name: string, }
export interface JsGetCellResponse { x: bigint, y: bigint, value: string, type_name: CellValueType, }
export interface JsHtmlOutput { sheet_id: string, x: bigint, y: bigint, html: string | null, w: string | null, h: string | null, }
export interface JsNumber { decimals: number | null, commas: boolean | null, format: NumericFormat | null, }
export interface JsOffset { column: number | null, row: number | null, size: number, }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// (x,y) position with the code, so `pos()` and `relCell()` can be calculated
// within the worker using getCells.

import { JsGetCellResponse } from '@/app/quadratic-core-types';
import { CellValueType } from '@/app/web-workers/javascriptWebWorker/worker/javascript/javascriptOutput';
import { javascriptClient } from '../javascriptClient';
import { javascriptCore } from '../javascriptCore';
import { Javascript } from './javascript';
Expand All @@ -19,7 +19,7 @@ export class JavascriptAPI {
}

private convertType(entry: JsGetCellResponse): CellType | undefined {
if (entry.type_name === 'blank') return undefined;
if (entry.type_name === CellValueType.Blank) return undefined;
if (entry.type_name === 'date time' || entry.type_name === 'date')
return `___date___${new Date(entry.value).getTime()}`;
return entry.type_name === 'number' ? parseFloat(entry.value) : entry.value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { javascriptConvertOutputArray, javascriptConvertOutputType } from './javascriptOutput';
import { CellValueType, javascriptConvertOutputArray, javascriptConvertOutputType } from './javascriptOutput';

describe('javascriptConvertOutputType', () => {
it('should convert numbers', () => {
Expand Down Expand Up @@ -38,7 +38,7 @@ describe('javascriptConvertOutputType', () => {
message = [];
expect(javascriptConvertOutputType(message, 'hello', 0, 0)).toEqual({
displayType: 'string',
output: ['hello', 'text'],
output: ['hello', CellValueType.Text],
});
expect(message.length).toBe(0);

Expand All @@ -47,7 +47,7 @@ describe('javascriptConvertOutputType', () => {

expect(javascriptConvertOutputType(message, true, 0, 0)).toEqual({
displayType: 'boolean',
output: ['true', 'logical'],
output: ['true', CellValueType.Boolean],
});
expect(message.length).toBe(0);

Expand All @@ -56,13 +56,13 @@ describe('javascriptConvertOutputType', () => {

expect(javascriptConvertOutputType(message, [], 0, 0)).toEqual({
displayType: 'empty array',
output: ['', 'array'],
output: ['', CellValueType.Blank],
});
expect(message.length).toBe(0);

expect(javascriptConvertOutputType(message, [[], [], []], 0, 0)).toEqual({
displayType: 'empty array',
output: ['', 'array'],
output: ['', CellValueType.Blank],
});
expect(message.length).toBe(0);
});
Expand All @@ -71,7 +71,7 @@ describe('javascriptConvertOutputType', () => {
let message: string[] = [];
expect(javascriptConvertOutputArray(message, [1, 2, 3], 0, 0)).toEqual({
displayType: 'number[]',
output: [[['1', 'number']], [['2', 'number']], [['3', 'number']]],
output: [[['1', CellValueType.Number]], [['2', CellValueType.Number]], [['3', CellValueType.Number]]],
});
expect(message.length).toBe(0);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
// Converts the Javascript output to the Rust format and the
// display type for use in the Code Editor.

export enum CellValueType {
Blank = 0,
Text = 1,
Number = 2,
Boolean = 3,
DateTime = 4,
Date = 5,
Time = 6,
Duration = 7,
Error = 8,
Html = 9,
Code = 10,
Image = 11,
Import = 12,
}

// Converts a single cell output and sets the displayType.
export function javascriptConvertOutputType(
message: string[],
Expand All @@ -9,7 +25,7 @@ export function javascriptConvertOutputType(
row: number,
x?: number,
y?: number
): { output: [string, string]; displayType: string } | null {
): { output: [string, CellValueType]; displayType: string } | null {
if (Array.isArray(value) && value.flat().length !== 0) {
return null;
}
Expand All @@ -23,7 +39,7 @@ export function javascriptConvertOutputType(
message.push(`Warning: Unsupported output type: 'Infinity' at cell(${column + (x ?? 0)}, ${row + (y ?? 0)})`);
return null;
}
return { output: [value.toString(), 'number'], displayType: 'number' };
return { output: [value.toString(), CellValueType.Number], displayType: 'number' };
} else if (typeof value === 'string' && value.includes('[object Promise]')) {
message.push(
`WARNING: Unsupported output type: \`Promise\` at cell(${column + (x ?? 0)}, ${
Expand All @@ -40,22 +56,22 @@ export function javascriptConvertOutputType(
);
return null;
}
return { output: [value.toISOString(), 'date time'], displayType: 'Date' };
return { output: [value.toISOString(), CellValueType.DateTime], displayType: 'Date' };
} else if (typeof value === 'function') {
message.push(`WARNING: Unsupported output type: 'function' at cell(${column + (x ?? 0)}, ${row + (y ?? 0)})`);
return null;
} else if (value instanceof Blob && (value as Blob).type.includes('image')) {
const image = new FileReaderSync().readAsDataURL(value as Blob);
return { output: [image, 'image'], displayType: 'OffscreenCanvas' };
return { output: [image, CellValueType.Image], displayType: 'OffscreenCanvas' };
} else if (typeof value === 'string') {
return { output: [value, 'text'], displayType: 'string' };
return { output: [value, CellValueType.Text], displayType: 'string' };
} else if (value === undefined) {
return null;
} else if (typeof value === 'boolean') {
return { output: [value ? 'true' : 'false', 'logical'], displayType: 'boolean' };
return { output: [value ? 'true' : 'false', CellValueType.Boolean], displayType: 'boolean' };
} else if (Array.isArray(value)) {
// this handles the case where the value.flat() is empty
return { output: ['', 'array'], displayType: 'empty array' };
return { output: ['', CellValueType.Blank], displayType: 'empty array' };
} else {
message.push(
`WARNING: Unsupported output type "${typeof value}" at cell(${column + (x ?? 0)}, ${
Expand Down Expand Up @@ -85,17 +101,17 @@ export function javascriptConvertOutputArray(
value: any,
column: number,
row: number
): { output: [string, string][][]; displayType: string } | null {
): { output: [string, CellValueType][][]; displayType: string } | null {
if (!Array.isArray(value) || value.length === 0 || value.flat().length === 0) {
return null;
}
const types: Set<string> = new Set();
const output: [string, string][][] = [];
const output: [string, CellValueType][][] = [];

// It may be an array of objects, where the object name is the heading row.
if (!Array.isArray(value[0]) && typeof value[0] === 'object' && !isExpectedObjectType(value[0])) {
const keys = Object.keys(value[0]);
output.push(keys.map((key) => [key, 'text']));
output.push(keys.map((key) => [key, CellValueType.Text]));
for (const [y, v] of value.entries()) {
const rowEntry: any[] = [];
output.push(rowEntry);
Expand All @@ -105,7 +121,7 @@ export function javascriptConvertOutputArray(
types.add(outputValue.displayType);
rowEntry.push(outputValue.output);
} else {
rowEntry.push(['', 'blank']);
rowEntry.push(['', CellValueType.Blank]);
}
}
}
Expand All @@ -120,7 +136,7 @@ export function javascriptConvertOutputArray(
types.add(outputValue.displayType);
output.push([outputValue.output]);
} else {
output.push([['', 'blank']]);
output.push([['', CellValueType.Blank]]);
}
}
}
Expand All @@ -132,7 +148,7 @@ export function javascriptConvertOutputArray(
output.push([]);
for (let i = 0; i < longest; i++) {
if (v.length <= i) {
output[y].push(['', 'blank']);
output[y].push(['', CellValueType.Blank]);
types.add('undefined');
} else {
const v2 = v[i];
Expand All @@ -141,7 +157,7 @@ export function javascriptConvertOutputArray(
types.add(outputValue.displayType);
output[y].push(outputValue.output);
} else {
output[y].push(['', 'blank']);
output[y].push(['', CellValueType.Blank]);
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions quadratic-core/src/bin/export_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use grid::{
};
use quadratic_core::color::Rgba;
use quadratic_core::controller::active_transactions::transaction_name::TransactionName;
use quadratic_core::controller::execution::run_code::get_cells::JsGetCellResponse;
use quadratic_core::controller::transaction_types::JsCodeResult;
use quadratic_core::grid::js_types::{
JsCodeCell, JsHtmlOutput, JsNumber, JsRenderCell, JsRenderCellSpecial, JsRenderCodeCell,
Expand Down Expand Up @@ -96,7 +95,6 @@ fn main() {
JsCodeCell,
JsCodeResult,
JsDataTableColumn,
JsGetCellResponse,
JsHtmlOutput,
JsNumber,
JsOffset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ mod tests {
use bigdecimal::BigDecimal;
use serial_test::serial;

use crate::cellvalue::CellValueType;
use crate::controller::active_transactions::pending_transaction::PendingTransaction;
use crate::controller::execution::run_code::get_cells::JsGetCellResponse;
use crate::controller::operations::operation::Operation;
Expand Down Expand Up @@ -499,7 +500,7 @@ mod tests {
x: 0,
y: 0,
value: "9".into(),
type_name: "number".into(),
type_name: CellValueType::Number,
}])
);
// pending cal
Expand Down
18 changes: 11 additions & 7 deletions quadratic-core/src/controller/execution/run_code/get_cells.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use uuid::Uuid;

use crate::{controller::GridController, error_core::CoreError, Rect, RunError, RunErrorMsg};
use crate::{
cellvalue::CellValueType, controller::GridController, error_core::CoreError, Rect, RunError,
RunErrorMsg,
};
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::wasm_bindgen;

#[wasm_bindgen]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "js", derive(ts_rs::TS))]
pub struct JsGetCellResponse {
pub x: i64,
pub y: i64,
pub value: String,
pub type_name: String,
pub type_name: CellValueType,
}

impl GridController {
Expand Down Expand Up @@ -274,7 +278,7 @@ mod test {
x: 0,
y: 0,
value: "test".into(),
type_name: "text".into()
type_name: CellValueType::Text
}])
);
}
Expand Down Expand Up @@ -344,19 +348,19 @@ mod test {
x: 0,
y: 0,
value: "test1".into(),
type_name: "text".into()
type_name: CellValueType::Text
},
JsGetCellResponse {
x: 0,
y: 1,
value: "test2".into(),
type_name: "text".into()
type_name: CellValueType::Text
},
JsGetCellResponse {
x: 0,
y: 2,
value: "test3".into(),
type_name: "text".into()
type_name: CellValueType::Text
}
])
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl GridController {
mod tests {
use super::*;
use crate::{
cellvalue::CellValueType,
controller::{
execution::run_code::get_cells::JsGetCellResponse, transaction_types::JsCodeResult,
},
Expand Down Expand Up @@ -168,7 +169,7 @@ mod tests {
x: 0,
y: 0,
value: "9".into(),
type_name: "number".into(),
type_name: CellValueType::Number
}])
);

Expand Down Expand Up @@ -264,7 +265,7 @@ mod tests {
x: 0,
y: 0,
value: "10".into(),
type_name: "number".into(),
type_name: CellValueType::Number
}])
);
assert!(gc
Expand Down Expand Up @@ -552,7 +553,7 @@ mod tests {
x: 0,
y: 0,
value: "1".into(),
type_name: "number".into(),
type_name: CellValueType::Number,
}
);
let result = gc.calculation_complete(JsCodeResult::new(
Expand Down Expand Up @@ -593,7 +594,7 @@ mod tests {
x: 0,
y: 1,
value: "2".into(),
type_name: "number".into(),
type_name: CellValueType::Number,
}
);
let result = gc.calculation_complete(JsCodeResult::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl GridController {
mod tests {
use super::*;
use crate::{
cellvalue::CellValueType,
controller::{
execution::run_code::get_cells::JsGetCellResponse, transaction_types::JsCodeResult,
},
Expand Down Expand Up @@ -176,7 +177,7 @@ mod tests {
x: 0,
y: 0,
value: "9".into(),
type_name: "number".into(),
type_name: CellValueType::Number,
}])
);

Expand Down Expand Up @@ -276,7 +277,7 @@ mod tests {
x: 0,
y: 0,
value: "10".into(),
type_name: "number".into(),
type_name: CellValueType::Number,
}])
);
assert!(gc
Expand Down Expand Up @@ -579,7 +580,7 @@ mod tests {
x: 0,
y: 0,
value: "1".into(),
type_name: "number".into(),
type_name: CellValueType::Number,
}
);
let result = gc.calculation_complete(JsCodeResult::new(
Expand Down Expand Up @@ -620,7 +621,7 @@ mod tests {
x: 0,
y: 1,
value: "2".into(),
type_name: "number".into(),
type_name: CellValueType::Number,
}
);
let result = gc.calculation_complete(JsCodeResult::new(
Expand Down
Loading