|
| 1 | +import { |
| 2 | + BqrsCellValue as BqrsCellValue, |
| 3 | + BqrsColumnKind as BqrsColumnKind, |
| 4 | + BqrsColumnKindCode, |
| 5 | + DecodedBqrsChunk, |
| 6 | + BqrsEntityValue as BqrsEntityValue, |
| 7 | + BqrsLineColumnLocation, |
| 8 | + BqrsResultSetSchema, |
| 9 | + BqrsUrlValue as BqrsUrlValue, |
| 10 | + BqrsWholeFileLocation, |
| 11 | + BqrsSchemaColumn, |
| 12 | +} from "./bqrs-cli-types"; |
| 13 | +import { |
| 14 | + CellValue, |
| 15 | + Column, |
| 16 | + ColumnKind, |
| 17 | + EntityValue, |
| 18 | + RawResultSet, |
| 19 | + Row, |
| 20 | + UrlValue, |
| 21 | + UrlValueResolvable, |
| 22 | +} from "./raw-result-types"; |
| 23 | +import { assertNever } from "./helpers-pure"; |
| 24 | +import { isEmptyPath } from "./bqrs-utils"; |
| 25 | + |
| 26 | +export function bqrsToResultSet( |
| 27 | + schema: BqrsResultSetSchema, |
| 28 | + chunk: DecodedBqrsChunk, |
| 29 | +): RawResultSet { |
| 30 | + const name = schema.name; |
| 31 | + const totalRowCount = schema.rows; |
| 32 | + |
| 33 | + const columns = schema.columns.map(mapColumn); |
| 34 | + |
| 35 | + const rows = chunk.tuples.map( |
| 36 | + (tuple): Row => tuple.map((cell): CellValue => mapCellValue(cell)), |
| 37 | + ); |
| 38 | + |
| 39 | + const resultSet: RawResultSet = { |
| 40 | + name, |
| 41 | + totalRowCount, |
| 42 | + columns, |
| 43 | + rows, |
| 44 | + }; |
| 45 | + |
| 46 | + if (chunk.next) { |
| 47 | + resultSet.nextPageOffset = chunk.next; |
| 48 | + } |
| 49 | + |
| 50 | + return resultSet; |
| 51 | +} |
| 52 | + |
| 53 | +function mapColumn(column: BqrsSchemaColumn): Column { |
| 54 | + const result: Column = { |
| 55 | + kind: mapColumnKind(column.kind), |
| 56 | + }; |
| 57 | + |
| 58 | + if (column.name) { |
| 59 | + result.name = column.name; |
| 60 | + } |
| 61 | + |
| 62 | + return result; |
| 63 | +} |
| 64 | + |
| 65 | +function mapColumnKind(kind: BqrsColumnKind): ColumnKind { |
| 66 | + switch (kind) { |
| 67 | + case BqrsColumnKindCode.STRING: |
| 68 | + return ColumnKind.String; |
| 69 | + case BqrsColumnKindCode.FLOAT: |
| 70 | + return ColumnKind.Float; |
| 71 | + case BqrsColumnKindCode.INTEGER: |
| 72 | + return ColumnKind.Integer; |
| 73 | + case BqrsColumnKindCode.BOOLEAN: |
| 74 | + return ColumnKind.Boolean; |
| 75 | + case BqrsColumnKindCode.DATE: |
| 76 | + return ColumnKind.Date; |
| 77 | + case BqrsColumnKindCode.ENTITY: |
| 78 | + return ColumnKind.Entity; |
| 79 | + default: |
| 80 | + assertNever(kind); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function mapCellValue(cellValue: BqrsCellValue): CellValue { |
| 85 | + switch (typeof cellValue) { |
| 86 | + case "string": |
| 87 | + return { |
| 88 | + type: "string", |
| 89 | + value: cellValue, |
| 90 | + }; |
| 91 | + case "number": |
| 92 | + return { |
| 93 | + type: "number", |
| 94 | + value: cellValue, |
| 95 | + }; |
| 96 | + case "boolean": |
| 97 | + return { |
| 98 | + type: "boolean", |
| 99 | + value: cellValue, |
| 100 | + }; |
| 101 | + case "object": |
| 102 | + return { |
| 103 | + type: "entity", |
| 104 | + value: mapEntityValue(cellValue), |
| 105 | + }; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +function mapEntityValue(cellValue: BqrsEntityValue): EntityValue { |
| 110 | + const result: EntityValue = {}; |
| 111 | + |
| 112 | + if (cellValue.id) { |
| 113 | + result.id = cellValue.id; |
| 114 | + } |
| 115 | + if (cellValue.label) { |
| 116 | + result.label = cellValue.label; |
| 117 | + } |
| 118 | + if (cellValue.url) { |
| 119 | + result.url = mapUrlValue(cellValue.url); |
| 120 | + } |
| 121 | + |
| 122 | + return result; |
| 123 | +} |
| 124 | + |
| 125 | +export function mapUrlValue(urlValue: BqrsUrlValue): UrlValue | undefined { |
| 126 | + if (typeof urlValue === "string") { |
| 127 | + const location = tryGetLocationFromString(urlValue); |
| 128 | + if (location !== undefined) { |
| 129 | + return location; |
| 130 | + } |
| 131 | + |
| 132 | + return { |
| 133 | + type: "string", |
| 134 | + value: urlValue, |
| 135 | + }; |
| 136 | + } |
| 137 | + |
| 138 | + if (isWholeFileLoc(urlValue)) { |
| 139 | + return { |
| 140 | + type: "wholeFileLocation", |
| 141 | + uri: urlValue.uri, |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + if (isLineColumnLoc(urlValue)) { |
| 146 | + return { |
| 147 | + type: "lineColumnLocation", |
| 148 | + uri: urlValue.uri, |
| 149 | + startLine: urlValue.startLine, |
| 150 | + startColumn: urlValue.startColumn, |
| 151 | + endLine: urlValue.endLine, |
| 152 | + endColumn: urlValue.endColumn, |
| 153 | + }; |
| 154 | + } |
| 155 | + |
| 156 | + return undefined; |
| 157 | +} |
| 158 | + |
| 159 | +function isLineColumnLoc(loc: BqrsUrlValue): loc is BqrsLineColumnLocation { |
| 160 | + return ( |
| 161 | + typeof loc !== "string" && |
| 162 | + !isEmptyPath(loc.uri) && |
| 163 | + "startLine" in loc && |
| 164 | + "startColumn" in loc && |
| 165 | + "endLine" in loc && |
| 166 | + "endColumn" in loc |
| 167 | + ); |
| 168 | +} |
| 169 | + |
| 170 | +function isWholeFileLoc(loc: BqrsUrlValue): loc is BqrsWholeFileLocation { |
| 171 | + return ( |
| 172 | + typeof loc !== "string" && !isEmptyPath(loc.uri) && !isLineColumnLoc(loc) |
| 173 | + ); |
| 174 | +} |
| 175 | + |
| 176 | +/** |
| 177 | + * The CodeQL filesystem libraries use this pattern in `getURL()` predicates |
| 178 | + * to describe the location of an entire filesystem resource. |
| 179 | + * Such locations appear as `StringLocation`s instead of `FivePartLocation`s. |
| 180 | + * |
| 181 | + * Folder resources also get similar URLs, but with the `folder` scheme. |
| 182 | + * They are deliberately ignored here, since there is no suitable location to show the user. |
| 183 | + */ |
| 184 | +const FILE_LOCATION_REGEX = /file:\/\/(.+):([0-9]+):([0-9]+):([0-9]+):([0-9]+)/; |
| 185 | + |
| 186 | +function tryGetLocationFromString(loc: string): UrlValueResolvable | undefined { |
| 187 | + const matches = FILE_LOCATION_REGEX.exec(loc); |
| 188 | + if (matches && matches.length > 1 && matches[1]) { |
| 189 | + if (isWholeFileMatch(matches)) { |
| 190 | + return { |
| 191 | + type: "wholeFileLocation", |
| 192 | + uri: matches[1], |
| 193 | + }; |
| 194 | + } else { |
| 195 | + return { |
| 196 | + type: "lineColumnLocation", |
| 197 | + uri: matches[1], |
| 198 | + startLine: Number(matches[2]), |
| 199 | + startColumn: Number(matches[3]), |
| 200 | + endLine: Number(matches[4]), |
| 201 | + endColumn: Number(matches[5]), |
| 202 | + }; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + return undefined; |
| 207 | +} |
| 208 | + |
| 209 | +function isWholeFileMatch(matches: RegExpExecArray): boolean { |
| 210 | + return ( |
| 211 | + matches[2] === "0" && |
| 212 | + matches[3] === "0" && |
| 213 | + matches[4] === "0" && |
| 214 | + matches[5] === "0" |
| 215 | + ); |
| 216 | +} |
0 commit comments