Skip to content

Add variable-length string and new bytes field type#10

Merged
rotu merged 6 commits intomainfrom
copilot/add-variable-length-string-field
Apr 12, 2026
Merged

Add variable-length string and new bytes field type#10
rotu merged 6 commits intomainfrom
copilot/add-variable-length-string-field

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 12, 2026

  • Explore codebase and understand existing patterns
  • Make byteLength optional in the string field function in fields.ts
  • Add bytes field function to fields.ts (fixed and variable-length, read-only, live Uint8Array view)
  • Add tests in mod_test.ts for variable-length string and bytes field
  • Run tests to validate changes (all 29 tests pass)
  • Bump version to 0.17.0-pre in package.json and deno.json
  • Add changelog entry for 0.17.0-pre in CHANGELOG.md
Original prompt

Summary

Add two new capabilities to the structview library:

  1. Variable-length string field: The existing string(fieldOffset, byteLength) function requires a fixed byteLength. Add support for omitting byteLength, in which case the string extends from fieldOffset to the end of the struct's buffer. This makes byteLength optional and defaults to variable-length (rest-of-struct).

  2. bytes field type: Add a new bytes() field function that returns a live Uint8Array view into the struct's underlying buffer. It should support:

    • Fixed-length: bytes(fieldOffset, byteLength) — returns a Uint8Array of exactly byteLength bytes starting at fieldOffset.
    • Variable-length: bytes(fieldOffset) — returns a Uint8Array from fieldOffset to the end of the struct. This is the default when byteLength is omitted.
    • The returned Uint8Array must be a live view (backed by the same ArrayBuffer), not a copy.
    • The bytes field should be read-only (no setter) — it returns a live view so mutations happen through the Uint8Array itself.

Implementation Details

Variable-length string

In fields.ts, modify the existing string function signature so byteLength is optional:

export function string(
  fieldOffset: number,
  byteLength?: number,
): StructPropertyDescriptor<string>

When byteLength is undefined, use structBytes(this, fieldOffset) (which already defaults end to dv.byteLength in core.ts).

bytes field

Add a new exported function in fields.ts:

export function bytes(
  fieldOffset: number,
  byteLength?: number,
): StructPropertyDescriptor<Uint8Array> & ReadOnlyAccessorDescriptor<Uint8Array>
  • When byteLength is provided: return structBytes(this, fieldOffset, fieldOffset + byteLength)
  • When byteLength is omitted: return structBytes(this, fieldOffset) (extends to end of struct)
  • This should be a getter-only (read-only) descriptor since the Uint8Array is a live view.

Exports

Make sure bytes is exported from mod.ts (it already re-exports * from "./fields.ts", so just adding the export in fields.ts should suffice).

Tests

Add tests in mod_test.ts covering:

  1. Variable-length string (no byteLength):

    • Reading a string that extends to the end of the struct
    • Writing a string that extends to the end of the struct
    • Trailing null trimming still works
    • Works correctly when the struct has other fields before it
  2. Fixed-length bytes:

    • Returns a Uint8Array of the correct length
    • Returns a live view (modifications through the Uint8Array are reflected in the underlying buffer)
    • Is read-only (assigning to the property throws — use // @ts-expect-error pattern like existing tests)
  3. Variable-length bytes (no byteLength):

    • Returns a Uint8Array extending from fieldOffset to the end of the struct
    • Returns a live view
    • Is read-only
  4. Existing string tests continue to pass — the existing string(offset, length) usage must remain fully backward compatible.

Reference

  • The structBytes helper in core.ts already supports optional end parameter (defaults to dv.byteLength), so variable-length behavior is straightforward.
  • Follow the existing patterns in fields.ts for field function signatures and descriptor construction.
  • Follow the existing test patterns in mod_test.ts (uses vitest test/expect and node:assert/strict).
  • Make sure to import bytes in the test file alongside the existing field imports.
  • Run deno x vitest run or npm x vitest run to verify tests pass.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: Please add the ability to declare a variable-length string field, extending to the end of the struct. that should be the default if length is not specified. if we don't have a bytes field type (either fixed or variable length, returning a live Uint8Array), add that too

User: Please add the ability to declare a variable-length string field, extending to the end of the struct. that should be the default if length is not specified. if we don't have a bytes field type (either fixed or variable length, returning a live Uint8Array), add that too. make sure to add tests

Assistant:

I'll start by exploring the repository to understand its structure and current capabilities before creating the PR.

Assistant: I'd like to help you create a PR to add variable-length string and bytes field types, but I need to know which repository to target. Could you please provide the repository in owner/repo format?

User: rotu/structview. open a PR

Assistant:

Let me first explore the repository to understand its current structure and capabilities.

Assistant: Now I have a thorough understanding of the codebase. Let me create the PR with a detailed problem statement.

Assistant: I've kicked off the Copilot cod...

This pull request was created from Copilot chat.

Copilot AI changed the title [WIP] Add variable-length string field and bytes field type support Add variable-length string and new bytes field type Apr 12, 2026
Copilot finished work on behalf of rotu April 12, 2026 18:13
Copilot AI requested a review from rotu April 12, 2026 18:13
Copy link
Copy Markdown
Owner

@rotu rotu left a comment

Choose a reason for hiding this comment

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

Please do a pre-minor version bump and add a changelog entry.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 12, 2026

Please do a pre-minor version bump and add a changelog entry.

Done in f8b59f1 — bumped to 0.17.0 in package.json and deno.json, and added the changelog entry.

Copilot finished work on behalf of rotu April 12, 2026 19:00
Copilot AI requested a review from rotu April 12, 2026 19:00
Copy link
Copy Markdown
Owner

@rotu rotu left a comment

Choose a reason for hiding this comment

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

No, a prerelease version. e.g. 0.17.0-pre

Agent-Logs-Url: https://github.com/rotu/structview/sessions/a2fba9b2-26b1-4194-a770-c799e30f326d

Co-authored-by: rotu <119948+rotu@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 12, 2026

No, a prerelease version. e.g. 0.17.0-pre

Updated to 0.17.0-pre in 7549a42.

Copilot AI requested a review from rotu April 12, 2026 19:05
Copilot finished work on behalf of rotu April 12, 2026 19:05
@rotu rotu marked this pull request as ready for review April 12, 2026 19:06
@rotu rotu merged commit bc8b39e into main Apr 12, 2026
8 checks passed
@rotu rotu deleted the copilot/add-variable-length-string-field branch April 12, 2026 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants