mirror of
https://github.com/yume-chan/ya-webadb.git
synced 2025-10-05 02:39:26 +02:00
fix(struct): support custom TypeScript type for uint8Array
This commit is contained in:
parent
f494d80166
commit
896e7e7bfd
5 changed files with 50 additions and 39 deletions
|
@ -52,7 +52,7 @@ interface ArrayBufferLikeFieldCreator<
|
|||
* @param name Name of the field
|
||||
* @param type `Array.SubType.ArrayBuffer` or `Array.SubType.String`
|
||||
* @param options Fixed-length array options
|
||||
* @param typescriptType Type of the field in TypeScript.
|
||||
* @param typeScriptType Type of the field in TypeScript.
|
||||
* For example, if this field is a string, you can declare it as a string enum or literal union.
|
||||
*/
|
||||
<
|
||||
|
@ -63,7 +63,7 @@ interface ArrayBufferLikeFieldCreator<
|
|||
name: TName,
|
||||
type: TType,
|
||||
options: FixedLengthBufferLikeFieldOptions,
|
||||
typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
): AddFieldDescriptor<
|
||||
TFields,
|
||||
TOmitInitKey,
|
||||
|
@ -88,7 +88,7 @@ interface ArrayBufferLikeFieldCreator<
|
|||
name: TName,
|
||||
type: TType,
|
||||
options: TOptions,
|
||||
typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
): AddFieldDescriptor<
|
||||
TFields,
|
||||
TOmitInitKey,
|
||||
|
@ -118,7 +118,7 @@ interface BoundArrayBufferLikeFieldDefinitionCreator<
|
|||
>(
|
||||
name: TName,
|
||||
options: FixedLengthBufferLikeFieldOptions,
|
||||
typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
): AddFieldDescriptor<
|
||||
TFields,
|
||||
TOmitInitKey,
|
||||
|
@ -127,7 +127,8 @@ interface BoundArrayBufferLikeFieldDefinitionCreator<
|
|||
TName,
|
||||
FixedLengthBufferLikeFieldDefinition<
|
||||
TType,
|
||||
FixedLengthBufferLikeFieldOptions
|
||||
FixedLengthBufferLikeFieldOptions,
|
||||
TTypeScriptType
|
||||
>
|
||||
>;
|
||||
|
||||
|
@ -139,7 +140,7 @@ interface BoundArrayBufferLikeFieldDefinitionCreator<
|
|||
>(
|
||||
name: TName,
|
||||
options: TOptions,
|
||||
typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
): AddFieldDescriptor<
|
||||
TFields,
|
||||
TOmitInitKey,
|
||||
|
@ -148,7 +149,8 @@ interface BoundArrayBufferLikeFieldDefinitionCreator<
|
|||
TName,
|
||||
VariableLengthBufferLikeFieldDefinition<
|
||||
TType,
|
||||
TOptions
|
||||
TOptions,
|
||||
TTypeScriptType
|
||||
>
|
||||
>;
|
||||
}
|
||||
|
@ -251,11 +253,11 @@ export class Struct<
|
|||
>(
|
||||
name: TName,
|
||||
type: TType,
|
||||
_typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
) {
|
||||
return this.field(
|
||||
name,
|
||||
new NumberFieldDefinition(type, _typescriptType),
|
||||
new NumberFieldDefinition(type, typeScriptType),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -267,12 +269,12 @@ export class Struct<
|
|||
TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType']
|
||||
>(
|
||||
name: TName,
|
||||
_typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
) {
|
||||
return this.number(
|
||||
name,
|
||||
NumberFieldType.Int8,
|
||||
_typescriptType
|
||||
typeScriptType
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -284,12 +286,12 @@ export class Struct<
|
|||
TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType']
|
||||
>(
|
||||
name: TName,
|
||||
_typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
) {
|
||||
return this.number(
|
||||
name,
|
||||
NumberFieldType.Uint8,
|
||||
_typescriptType
|
||||
typeScriptType
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -301,12 +303,12 @@ export class Struct<
|
|||
TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType']
|
||||
>(
|
||||
name: TName,
|
||||
_typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
) {
|
||||
return this.number(
|
||||
name,
|
||||
NumberFieldType.Int16,
|
||||
_typescriptType
|
||||
typeScriptType
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -318,12 +320,12 @@ export class Struct<
|
|||
TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType']
|
||||
>(
|
||||
name: TName,
|
||||
_typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
) {
|
||||
return this.number(
|
||||
name,
|
||||
NumberFieldType.Uint16,
|
||||
_typescriptType
|
||||
typeScriptType
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -335,12 +337,12 @@ export class Struct<
|
|||
TTypeScriptType = (typeof NumberFieldType)['Int32']['TTypeScriptType']
|
||||
>(
|
||||
name: TName,
|
||||
_typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
) {
|
||||
return this.number(
|
||||
name,
|
||||
NumberFieldType.Int32,
|
||||
_typescriptType
|
||||
typeScriptType
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -352,12 +354,12 @@ export class Struct<
|
|||
TTypeScriptType = (typeof NumberFieldType)['Uint32']['TTypeScriptType']
|
||||
>(
|
||||
name: TName,
|
||||
typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
) {
|
||||
return this.number(
|
||||
name,
|
||||
NumberFieldType.Uint32,
|
||||
typescriptType
|
||||
typeScriptType
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -368,11 +370,11 @@ export class Struct<
|
|||
>(
|
||||
name: TName,
|
||||
type: TType,
|
||||
_typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
) {
|
||||
return this.field(
|
||||
name,
|
||||
new BigIntFieldDefinition(type, _typescriptType),
|
||||
new BigIntFieldDefinition(type, typeScriptType),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -386,12 +388,12 @@ export class Struct<
|
|||
TTypeScriptType = BigIntFieldType['TTypeScriptType']
|
||||
>(
|
||||
name: TName,
|
||||
_typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
) {
|
||||
return this.bigint(
|
||||
name,
|
||||
BigIntFieldType.Int64,
|
||||
_typescriptType
|
||||
typeScriptType
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -405,12 +407,12 @@ export class Struct<
|
|||
TTypeScriptType = BigIntFieldType['TTypeScriptType']
|
||||
>(
|
||||
name: TName,
|
||||
_typescriptType?: TTypeScriptType,
|
||||
typeScriptType?: TTypeScriptType,
|
||||
) {
|
||||
return this.bigint(
|
||||
name,
|
||||
BigIntFieldType.Uint64,
|
||||
_typescriptType
|
||||
typeScriptType
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -445,9 +447,10 @@ export class Struct<
|
|||
Uint8ArrayBufferFieldSubType
|
||||
> = (
|
||||
name: PropertyKey,
|
||||
options: any
|
||||
options: any,
|
||||
typeScriptType: any,
|
||||
): any => {
|
||||
return this.arrayBufferLike(name, Uint8ArrayBufferFieldSubType.Instance, options);
|
||||
return this.arrayBufferLike(name, Uint8ArrayBufferFieldSubType.Instance, options, typeScriptType);
|
||||
};
|
||||
|
||||
public string: BoundArrayBufferLikeFieldDefinitionCreator<
|
||||
|
@ -458,9 +461,10 @@ export class Struct<
|
|||
StringBufferFieldSubType
|
||||
> = (
|
||||
name: PropertyKey,
|
||||
options: any
|
||||
options: any,
|
||||
typeScriptType: any,
|
||||
): any => {
|
||||
return this.arrayBufferLike(name, StringBufferFieldSubType.Instance, options);
|
||||
return this.arrayBufferLike(name, StringBufferFieldSubType.Instance, options, typeScriptType);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -38,7 +38,8 @@ export abstract class BufferFieldSubType<TValue = unknown, TTypeScriptType = TVa
|
|||
}
|
||||
|
||||
/** An `BufferFieldSubType` that's actually an `Uint8Array` */
|
||||
export class Uint8ArrayBufferFieldSubType extends BufferFieldSubType<Uint8Array> {
|
||||
export class Uint8ArrayBufferFieldSubType<TTypeScriptType = Uint8Array>
|
||||
extends BufferFieldSubType<Uint8Array, TTypeScriptType> {
|
||||
public static readonly Instance = new Uint8ArrayBufferFieldSubType();
|
||||
|
||||
protected constructor() {
|
||||
|
@ -85,9 +86,10 @@ export abstract class BufferLikeFieldDefinition<
|
|||
TType extends BufferFieldSubType<any, any> = BufferFieldSubType<unknown, unknown>,
|
||||
TOptions = void,
|
||||
TOmitInitKey extends PropertyKey = never,
|
||||
TTypeScriptType = TType["TTypeScriptType"],
|
||||
> extends StructFieldDefinition<
|
||||
TOptions,
|
||||
TType['TTypeScriptType'],
|
||||
TTypeScriptType,
|
||||
TOmitInitKey
|
||||
>{
|
||||
public readonly type: TType;
|
||||
|
|
|
@ -7,9 +7,12 @@ export interface FixedLengthBufferLikeFieldOptions {
|
|||
export class FixedLengthBufferLikeFieldDefinition<
|
||||
TType extends BufferFieldSubType = BufferFieldSubType,
|
||||
TOptions extends FixedLengthBufferLikeFieldOptions = FixedLengthBufferLikeFieldOptions,
|
||||
TTypeScriptType = TType["TTypeScriptType"],
|
||||
> extends BufferLikeFieldDefinition<
|
||||
TType,
|
||||
TOptions
|
||||
TOptions,
|
||||
never,
|
||||
TTypeScriptType
|
||||
> {
|
||||
public getSize(): number {
|
||||
return this.options.length;
|
||||
|
|
|
@ -26,11 +26,13 @@ export interface VariableLengthBufferLikeFieldOptions<
|
|||
|
||||
export class VariableLengthBufferLikeFieldDefinition<
|
||||
TType extends BufferFieldSubType = BufferFieldSubType,
|
||||
TOptions extends VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions
|
||||
TOptions extends VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions,
|
||||
TTypeScriptType = TType["TTypeScriptType"],
|
||||
> extends BufferLikeFieldDefinition<
|
||||
TType,
|
||||
TOptions,
|
||||
TOptions['lengthField']
|
||||
TOptions['lengthField'],
|
||||
TTypeScriptType
|
||||
> {
|
||||
public getSize(): number {
|
||||
return 0;
|
||||
|
@ -47,7 +49,7 @@ export class VariableLengthBufferLikeFieldDefinition<
|
|||
public override create(
|
||||
options: Readonly<StructOptions>,
|
||||
struct: StructValue,
|
||||
value: TType['TTypeScriptType'],
|
||||
value: TTypeScriptType,
|
||||
array?: Uint8Array
|
||||
): VariableLengthBufferLikeStructFieldValue<this> {
|
||||
return new VariableLengthBufferLikeStructFieldValue(
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
"composite": true,
|
||||
/* Basic Options */
|
||||
"target": "ES2022", // /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
|
||||
"module": "Node12", // /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
|
||||
"module": "NodeNext", // /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
|
||||
"lib": [ // /* Specify library files to be included in the compilation. */
|
||||
"ESNext"
|
||||
],
|
||||
|
@ -34,7 +34,7 @@
|
|||
"noUncheckedIndexedAccess": true,
|
||||
"resolveJsonModule": true,
|
||||
/* Module Resolution Options */
|
||||
"moduleResolution": "Node12", // /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
||||
"moduleResolution": "NodeNext", // /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
||||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
||||
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
||||
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue