-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic support for
Ctx{Get,Set}
decorators
`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[] } ```
- Loading branch information
Showing
6 changed files
with
408 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { describe, expect } from '@jest/globals' | ||
import { CtxSet, CtxGet, useContextGet, useContextSet } from '../context' | ||
import Meta from '../../metadatas' | ||
|
||
function testContextGet (TargetClass: new () => any, field: string, ctx: object, expected: any) { | ||
const instance = new TargetClass() | ||
|
||
const contexts = Meta.getContext(TargetClass[Symbol.metadata] as DecoratorMetadataObject, field) | ||
expect(useContextGet(contexts, instance, ctx)).toEqual(expected) | ||
} | ||
|
||
function testContextSet (TargetClass: new () => any, field: string, value: any, ctx: object) { | ||
const instance = new TargetClass() | ||
|
||
const contexts = Meta.getContext(TargetClass[Symbol.metadata] as DecoratorMetadataObject, field) | ||
// expect(useContext(ctx, value, instance)).toStrictEqual(equal) | ||
useContextSet(contexts, value, instance, ctx) | ||
} | ||
|
||
describe('@Ctx: functions', () => { | ||
it('should modify the ctx object', () => { | ||
class TestClass { | ||
@CtxSet('test') | ||
data: number | ||
} | ||
const ctx = {} | ||
const VALUE = 1 | ||
testContextSet(TestClass, 'data', VALUE, ctx) | ||
// @ts-ignore | ||
Check failure on line 29 in src/decorators/__tests__/context.test.ts GitHub Actions / lint
|
||
expect(ctx.test).toEqual(VALUE) | ||
}) | ||
it('should modify the ctx object with recursive accessors', () => { | ||
class TestClass { | ||
@CtxSet('foo.bar.1') | ||
data: number | ||
|
||
@CtxSet('foo.bar.2') | ||
data_2: number | ||
} | ||
const ctx = {} | ||
const VALUE = 1 | ||
testContextSet(TestClass, 'data', VALUE, ctx) | ||
testContextSet(TestClass, 'data_2', VALUE, ctx) | ||
// @ts-ignore | ||
Check failure on line 44 in src/decorators/__tests__/context.test.ts GitHub Actions / lint
|
||
expect(ctx.foo.bar[1]).toEqual(VALUE) | ||
// @ts-ignore | ||
Check failure on line 46 in src/decorators/__tests__/context.test.ts GitHub Actions / lint
|
||
expect(ctx.foo.bar[2]).toEqual(VALUE) | ||
}) | ||
it('should get value from the ctx object', () => { | ||
class TestClass { | ||
@CtxGet('test') | ||
data: number | ||
} | ||
const VALUE = 1 | ||
const ctx = { test: VALUE } | ||
testContextGet(TestClass, 'data', ctx, VALUE) | ||
}) | ||
it('should get value from the ctx object with recursive accessors', () => { | ||
class TestClass { | ||
@CtxGet('test') | ||
data: number | ||
} | ||
const VALUE = 1 | ||
const ctx = { test: VALUE } | ||
testContextGet(TestClass, 'data', ctx, VALUE) | ||
}) | ||
// it('should throw when accessing non existing properties', () => { | ||
// class TestClass { | ||
// @CtxGet('test.foo') | ||
// data: number | ||
// } | ||
// const VALUE = 1 | ||
// const ctx = { test: VALUE } | ||
// testContextGet(TestClass, 'data', ctx, VALUE) | ||
// }) | ||
// it('should throw when overriding an existing object', () => { | ||
// class TestClass { | ||
// @CtxGet('test.foo') | ||
// data: number | ||
// } | ||
// const VALUE = 1 | ||
// const ctx = { test: VALUE } | ||
// testContextGet(TestClass, 'data', ctx, VALUE) | ||
// }) | ||
}) |
Oops, something went wrong.