-
Notifications
You must be signed in to change notification settings - Fork 2
Rewrite type declarations #42
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
Open
JELoohuis
wants to merge
20
commits into
athombv:master
Choose a base branch
from
JELoohuis:add-generic-types
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9c50dcb
Rewrite type declarations
JELoohuis c2f0d61
Apply PR fixes
JELoohuis 7e0ae66
Support number enum indices
JELoohuis 1bc91d7
Clean up redundant type import
JELoohuis 655f817
fix(docs): Update example for creating Struct instance in TypeScript
RobinBol a283823
style: Standardize formatting of DataType declarations and function s…
RobinBol 0bfc54c
fix(types): add flag property access to Bitmap type
RobinBol 8ea0d5e
refactor(types): Simplify StructProperties type definition
RobinBol 4853315
feat(types): support Struct as a field type in Struct
RobinBol ff92e21
feat(types): add Array0/Array8 overload for Struct element types
RobinBol 65f9b34
chore(types): add typecheck CI job with types-test.ts
RobinBol bc8d3b1
Clarify Array constructor overloads
JELoohuis faa2500
Fix data8-data32 types
JELoohuis d801d9e
Fix noData fromBuffer type
JELoohuis 14785fe
Fix Bitmap setBit type value not being optional
JELoohuis b6add0a
Add type test for default value
JELoohuis 93d5019
Add support for enums that receive undefined values
JELoohuis 67252dc
Allow any type to be passed into noData toBuffer
JELoohuis 49ac013
Add type test for toBuffer
JELoohuis 85cf2c7
Type return of noData.toBuffer as null
JELoohuis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,143 +1,227 @@ | ||
| type ToBuffer = (buffer: Buffer, value: unknown, index: number) => Buffer; | ||
| type FromBuffer = (buffer: Buffer, index: number, returnLength: boolean) => unknown; | ||
| declare module "@athombv/data-types" { | ||
| class DataType<ToBuffer, FromBuffer = ToBuffer, ToBufferReturn = number> { | ||
| constructor( | ||
| id: number, | ||
| shortName: string, | ||
| length: number, | ||
| toBuffer: (buffer: Buffer, value: ToBuffer, index?: number) => ToBufferReturn, | ||
| fromBuffer: (buffer: Buffer, index?: number) => FromBuffer, | ||
| ...args: Array<unknown> | ||
| ); | ||
|
|
||
| declare module "@athombv/data-types" { | ||
| interface DataTypeInterface { | ||
| id: number; | ||
| shortName: string; | ||
| length: number; | ||
| toBuffer: ToBuffer; | ||
| fromBuffer: FromBuffer; | ||
| args: unknown[]; | ||
| defaultValue: unknown; | ||
| isAnalog: () => boolean; | ||
| inspect: () => string; | ||
| toBuffer: (buffer: Buffer, value: ToBuffer, index?: number) => ToBufferReturn; | ||
| fromBuffer: (buffer: Buffer, index?: number) => FromBuffer; | ||
| args: Array<unknown>; | ||
| defaultValue: FromBuffer; | ||
|
|
||
| get isAnalog(): boolean; | ||
| inspect(): string; | ||
| } | ||
|
|
||
| interface DataTypeConstructor { | ||
| new ( | ||
| id: number, | ||
| shortName: string, | ||
|
|
||
| const DataTypes: { | ||
| noData: DataType<any, { result:null, length: 0 }, null> | ||
|
|
||
| data8: DataType<number>; | ||
| data16: DataType<number>; | ||
| data24: DataType<number>; | ||
| data32: DataType<number>; | ||
| data40: DataType<Buffer>; | ||
| data48: DataType<Buffer>; | ||
| data56: DataType<Buffer>; | ||
| data64: DataType<Buffer>; | ||
|
|
||
| bool: DataType<boolean | null>; | ||
|
|
||
| map8: <Flags extends string | null>(...flags: Array<Flags>) => DataType<Bitmap<Flags>>; | ||
| map16: <Flags extends string | null>(...flags: Array<Flags>) => DataType<Bitmap<Flags>>; | ||
| map24: <Flags extends string | null>(...flags: Array<Flags>) => DataType<Bitmap<Flags>>; | ||
| map32: <Flags extends string | null>(...flags: Array<Flags>) => DataType<Bitmap<Flags>>; | ||
| map40: <Flags extends string | null>(...flags: Array<Flags>) => DataType<Bitmap<Flags>>; | ||
| map48: <Flags extends string | null>(...flags: Array<Flags>) => DataType<Bitmap<Flags>>; | ||
| map56: <Flags extends string | null>(...flags: Array<Flags>) => DataType<Bitmap<Flags>>; | ||
| map64: <Flags extends string | null>(...flags: Array<Flags>) => DataType<Bitmap<Flags>>; | ||
|
|
||
| uint8: DataType<number>; | ||
| uint16: DataType<number>; | ||
| uint24: DataType<number>; | ||
| uint32: DataType<number>; | ||
| uint40: DataType<number>; | ||
| uint48: DataType<number>; | ||
| // uint56: DataType<number>, | ||
| // uint64: DataType<number>, | ||
|
|
||
| int8: DataType<number>; | ||
| int16: DataType<number>; | ||
| int24: DataType<number>; | ||
| int32: DataType<number>; | ||
| int40: DataType<number>; | ||
| int48: DataType<number>; | ||
| // int56: DataType<number>, | ||
| // int64: DataType<number>, | ||
|
|
||
| enum8: <Flags extends string | number | undefined>(flags: Record<Flags, number>) => DataType<Exclude<Flags, undefined>, Flags>; | ||
| enum16: <Flags extends string | number | undefined>(flags: Record<Flags, number>) => DataType<Exclude<Flags, undefined>, Flags>; | ||
| enum32: <Flags extends string | number | undefined>(flags: Record<Flags, number>) => DataType<Exclude<Flags, undefined>, Flags>; | ||
|
|
||
| // semi: DataType<number>, | ||
| single: DataType<number>; | ||
| double: DataType<number>; | ||
|
|
||
| octstr: DataType<Buffer>; | ||
| string: DataType<string>; | ||
| // octstr16: DataType<string>, | ||
| // string16: DataType<string>, | ||
|
|
||
| // array | ||
| // struct | ||
| // set | ||
| // bag | ||
|
|
||
| // ToD | ||
| // date | ||
| // UTC | ||
|
|
||
| // clusterId | ||
| // attribId | ||
|
|
||
| // bacOID | ||
| EUI48: DataType<string>; | ||
| EUI64: DataType<string>; | ||
| key128: DataType<string>; | ||
|
|
||
| //* Internal Types *// | ||
| map4: <Flags extends string | null>(...flags: Array<Flags>) => DataType<Bitmap<Flags>>; | ||
| uint4: DataType<number>; | ||
| enum4: <Flags extends string | number | undefined>(flags: Record<Flags, number>) => DataType<Exclude<Flags, undefined>, Flags>; | ||
|
|
||
| buffer: DataType<Buffer>; | ||
| buffer8: DataType<Buffer>; | ||
| buffer16: DataType<Buffer>; | ||
|
|
||
| Array0: { | ||
| <T>(type: DataType<T>): DataType<Array<T>>; | ||
| // Overload to allow structs in arrays | ||
| <Defs extends Record<string, StructField>>( | ||
| type: StaticStruct<Defs>, | ||
| ): DataType<Array<StructProperties<Defs>>>; | ||
| }; | ||
| Array8: { | ||
| <T>(type: DataType<T>): DataType<Array<T>>; | ||
| // Overload to allow structs in arrays | ||
| <Defs extends Record<string, StructField>>( | ||
| type: StaticStruct<Defs>, | ||
| ): DataType<Array<StructProperties<Defs>>>; | ||
| }; | ||
| FixedString: (length: number) => DataType<string>; | ||
| }; | ||
|
|
||
| type Bitmap<Flags extends string | null> = BitmapBase<Flags> & { | ||
| [K in Flags as K extends string ? K : never]: boolean; | ||
| }; | ||
|
|
||
| class BitmapBase<Flags extends string | null> { | ||
| _buffer: Buffer; | ||
| _fields: Array<Flags>; | ||
| setBit(index: number, value?: boolean): void; | ||
| getBit(index: number): boolean; | ||
| clearBit(index: number): void; | ||
| setBits(bits: number | Array<Flags>): void; | ||
| getBits(): Array<Flags>; | ||
| get length(): number; | ||
| static fromBuffer<Flags extends string | null>( | ||
| buffer: Buffer, | ||
| index: number, | ||
| length: number, | ||
| flags: Array<Flags>, | ||
| ): Bitmap<Flags>; | ||
| static toBuffer<Flags extends string | null>( | ||
| buffer: Buffer, | ||
| index: number, | ||
| length: number, | ||
| toBuf: ToBuffer, | ||
| fromBuf: FromBuffer, | ||
| ...args: unknown[] | ||
| ): DataTypeInterface; | ||
| flags: Array<Flags>, | ||
| value: number, | ||
| ): number; | ||
| static toBuffer<Flags extends string | null>( | ||
| buffer: Buffer, | ||
| index: number, | ||
| length: number, | ||
| flags: Array<Flags> | undefined, | ||
| value: Bitmap<Flags>, | ||
| ): number; | ||
| toArray(): Array<Flags>; | ||
| toBuffer(buffer: Buffer, index: number): Buffer; | ||
| copy(): Bitmap<Flags>; | ||
| toJSON(): object; | ||
| inspect(): string; | ||
| } | ||
|
|
||
| interface DataTypeFunctionConstructor { | ||
| (...args: unknown[]): DataTypeConstructor; | ||
|
|
||
| type StructField = DataType<any, any, any> | StaticStruct<any>; | ||
|
|
||
| interface StaticStruct<Defs extends Record<string, StructField>> { | ||
| get fields(): Defs; | ||
| get name(): string; | ||
| get length(): number; | ||
| fromJSON(props: any): StructInstance<Defs>; | ||
|
JELoohuis marked this conversation as resolved.
|
||
| fromArgs(...args: Array<unknown>): StructInstance<Defs>; | ||
| fromBuffer(buffer: Buffer, index?: number, returnLength?: false): StructInstance<Defs>; | ||
| fromBuffer( | ||
| buffer: Buffer, | ||
| index?: number, | ||
| returnLength?: true, | ||
| ): { | ||
| result: StructInstance<Defs>; | ||
| length: number; | ||
| }; | ||
| toBuffer(buffer?: Buffer, value?: StructProperties<Defs>, index?: number): number; | ||
| } | ||
|
|
||
| type DataTypeItem = | ||
| | DataTypeConstructor | ||
| | DataTypeFunctionConstructor | ||
| | { [name: string]: DataTypeItem }; // This OR is needed for Structs with a second level of DataTypes | ||
|
|
||
| type GenericMap<T> = { | ||
| [K in keyof T]: DataTypeItem; | ||
|
|
||
| type StructProperties<Defs extends Record<string, StructField>> = { | ||
| [Property in keyof Defs]: Defs[Property] extends DataType<infer ToBuffer, infer FromBuffer, any> | ||
| ? FromBuffer | ||
| : Defs[Property] extends StaticStruct<infer InnerDefs extends Record<string, StructField>> | ||
| ? StructProperties<InnerDefs> | ||
| : never; | ||
| }; | ||
| interface StructTypeInterface<T> { | ||
| toJSON: () => T; | ||
|
|
||
| type StructInstance<Defs extends Record<string, StructField>> = StructProperties<Defs> & { | ||
| toJSON: () => StructProperties<Defs>; | ||
| toBuffer: (buffer?: Buffer, index?: number) => Buffer; | ||
| } | ||
|
|
||
| export interface StructInstance<StructType> { | ||
| fromBuffer: (buffer: Buffer) => StructType & StructTypeInterface<StructType>; | ||
| toBuffer: (buffer: Buffer, object: StructType, index?: number) => Buffer; | ||
| fields: GenericMap<StructType>; | ||
| name: string; | ||
| length: number; | ||
| fromJSON: (object: StructType) => StructType & StructTypeInterface<StructType>; | ||
| fromArgs: (...args: unknown[]) => StructType & StructTypeInterface<StructType>; | ||
| } | ||
| export const DataTypes: { | ||
| noData: DataTypeConstructor; | ||
| data8: DataTypeConstructor; | ||
| data16: DataTypeConstructor; | ||
| data24: DataTypeConstructor; | ||
| data32: DataTypeConstructor; | ||
| data40: DataTypeConstructor; | ||
| data48: DataTypeConstructor; | ||
| data56: DataTypeConstructor; | ||
| data64: DataTypeConstructor; | ||
| bool: DataTypeConstructor; | ||
| map8: DataTypeFunctionConstructor; | ||
| map16: DataTypeFunctionConstructor; | ||
| map24: DataTypeFunctionConstructor; | ||
| map32: DataTypeFunctionConstructor; | ||
| map40: DataTypeFunctionConstructor; | ||
| map48: DataTypeFunctionConstructor; | ||
| map56: DataTypeFunctionConstructor; | ||
| map64: DataTypeFunctionConstructor; | ||
| uint8: DataTypeConstructor; | ||
| uint16: DataTypeConstructor; | ||
| uint24: DataTypeConstructor; | ||
| uint32: DataTypeConstructor; | ||
| uint40: DataTypeConstructor; | ||
| uint48: DataTypeConstructor; | ||
| int8: DataTypeConstructor; | ||
| int16: DataTypeConstructor; | ||
| int24: DataTypeConstructor; | ||
| int32: DataTypeConstructor; | ||
| int40: DataTypeConstructor; | ||
| int48: DataTypeConstructor; | ||
| enum8: DataTypeFunctionConstructor; | ||
| enum16: DataTypeFunctionConstructor; | ||
| enum32: DataTypeFunctionConstructor; | ||
| single: DataTypeConstructor; | ||
| double: DataTypeConstructor; | ||
| octstr: DataTypeConstructor; | ||
| string: DataTypeConstructor; | ||
| EUI48: DataTypeConstructor; | ||
| EUI64: DataTypeConstructor; | ||
| key128: DataTypeConstructor; | ||
| uint4: DataTypeConstructor; | ||
| enum4: DataTypeFunctionConstructor; | ||
| map4: DataTypeFunctionConstructor; | ||
| buffer: DataTypeConstructor; | ||
| buffer8: DataTypeConstructor; | ||
| buffer16: DataTypeConstructor; | ||
| Array0: DataTypeFunctionConstructor; | ||
| Array8: DataTypeFunctionConstructor; | ||
| FixedString: DataTypeFunctionConstructor; | ||
| }; | ||
| export const DataType: DataTypeInterface; | ||
| export function Struct<StructType>( | ||
|
|
||
| function Struct<Defs extends Record<string, StructField>>( | ||
| name: string, | ||
| objectDefinition: GenericMap<StructType> | ||
| ): StructInstance<StructType>; | ||
| defs: Defs, | ||
| opts?: { encodeMissingFieldsBehavior?: "default" | "skip" }, | ||
| ): StaticStruct<Defs>; | ||
| } | ||
|
|
||
| /* | ||
| How to use @athombv/data-types in TypeScript: | ||
|
|
||
| // Create a type that represents the Struct data | ||
| type ZdoEndDeviceAnnounceIndication = { | ||
| srcAddr: number; | ||
| IEEEAddr: string; | ||
| }; | ||
| // Create a Struct instance | ||
| const ZdoEndDeviceAnnounceIndicationStruct = Struct("ZdoEndDeviceAnnounceIndication", { | ||
| srcAddr: DataTypes.uint16, | ||
| IEEEAddr: DataTypes.EUI64, | ||
| }); | ||
|
|
||
| // Create a Struct instance with generic type ZdoEndDeviceAnnounceIndication | ||
| const ZdoEndDeviceAnnounceIndicationStruct = | ||
| Struct<ZdoEndDeviceAnnounceIndication>("ZdoEndDeviceAnnounceIndication", { | ||
| srcAddr: DataTypes.uint16, | ||
| IEEEAddr: DataTypes.EUI64, | ||
| }); | ||
|
|
||
| // Create ZdoEndDeviceAnnounceIndication object | ||
| // Create ZdoEndDeviceAnnounceIndication object from a buffer | ||
| const ZdoEndDeviceAnnounceObject = ZdoEndDeviceAnnounceIndicationStruct.fromBuffer( | ||
| Buffer.from([0, 1, 2, 3]) | ||
| ); | ||
|
|
||
| ZdoEndDeviceAnnounceObject.srcAddr.trim(); // This errors, srcAddr is not a string | ||
| // @ts-expect-error srcAddr is not a string | ||
| ZdoEndDeviceAnnounceObject.srcAddr.trim(); | ||
|
|
||
| // Create Buffer instance from ZdoEndDeviceAnnounceObject | ||
| const ZdoEndDeviceAnnounceBuffer = ZdoEndDeviceAnnounceIndicationStruct.toBuffer({ srcAddr: 1, IEEAddr: 'abc' }); // This errors due to typo in IEEEAddr name | ||
| const ZdoEndDeviceAnnounceBuffer = Buffer.alloc(8); | ||
| // @ts-expect-error typo in IEEEAddr name | ||
| ZdoEndDeviceAnnounceIndicationStruct.toBuffer(ZdoEndDeviceAnnounceBuffer, { srcAddr: 1, IEEAddr: 'abc' }); | ||
|
|
||
| const Item = Struct('Item', { a: DataTypes.uint8, b: DataTypes.uint8 }); | ||
| const ArrayOfItems = DataTypes.Array8(Item); | ||
|
|
||
| Known limitations: | ||
| - Structs in Structs are considered a no-go by these definitions. | ||
| - DataTypes have no related JS type, so a few unknowns are used. | ||
| Known limitation: Struct.fromArgs cannot be typed. | ||
| */ | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.