-
Notifications
You must be signed in to change notification settings - Fork 8
feat(nodejs): array support #50
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
Open
glasstiger
wants to merge
6
commits into
binary_protocol_arrays
Choose a base branch
from
arrays_support
base: binary_protocol_arrays
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c7b2525
feat(nodejs): array support
glasstiger 46664d0
test unsupported array types
glasstiger 2cdf4e5
more tests, better error message
glasstiger ab6f968
array validation
glasstiger 165d827
Merge branch 'binary_protocol_arrays' into arrays_support
glasstiger 505a9bb
add back scheduled run of build
glasstiger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ on: | |
branches: | ||
- main | ||
pull_request: | ||
schedule: | ||
- cron: '15 2,10,18 * * *' | ||
|
||
jobs: | ||
test: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,3 +1,5 @@ | ||||||
type ArrayPrimitive = "number" | "boolean" | "string" | null; | ||||||
|
||||||
type TimestampUnit = "ns" | "us" | "ms"; | ||||||
|
||||||
function isBoolean(value: unknown): value is boolean { | ||||||
|
@@ -36,6 +38,73 @@ function timestampToNanos(timestamp: bigint, unit: TimestampUnit) { | |||||
} | ||||||
} | ||||||
|
||||||
function getDimensions(data: unknown) { | ||||||
const dimensions: number[] = []; | ||||||
while (Array.isArray(data)) { | ||||||
if (data.length === 0) { | ||||||
throw new Error("Zero length array not supported"); | ||||||
} | ||||||
dimensions.push(data.length); | ||||||
data = data[0]; | ||||||
} | ||||||
return dimensions; | ||||||
} | ||||||
|
||||||
function validateArray(data: unknown[], dimensions: number[]): ArrayPrimitive { | ||||||
if (data === null || data === undefined) { | ||||||
return null; | ||||||
} | ||||||
if (!Array.isArray(data)) { | ||||||
throw new Error( | ||||||
`The value must be an array [value=${JSON.stringify(data)}, type=${typeof data}]`, | ||||||
); | ||||||
} | ||||||
|
||||||
let expectedType: ArrayPrimitive = null; | ||||||
|
||||||
function checkArray( | ||||||
array: unknown[], | ||||||
depth: number = 0, | ||||||
path: string = "", | ||||||
): void { | ||||||
const expectedLength = dimensions[depth]; | ||||||
if (array.length !== expectedLength) { | ||||||
throw new Error( | ||||||
`Length of arrays do not match [expected=${expectedLength}, actual=${array.length}, dimensions=[${dimensions}], path=${path}]`, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||
); | ||||||
} | ||||||
|
||||||
if (depth < dimensions.length - 1) { | ||||||
// intermediate level, expecting arrays | ||||||
for (let i = 0; i < array.length; i++) { | ||||||
if (!Array.isArray(array[i])) { | ||||||
throw new Error( | ||||||
`Mixed types found [expected=array, current=${typeof array[i]}, path=${path}[${i}]]`, | ||||||
); | ||||||
} | ||||||
checkArray(array[i] as unknown[], depth + 1, `${path}[${i}]`); | ||||||
} | ||||||
} else { | ||||||
// leaf level, expecting primitives | ||||||
if (expectedType === null) { | ||||||
expectedType = typeof array[0] as ArrayPrimitive; | ||||||
} | ||||||
|
||||||
for (let i = 0; i < array.length; i++) { | ||||||
const currentType = typeof array[i] as ArrayPrimitive; | ||||||
if (currentType !== expectedType) { | ||||||
throw new Error( | ||||||
`Mixed types found [expected=${expectedType}, current=${currentType}, path=${path}[${i}]]`, | ||||||
); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
checkArray(data); | ||||||
return expectedType; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Fetches JSON data from a URL. | ||||||
* @template T - The expected type of the JSON response | ||||||
|
@@ -66,4 +135,7 @@ export { | |||||
timestampToNanos, | ||||||
TimestampUnit, | ||||||
fetchJson, | ||||||
getDimensions, | ||||||
validateArray, | ||||||
ArrayPrimitive, | ||||||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: considering that arrays are V2-only, it makes sense to move
writeArray
and related methods tobufferv2.ts
instead ofbase.ts