Add variable-length string and new bytes field type#10
Merged
Conversation
Agent-Logs-Url: https://github.com/rotu/structview/sessions/a76e014c-40c4-4051-80ca-4fb84eee5b03 Co-authored-by: rotu <119948+rotu@users.noreply.github.com>
Agent-Logs-Url: https://github.com/rotu/structview/sessions/a76e014c-40c4-4051-80ca-4fb84eee5b03 Co-authored-by: rotu <119948+rotu@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add variable-length string field and bytes field type support
Add variable-length Apr 12, 2026
string and new bytes field type
rotu
requested changes
Apr 12, 2026
Owner
rotu
left a comment
There was a problem hiding this comment.
Please do a pre-minor version bump and add a changelog entry.
Agent-Logs-Url: https://github.com/rotu/structview/sessions/bfcb4acd-f01b-4495-9580-419915c00f9e Co-authored-by: rotu <119948+rotu@users.noreply.github.com>
Contributor
Author
Done in f8b59f1 — bumped to |
rotu
requested changes
Apr 12, 2026
Owner
rotu
left a comment
There was a problem hiding this comment.
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>
Contributor
Author
Updated to |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
byteLengthoptional in thestringfield function infields.tsbytesfield function tofields.ts(fixed and variable-length, read-only, live Uint8Array view)mod_test.tsfor variable-length string and bytes fieldpackage.jsonanddeno.jsonCHANGELOG.mdOriginal prompt
Summary
Add two new capabilities to the structview library:
Variable-length
stringfield: The existingstring(fieldOffset, byteLength)function requires a fixedbyteLength. Add support for omittingbyteLength, in which case the string extends fromfieldOffsetto the end of the struct's buffer. This makesbyteLengthoptional and defaults to variable-length (rest-of-struct).bytesfield type: Add a newbytes()field function that returns a liveUint8Arrayview into the struct's underlying buffer. It should support:bytes(fieldOffset, byteLength)— returns aUint8Arrayof exactlybyteLengthbytes starting atfieldOffset.bytes(fieldOffset)— returns aUint8ArrayfromfieldOffsetto the end of the struct. This is the default whenbyteLengthis omitted.Uint8Arraymust be a live view (backed by the sameArrayBuffer), not a copy.bytesfield should be read-only (no setter) — it returns a live view so mutations happen through theUint8Arrayitself.Implementation Details
Variable-length
stringIn
fields.ts, modify the existingstringfunction signature sobyteLengthis optional:When
byteLengthisundefined, usestructBytes(this, fieldOffset)(which already defaultsendtodv.byteLengthincore.ts).bytesfieldAdd a new exported function in
fields.ts:byteLengthis provided: returnstructBytes(this, fieldOffset, fieldOffset + byteLength)byteLengthis omitted: returnstructBytes(this, fieldOffset)(extends to end of struct)Exports
Make sure
bytesis exported frommod.ts(it already re-exports* from "./fields.ts", so just adding the export infields.tsshould suffice).Tests
Add tests in
mod_test.tscovering:Variable-length string (no byteLength):
Fixed-length bytes:
Uint8Arrayof the correct lengthUint8Arrayare reflected in the underlying buffer)// @ts-expect-errorpattern like existing tests)Variable-length bytes (no byteLength):
Uint8Arrayextending fromfieldOffsetto the end of the structExisting
stringtests continue to pass — the existingstring(offset, length)usage must remain fully backward compatible.Reference
structByteshelper incore.tsalready supports optionalendparameter (defaults todv.byteLength), so variable-length behavior is straightforward.fields.tsfor field function signatures and descriptor construction.mod_test.ts(uses vitesttest/expectandnode:assert/strict).bytesin the test file alongside the existing field imports.deno x vitest runornpm x vitest runto 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
stringfield, extending to the end of the struct. that should be the default if length is not specified. if we don't have abytesfield type (either fixed or variable length, returning a live Uint8Array), add that tooUser: Please add the ability to declare a variable-length
stringfield, extending to the end of the struct. that should be the default if length is not specified. if we don't have abytesfield type (either fixed or variable length, returning a live Uint8Array), add that too. make sure to add testsAssistant:
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
stringandbytesfield types, but I need to know which repository to target. Could you please provide the repository inowner/repoformat?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.