Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 16, 2025

Addressed review comments from @streamich:

Changes Made:

XdrEncoder improvements:

  • Use DataView.prototype.setBigInt64() for hyper encoding instead of manual bit manipulation
  • Use DataView.prototype.setBigUint64() for unsigned hyper encoding
  • Removed size check and parameter from writeOpaque() method - now takes only data
  • Reused writeOpaque() in writeVarlenOpaque() for code simplification
  • Fixed error messages in writeArr() and writeObj() to be consistent: 'not implemented'

XdrSchemaEncoder improvements:

  • Removed validator from encoder - assumes schema is valid for better performance
  • Updated writeOpaque() call to match new signature without size parameter

Test updates:

  • Updated XdrEncoder tests to remove size parameter from writeOpaque() calls
  • Removed test that checked size mismatch error (no longer applicable)

Quality assurance:

  • All 152 XDR tests pass
  • All 3061 total tests pass
  • Code formatted with prettier
  • Maintains backward compatibility for schema encoder interface

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.

…tests

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement XDR encoder Implement XDR encoder with schema validation and comprehensive tests Sep 16, 2025
Copilot AI requested a review from streamich September 16, 2025 10:28
Copilot finished work on behalf of streamich September 16, 2025 10:28
@streamich streamich marked this pull request as ready for review September 16, 2025 10:29
@streamich
Copy link
Collaborator

@copilot

  • In XdrEncoder:
    • In writeQuadruple implementation throw Error('not implemented')
    • In writeStr() use existing UTF-8 encoding method on Writer class, see how other users of Writer do that.
    • Inline .getPaddedSize() method.
    • Remove .writeArr() and .writeObj() methods.
  • In XdrSchemaEncoder:
    • Remove all schema validation, users will use XdrSchemaValidator separately if they need schema validation. (Leave data validation.)
    • Remove JSDoc comments.
    • Implement the missing case 'union': case which now throws. Add XdrUnion(discriminant, value) class, which is a 2-tuple data type, which contains the selected discriminant and value.
  • Make sure formatter and liner pass

…n, add XdrUnion, fix quadruple

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Sep 16, 2025

@copilot

  • In XdrEncoder:
    ...

Implemented all requested changes:

XdrEncoder changes:

  • writeQuadruple now throws Error('not implemented') (d4705ad)
  • writeStr() uses writer.utf8() method instead of TextEncoder (d4705ad)
  • Inlined .getPaddedSize() method calculations (d4705ad)
  • Removed .writeArr() and .writeObj() methods (kept as throwing methods for interface compatibility) (d4705ad)

XdrSchemaEncoder changes:

  • Removed all schema validation - users will use XdrSchemaValidator separately (d4705ad)
  • Removed all JSDoc comments (d4705ad)
  • Implemented case 'union': with new XdrUnion(discriminant, value) class (d4705ad)

Additional:

  • Created XdrUnion class as 2-tuple data type (d4705ad)
  • Updated all tests, formatter and linter pass (d4705ad)
  • All 153 tests passing (d4705ad)

Copilot AI changed the title Implement XDR encoder with schema validation and comprehensive tests Implement XDR encoder with separated schema validation and comprehensive tests Sep 16, 2025
Copilot finished work on behalf of streamich September 16, 2025 11:07
Copy link
Collaborator

@streamich streamich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot

  • Address review comments
  • Make sure yarn test passes
  • Make sure to run yarn prettier in the end

/**
* Writes an XDR signed 64-bit integer (hyper) in big-endian format.
*/
public writeHyper(hyper: number | bigint): void {
Copy link
Collaborator

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.

Copy link
Contributor Author

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 {
Copy link
Collaborator

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.

Copy link
Contributor Author

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)

Comment on lines 177 to 180
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}`);
}
Copy link
Collaborator

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.

Suggested change
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;

Copy link
Contributor Author

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)

Comment on lines 202 to 215

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);
}
}
Copy link
Collaborator

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:

Suggested change
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);
}

Copy link
Contributor Author

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)

}

public writeArr(arr: unknown[]): void {
throw new Error('writeArr not implemented in XDR encoder');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error('writeArr not implemented in XDR encoder');
throw new Error('not implemented');

Copy link
Contributor Author

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)

}

public writeObj(obj: Record<string, unknown>): void {
throw new Error('writeObj not implemented in XDR encoder');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error('writeObj not implemented in XDR encoder');
throw new Error('not implemented');

Copy link
Contributor Author

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)


export class XdrSchemaEncoder {
private encoder: XdrEncoder;
private validator: XdrSchemaValidator;
Copy link
Collaborator

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.

Suggested change
private validator: XdrSchemaValidator;

Copy link
Contributor Author

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>
Copy link
Contributor Author

Copilot AI commented Sep 16, 2025

@copilot

  • Address review comments...

Addressed all review comments, tests pass, and ran prettier (d10904e):

✅ Used native setBigInt64() and setBigUint64() methods
✅ Simplified opaque handling and removed size parameter
✅ Fixed error messages and removed validator
✅ All 152 XDR tests pass + 3061 total tests pass
✅ Code formatted with prettier

@streamich streamich merged commit c4eda46 into master Sep 16, 2025
8 checks passed
@streamich streamich deleted the copilot/fix-53 branch September 16, 2025 18:00
@github-actions
Copy link

🎉 This PR is included in version 1.13.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants