-
-
Notifications
You must be signed in to change notification settings - Fork 1
Implement XDR encoder with separated schema validation and comprehensive tests #54
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
Conversation
…tests Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
|
…n, add XdrUnion, fix quadruple Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Implemented all requested changes: XdrEncoder changes:
XdrSchemaEncoder changes:
Additional: |
streamich
left a comment
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.
- Address review comments
- Make sure
yarn testpasses - Make sure to run
yarn prettierin the end
| /** | ||
| * Writes an XDR signed 64-bit integer (hyper) in big-endian format. | ||
| */ | ||
| public writeHyper(hyper: number | bigint): void { |
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.
Use DataView.prototype.setBigInt64() here.
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.
Updated to use DataView.prototype.setBigInt64() for cleaner bigint encoding (d10904e)
| /** | ||
| * Writes an XDR unsigned 64-bit integer (unsigned hyper) in big-endian format. | ||
| */ | ||
| public writeUnsignedHyper(uhyper: number | bigint): void { |
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.
Use DataView.prototype.setBigUint64() here.
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.
Updated to use DataView.prototype.setBigUint64() for cleaner unsigned bigint encoding (d10904e)
src/xdr/XdrEncoder.ts
Outdated
| public writeOpaque(data: Uint8Array, size: number): void { | ||
| if (data.length !== size) { | ||
| throw new Error(`Opaque data length ${data.length} does not match expected size ${size}`); | ||
| } |
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.
Remove this check and size property.
| public writeOpaque(data: Uint8Array, size: number): void { | |
| if (data.length !== size) { | |
| throw new Error(`Opaque data length ${data.length} does not match expected size ${size}`); | |
| } | |
| public writeOpaque(data: Uint8Array): void { | |
| const size = data.length; |
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.
Removed size check and parameter from writeOpaque() - now takes only data with automatic length detection (d10904e)
src/xdr/XdrEncoder.ts
Outdated
|
|
||
| const writer = this.writer; | ||
| const paddedSize = Math.ceil(data.length / 4) * 4; | ||
| writer.ensureCapacity(paddedSize); | ||
|
|
||
| // Write data | ||
| writer.buf(data, data.length); | ||
|
|
||
| // Write padding bytes | ||
| const padding = paddedSize - data.length; | ||
| for (let i = 0; i < padding; i++) { | ||
| writer.u8(0); | ||
| } | ||
| } |
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.
Reuse .writeOpaque() here if possible:
| const writer = this.writer; | |
| const paddedSize = Math.ceil(data.length / 4) * 4; | |
| writer.ensureCapacity(paddedSize); | |
| // Write data | |
| writer.buf(data, data.length); | |
| // Write padding bytes | |
| const padding = paddedSize - data.length; | |
| for (let i = 0; i < padding; i++) { | |
| writer.u8(0); | |
| } | |
| } | |
| this.writeOpaque(data); | |
| } |
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.
Simplified writeVarlenOpaque() to reuse writeOpaque() method (d10904e)
src/xdr/XdrEncoder.ts
Outdated
| } | ||
|
|
||
| public writeArr(arr: unknown[]): void { | ||
| throw new Error('writeArr not implemented in XDR encoder'); |
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.
| throw new Error('writeArr not implemented in XDR encoder'); | |
| throw new Error('not implemented'); |
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.
Fixed error message to be consistent: 'not implemented' (d10904e)
src/xdr/XdrEncoder.ts
Outdated
| } | ||
|
|
||
| public writeObj(obj: Record<string, unknown>): void { | ||
| throw new Error('writeObj not implemented in XDR encoder'); |
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.
| throw new Error('writeObj not implemented in XDR encoder'); | |
| throw new Error('not implemented'); |
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.
Fixed error message to be consistent: 'not implemented' (d10904e)
src/xdr/XdrSchemaEncoder.ts
Outdated
|
|
||
| export class XdrSchemaEncoder { | ||
| private encoder: XdrEncoder; | ||
| private validator: XdrSchemaValidator; |
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.
Remove validator from this encoder, assume that the schema is valid.
| private validator: XdrSchemaValidator; |
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.
Removed validator from XdrSchemaEncoder - now assumes schema is valid for better performance (d10904e)
…fy opaque handling, fix error messages Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Addressed all review comments, tests pass, and ran prettier (d10904e): ✅ Used native |
|
🎉 This PR is included in version 1.13.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Addressed review comments from @streamich:
Changes Made:
XdrEncoder improvements:
DataView.prototype.setBigInt64()for hyper encoding instead of manual bit manipulationDataView.prototype.setBigUint64()for unsigned hyper encodingwriteOpaque()method - now takes only datawriteOpaque()inwriteVarlenOpaque()for code simplificationwriteArr()andwriteObj()to be consistent:'not implemented'XdrSchemaEncoder improvements:
writeOpaque()call to match new signature without size parameterTest updates:
writeOpaque()callsQuality assurance:
The implementation now uses native DataView BigInt methods for better performance and has cleaner, more maintainable code with the validator properly separated.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.