(value: RowValue | undefined)
| 13 | |
| 14 | // extractSQLRowValuePlain extracts a plain value from a RowValue. |
| 15 | export const extractSQLRowValuePlain = (value: RowValue | undefined) => { |
| 16 | if (typeof value === "undefined" || value.kind?.case === "nullValue") { |
| 17 | return null; |
| 18 | } |
| 19 | |
| 20 | const plainObject = toJson(RowValueSchema, value); |
| 21 | if (plainObject === null) { |
| 22 | return undefined; |
| 23 | } |
| 24 | const keys = Object.keys(plainObject); |
| 25 | if (keys.length === 0) { |
| 26 | return undefined; // Will be displayed as "UNSET" |
| 27 | } |
| 28 | if (keys.length > 1) { |
| 29 | console.debug("mixed type in row value", value); |
| 30 | } |
| 31 | |
| 32 | // First check if there's a formatted stringValue which should take precedence |
| 33 | if (value.kind?.case === "stringValue") { |
| 34 | let stringValue = value.kind.value; |
| 35 | if (stringValue.startsWith('"') && stringValue.endsWith('"')) { |
| 36 | stringValue = stringValue.replace(/^"|"$/g, ""); |
| 37 | } |
| 38 | return stringValue; |
| 39 | } |
| 40 | |
| 41 | // Handle binary data with auto-format detection |
| 42 | if (value.kind?.case === "bytesValue") { |
| 43 | // Ensure bytesValue exists before converting to array |
| 44 | const byteArray = Array.from(value.kind.value); |
| 45 | |
| 46 | // For single byte/bit values (could be boolean) |
| 47 | if (byteArray.length === 1) { |
| 48 | // If it's 0 or 1, display as boolean |
| 49 | if (byteArray[0] === 0 || byteArray[0] === 1) { |
| 50 | return byteArray[0] === 1 ? "true" : "false"; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Check if it's readable text |
| 55 | const isReadableText = byteArray.every( |
| 56 | (byte: number) => byte >= 32 && byte <= 126 |
| 57 | ); |
| 58 | if (isReadableText) { |
| 59 | try { |
| 60 | return new TextDecoder().decode(new Uint8Array(byteArray)); |
| 61 | } catch { |
| 62 | // If text decoding fails, fallback to hex |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // The column type isn't available in this context |
| 67 | // Default to HEX format for most binary data as it's more compact |
| 68 | return ( |
| 69 | "0x" + |
| 70 | byteArray |
| 71 | .map((byte: number) => byte.toString(16).toUpperCase().padStart(2, "0")) |
| 72 | .join("") |
no test coverage detected