Skip to content

Commit

Permalink
refactor(parse): Simplify message filtering and processing logic `par…
Browse files Browse the repository at this point in the history
…seResult` (#476)

* refactor(pglite/parse): Simplify message filtering and processing logic in parseResults

* refactor(pglite/parse): remove semicolon

* Tweeks and a cheageset

---------

Co-authored-by: Sam Willis <[email protected]>
  • Loading branch information
jeet-dhandha and samwillis authored Jan 13, 2025
1 parent 7ce9f04 commit f3f1103
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-eagles-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@electric-sql/pglite': patch
---

Refactor the protocol message parse code to be simpler and easer to follow
95 changes: 56 additions & 39 deletions packages/pglite/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,52 +23,69 @@ export function parseResults(
let affectedRows = 0
const parsers = { ...defaultParsers, ...options?.parsers }

const filteredMessages = messages.filter(
(msg) =>
msg.name === 'rowDescription' ||
msg.name === 'dataRow' ||
msg.name === 'commandComplete',
const processMessageTypes = new Set([
'rowDescription',
'dataRow',
'commandComplete',
])

const filteredMessages = messages.filter((msg) =>
processMessageTypes.has(msg.name),
)

filteredMessages.forEach((message, index) => {
if (message.name === 'rowDescription') {
const msg = message as RowDescriptionMessage
currentResultSet.fields = msg.fields.map((field) => ({
name: field.name,
dataTypeID: field.dataTypeID,
}))
} else if (message.name === 'dataRow' && currentResultSet) {
const msg = message as DataRowMessage
if (options?.rowMode === 'array') {
currentResultSet.rows.push(
msg.fields.map((field, i) =>
parseType(field, currentResultSet!.fields[i].dataTypeID, parsers),
),
)
} else {
// rowMode === "object"
currentResultSet.rows.push(
Object.fromEntries(
msg.fields.map((field, i) => [
currentResultSet!.fields[i].name,
switch (message.name) {
case 'rowDescription': {
const msg = message as RowDescriptionMessage
currentResultSet.fields = msg.fields.map((field) => ({
name: field.name,
dataTypeID: field.dataTypeID,
}))
break
}
case 'dataRow': {
if (!currentResultSet) break
const msg = message as DataRowMessage
if (options?.rowMode === 'array') {
currentResultSet.rows.push(
msg.fields.map((field, i) =>
parseType(field, currentResultSet!.fields[i].dataTypeID, parsers),
]),
),
)
),
)
} else {
// rowMode === "object"
currentResultSet.rows.push(
Object.fromEntries(
msg.fields.map((field, i) => [
currentResultSet!.fields[i].name,
parseType(
field,
currentResultSet!.fields[i].dataTypeID,
parsers,
),
]),
),
)
}
break
}
} else if (message.name === 'commandComplete') {
const msg = message as CommandCompleteMessage
affectedRows += retrieveRowCount(msg)
case 'commandComplete': {
const msg = message as CommandCompleteMessage
affectedRows += retrieveRowCount(msg)

if (index === filteredMessages.length - 1)
resultSets.push({
...currentResultSet,
affectedRows,
...(blob ? { blob } : {}),
})
else resultSets.push(currentResultSet)
if (index === filteredMessages.length - 1) {
resultSets.push({
...currentResultSet,
affectedRows,
...(blob ? { blob } : {}),
})
} else {
resultSets.push(currentResultSet)
}

currentResultSet = { rows: [], fields: [] }
currentResultSet = { rows: [], fields: [] }
break
}
}
})

Expand Down

0 comments on commit f3f1103

Please sign in to comment.