Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(parse): Simplify message filtering and processing logic parseResult #476

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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