Skip to content

Add declarative optional and dynamic field layouts#8

Draft
Copilot wants to merge 7 commits intomainfrom
copilot/add-optional-variably-sized-fields-again
Draft

Add declarative optional and dynamic field layouts#8
Copilot wants to merge 7 commits intomainfrom
copilot/add-optional-variably-sized-fields-again

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 2, 2026

This extends structview's explicit-offset API with a complementary declarative layer for optional and variably sized fields. The new model keeps fixed layouts unchanged while allowing later fields to derive their presence, length, and offset from earlier fields.

  • New declarative field capabilities

    • Added optional() to gate a field behind a boolean property or resolver
    • Field factories can now resolve offsets and lengths from:
      • literal numbers
      • property names
      • accessor/resolver functions
  • What this enables

    • Optional fields without custom fromDataView() boilerplate
    • Variable-length fields whose size comes from another field
    • Follow-on fields whose offset depends on whether earlier fields are present
  • API surface updated

    • Dynamic offsets now work across scalar fields and strings
    • Dynamic lengths now work for string(), typedArray(), substruct(), and arbitrary-width integer fields
    • Existing fixed-offset usage remains valid
  • Docs and examples

    • Added README guidance showing how to express packet-style layouts declaratively
import { bool, defineStruct, optional, string, u16, u8 } from "@rotu/structview"

class Record extends defineStruct({
  has_name: bool(0),
  name_length: u8(1),
  name: optional(string(2, "name_length"), "has_name"),
  name_end: {
    get() {
      return 2 + (this.has_name ? this.name_length : 0)
    },
  },
  checksum: u16("name_end"),
}) {}
  • Scope
    • This is an additive design: it does not replace the current explicit layout model
    • It introduces a small set of composable primitives for expressing non-fixed layouts in the same descriptor style as the rest of the library

Copilot AI changed the title [WIP] Design optional and variably sized fields for current API Add declarative optional and dynamic field layouts Apr 2, 2026
Copilot finished work on behalf of rotu April 2, 2026 22:37
Copilot AI requested a review from rotu April 2, 2026 22:37
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