Skip to content

Commit cd26f27

Browse files
committed
Introduce raw result types
1 parent 3c98f94 commit cd26f27

3 files changed

Lines changed: 206 additions & 1 deletion

File tree

extensions/ql-vscode/src/common/bqrs-cli-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export namespace ColumnKindCode {
1313
export const ENTITY = "e";
1414
}
1515

16-
type ColumnKind =
16+
export type ColumnKind =
1717
| typeof ColumnKindCode.FLOAT
1818
| typeof ColumnKindCode.INTEGER
1919
| typeof ColumnKindCode.STRING
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import {
2+
CellValue as BqrsCellValue,
3+
ColumnKind as BqrsColumnKind,
4+
ColumnKindCode,
5+
DecodedBqrsChunk,
6+
EntityValue as BqrsEntityValue,
7+
ResultSetSchema,
8+
UrlValue as BqrsUrlValue,
9+
} from "./bqrs-cli-types";
10+
import {
11+
CellValue,
12+
Column,
13+
ColumnKind,
14+
EntityValue,
15+
RawResultSet,
16+
Tuple,
17+
UrlValue,
18+
} from "./raw-result-types";
19+
import { assertNever } from "./helpers-pure";
20+
21+
export function bqrsToResultSet(
22+
schema: ResultSetSchema,
23+
chunk: DecodedBqrsChunk,
24+
): RawResultSet {
25+
const name = schema.name;
26+
const rows = schema.rows;
27+
const nextPageOffset = chunk.next;
28+
29+
const columns = schema.columns.map(
30+
(column): Column => ({
31+
kind: mapColumnKind(column.kind),
32+
name: column.name,
33+
}),
34+
);
35+
36+
const tuples = chunk.tuples.map(
37+
(tuple): Tuple => tuple.map((cell): CellValue => mapCellValue(cell)),
38+
);
39+
40+
return {
41+
name,
42+
rows,
43+
columns,
44+
tuples,
45+
nextPageOffset,
46+
};
47+
}
48+
49+
function mapColumnKind(kind: BqrsColumnKind): ColumnKind {
50+
switch (kind) {
51+
case ColumnKindCode.STRING:
52+
return ColumnKind.String;
53+
case ColumnKindCode.FLOAT:
54+
return ColumnKind.Float;
55+
case ColumnKindCode.INTEGER:
56+
return ColumnKind.Integer;
57+
case ColumnKindCode.BOOLEAN:
58+
return ColumnKind.Boolean;
59+
case ColumnKindCode.DATE:
60+
return ColumnKind.Date;
61+
case ColumnKindCode.ENTITY:
62+
return ColumnKind.Entity;
63+
default:
64+
assertNever(kind);
65+
}
66+
}
67+
68+
function mapCellValue(cellValue: BqrsCellValue): CellValue {
69+
switch (typeof cellValue) {
70+
case "string":
71+
return {
72+
type: "string",
73+
value: cellValue,
74+
};
75+
case "number":
76+
return {
77+
type: "number",
78+
value: cellValue,
79+
};
80+
case "boolean":
81+
return {
82+
type: "boolean",
83+
value: cellValue,
84+
};
85+
case "object":
86+
return {
87+
type: "entity",
88+
value: mapEntityValue(cellValue),
89+
};
90+
}
91+
}
92+
93+
function mapEntityValue(cellValue: BqrsEntityValue): EntityValue {
94+
return {
95+
url: cellValue.url === undefined ? undefined : mapUrlValue(cellValue.url),
96+
label: cellValue.label,
97+
id: cellValue.id,
98+
};
99+
}
100+
101+
function mapUrlValue(urlValue: BqrsUrlValue): UrlValue {
102+
if (typeof urlValue === "string") {
103+
return {
104+
type: "string",
105+
value: urlValue,
106+
};
107+
}
108+
109+
if (urlValue.startLine) {
110+
return {
111+
type: "lineColumnLocation",
112+
uri: urlValue.uri,
113+
startLine: urlValue.startLine,
114+
startColumn: urlValue.startColumn,
115+
endLine: urlValue.endLine,
116+
endColumn: urlValue.endColumn,
117+
};
118+
}
119+
120+
return {
121+
type: "wholeFileLocation",
122+
uri: urlValue.uri,
123+
};
124+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
export enum ColumnKind {
2+
String = "string",
3+
Float = "float",
4+
Integer = "integer",
5+
Boolean = "boolean",
6+
Date = "date",
7+
Entity = "entity",
8+
}
9+
10+
export type Column = {
11+
name?: string;
12+
kind: ColumnKind;
13+
};
14+
15+
export type UrlValueString = {
16+
type: "string";
17+
value: string;
18+
};
19+
20+
export type UrlValueWholeFileLocation = {
21+
type: "wholeFileLocation";
22+
uri: string;
23+
};
24+
25+
export type UrlValueLineColumnLocation = {
26+
type: "lineColumnLocation";
27+
uri: string;
28+
startLine: number;
29+
startColumn: number;
30+
endLine: number;
31+
endColumn: number;
32+
};
33+
34+
export type UrlValue =
35+
| UrlValueString
36+
| UrlValueWholeFileLocation
37+
| UrlValueLineColumnLocation;
38+
39+
export type EntityValue = {
40+
url?: UrlValue;
41+
label?: string;
42+
id?: number;
43+
};
44+
45+
export type CellValueEntity = {
46+
type: "entity";
47+
value: EntityValue;
48+
};
49+
50+
export type CellValueNumber = {
51+
type: "number";
52+
value: number;
53+
};
54+
55+
export type CellValueString = {
56+
type: "string";
57+
value: string;
58+
};
59+
60+
export type CellValueBoolean = {
61+
type: "boolean";
62+
value: boolean;
63+
};
64+
65+
export type CellValue =
66+
| CellValueEntity
67+
| CellValueNumber
68+
| CellValueString
69+
| CellValueBoolean;
70+
71+
export type Tuple = CellValue[];
72+
73+
export type RawResultSet = {
74+
name: string;
75+
rows: number;
76+
77+
columns: Column[];
78+
tuples: Tuple[];
79+
80+
nextPageOffset?: number;
81+
};

0 commit comments

Comments
 (0)