Skip to content
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

feat: basic support for Ctx{Get,Set} decorators #12

Merged
merged 1 commit into from
Dec 12, 2024
Merged

Conversation

tperale
Copy link
Owner

@tperale tperale commented Dec 12, 2024

Fixed #10

Ctx decorators are a way to share informations across objects during the reading of the binary file definition.

In the following example, a streaming protocol that receives records of arbitrary length is defined.
Records have two different type a 'definition' or a 'data' both use an 'id' to identify themself. The definition defines the size of the data message they define by using CtxSet to store that size into the context. The data message uses CtxGet to fetch its size defined previously by the definition.

class RecordDefinition {
    @Relation(PrimitiveSymbol.u8)
    id: number

    @CtxSet(_ => `Definition.${_.id}`)
    @Relation(PrimitiveSymbol.u8)
    size: number
}

class RecordMessage {
    @Relation(PrimitiveSymbol.u8)
    id: number

    @CtxGet(_ => `Definition.${_.id}`)
    _size: number

    @Count('_size')
    @Relation(PrimitiveSymbol.u8)
    data
}

class Record {
    @Relation(PrimitiveSymbol.u8)
    type: number

    @Choice('type', {
        0x00: RecordDefinition,
        0x01: RecordMessage,
    })
    message: RecordDefinition | RecordMessage
}

class Protocol {
    @Until(EOF)
    records: Record[]
}

`Ctx` decorators are a way to share informations across
objects during the reading of the binary file definition.

In the following example, a streaming protocol that receives records
of arbitrary length is defined.
Records have two different type a 'definition' or a 'data' both use an
'id' to identify themself. The definition defines the size of the data
message they define by using `CtxSet` to store that size into the
context. The data message uses `CtxGet` to fetch its size defined
previously by the definition.

```typescript
class RecordDefinition {
    @relation(PrimitiveSymbol.u8)
    id: number

    @CtxSet(_ => `Definition.${_.id}`)
    @relation(PrimitiveSymbol.u8)
    size: number
}

class RecordMessage {
    @relation(PrimitiveSymbol.u8)
    id: number

    @CtxGet(_ => `Definition.${_.id}`)
    _size: number

    @count('_size')
    @relation(PrimitiveSymbol.u8)
    data
}

class Record {
    @relation(PrimitiveSymbol.u8)
    type: number

    @choice('type', {
        0x00: RecordDefinition,
        0x01: RecordMessage,
    })
    message: RecordDefinition | RecordMessage
}

class Protocol {
    @until(EOF)
    records: Record[]
}
```
@tperale tperale merged commit b450ed2 into dev Dec 12, 2024
3 checks passed
@tperale tperale deleted the dev-context branch December 12, 2024 21:03
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.

1 participant