|
| 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 | +} |
0 commit comments