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

v0.4.0 #13

Merged
merged 6 commits into from
Dec 12, 2024
Merged

v0.4.0 #13

merged 6 commits into from
Dec 12, 2024

Conversation

tperale
Copy link
Owner

@tperale tperale commented Dec 12, 2024

No description provided.

`@ValueSet` decorator set the value of the decorated property based on
a function passed as a parameter.
This decorator don't read anything from the binary file and is just used
to add more context to a class during the reading.

In the following example `@ValueSet` is used to fetch the protocol type name
based on an id read in by the binary definition.
The `protocol_name` will just appear when the object is serialized and will
gave the object more context.

```typescript
const ID_TO_NAME = {
  1: "Record",
  ...
}

class Protocol {
  @relation(PrimitiveSymbol.u8)
  protocol_id: number

  @ValueSet(_ => ID_TO_NAME[_.protocol_id] || 'UNKNOWN')
  protocol_name: string
}
```
`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 be1cfc4 into main Dec 12, 2024
3 checks passed
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