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 name Name of the field
|
||||||
* @param type `Array.SubType.ArrayBuffer` or `Array.SubType.String`
|
* @param type `Array.SubType.ArrayBuffer` or `Array.SubType.String`
|
||||||
* @param options Fixed-length array options
|
* @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.
|
* 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,
|
name: TName,
|
||||||
type: TType,
|
type: TType,
|
||||||
options: FixedLengthBufferLikeFieldOptions,
|
options: FixedLengthBufferLikeFieldOptions,
|
||||||
typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
): AddFieldDescriptor<
|
): AddFieldDescriptor<
|
||||||
TFields,
|
TFields,
|
||||||
TOmitInitKey,
|
TOmitInitKey,
|
||||||
|
@ -88,7 +88,7 @@ interface ArrayBufferLikeFieldCreator<
|
||||||
name: TName,
|
name: TName,
|
||||||
type: TType,
|
type: TType,
|
||||||
options: TOptions,
|
options: TOptions,
|
||||||
typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
): AddFieldDescriptor<
|
): AddFieldDescriptor<
|
||||||
TFields,
|
TFields,
|
||||||
TOmitInitKey,
|
TOmitInitKey,
|
||||||
|
@ -118,7 +118,7 @@ interface BoundArrayBufferLikeFieldDefinitionCreator<
|
||||||
>(
|
>(
|
||||||
name: TName,
|
name: TName,
|
||||||
options: FixedLengthBufferLikeFieldOptions,
|
options: FixedLengthBufferLikeFieldOptions,
|
||||||
typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
): AddFieldDescriptor<
|
): AddFieldDescriptor<
|
||||||
TFields,
|
TFields,
|
||||||
TOmitInitKey,
|
TOmitInitKey,
|
||||||
|
@ -127,7 +127,8 @@ interface BoundArrayBufferLikeFieldDefinitionCreator<
|
||||||
TName,
|
TName,
|
||||||
FixedLengthBufferLikeFieldDefinition<
|
FixedLengthBufferLikeFieldDefinition<
|
||||||
TType,
|
TType,
|
||||||
FixedLengthBufferLikeFieldOptions
|
FixedLengthBufferLikeFieldOptions,
|
||||||
|
TTypeScriptType
|
||||||
>
|
>
|
||||||
>;
|
>;
|
||||||
|
|
||||||
|
@ -139,7 +140,7 @@ interface BoundArrayBufferLikeFieldDefinitionCreator<
|
||||||
>(
|
>(
|
||||||
name: TName,
|
name: TName,
|
||||||
options: TOptions,
|
options: TOptions,
|
||||||
typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
): AddFieldDescriptor<
|
): AddFieldDescriptor<
|
||||||
TFields,
|
TFields,
|
||||||
TOmitInitKey,
|
TOmitInitKey,
|
||||||
|
@ -148,7 +149,8 @@ interface BoundArrayBufferLikeFieldDefinitionCreator<
|
||||||
TName,
|
TName,
|
||||||
VariableLengthBufferLikeFieldDefinition<
|
VariableLengthBufferLikeFieldDefinition<
|
||||||
TType,
|
TType,
|
||||||
TOptions
|
TOptions,
|
||||||
|
TTypeScriptType
|
||||||
>
|
>
|
||||||
>;
|
>;
|
||||||
}
|
}
|
||||||
|
@ -251,11 +253,11 @@ export class Struct<
|
||||||
>(
|
>(
|
||||||
name: TName,
|
name: TName,
|
||||||
type: TType,
|
type: TType,
|
||||||
_typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
) {
|
) {
|
||||||
return this.field(
|
return this.field(
|
||||||
name,
|
name,
|
||||||
new NumberFieldDefinition(type, _typescriptType),
|
new NumberFieldDefinition(type, typeScriptType),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -267,12 +269,12 @@ export class Struct<
|
||||||
TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType']
|
TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType']
|
||||||
>(
|
>(
|
||||||
name: TName,
|
name: TName,
|
||||||
_typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
) {
|
) {
|
||||||
return this.number(
|
return this.number(
|
||||||
name,
|
name,
|
||||||
NumberFieldType.Int8,
|
NumberFieldType.Int8,
|
||||||
_typescriptType
|
typeScriptType
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -284,12 +286,12 @@ export class Struct<
|
||||||
TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType']
|
TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType']
|
||||||
>(
|
>(
|
||||||
name: TName,
|
name: TName,
|
||||||
_typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
) {
|
) {
|
||||||
return this.number(
|
return this.number(
|
||||||
name,
|
name,
|
||||||
NumberFieldType.Uint8,
|
NumberFieldType.Uint8,
|
||||||
_typescriptType
|
typeScriptType
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -301,12 +303,12 @@ export class Struct<
|
||||||
TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType']
|
TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType']
|
||||||
>(
|
>(
|
||||||
name: TName,
|
name: TName,
|
||||||
_typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
) {
|
) {
|
||||||
return this.number(
|
return this.number(
|
||||||
name,
|
name,
|
||||||
NumberFieldType.Int16,
|
NumberFieldType.Int16,
|
||||||
_typescriptType
|
typeScriptType
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,12 +320,12 @@ export class Struct<
|
||||||
TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType']
|
TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType']
|
||||||
>(
|
>(
|
||||||
name: TName,
|
name: TName,
|
||||||
_typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
) {
|
) {
|
||||||
return this.number(
|
return this.number(
|
||||||
name,
|
name,
|
||||||
NumberFieldType.Uint16,
|
NumberFieldType.Uint16,
|
||||||
_typescriptType
|
typeScriptType
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -335,12 +337,12 @@ export class Struct<
|
||||||
TTypeScriptType = (typeof NumberFieldType)['Int32']['TTypeScriptType']
|
TTypeScriptType = (typeof NumberFieldType)['Int32']['TTypeScriptType']
|
||||||
>(
|
>(
|
||||||
name: TName,
|
name: TName,
|
||||||
_typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
) {
|
) {
|
||||||
return this.number(
|
return this.number(
|
||||||
name,
|
name,
|
||||||
NumberFieldType.Int32,
|
NumberFieldType.Int32,
|
||||||
_typescriptType
|
typeScriptType
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -352,12 +354,12 @@ export class Struct<
|
||||||
TTypeScriptType = (typeof NumberFieldType)['Uint32']['TTypeScriptType']
|
TTypeScriptType = (typeof NumberFieldType)['Uint32']['TTypeScriptType']
|
||||||
>(
|
>(
|
||||||
name: TName,
|
name: TName,
|
||||||
typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
) {
|
) {
|
||||||
return this.number(
|
return this.number(
|
||||||
name,
|
name,
|
||||||
NumberFieldType.Uint32,
|
NumberFieldType.Uint32,
|
||||||
typescriptType
|
typeScriptType
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -368,11 +370,11 @@ export class Struct<
|
||||||
>(
|
>(
|
||||||
name: TName,
|
name: TName,
|
||||||
type: TType,
|
type: TType,
|
||||||
_typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
) {
|
) {
|
||||||
return this.field(
|
return this.field(
|
||||||
name,
|
name,
|
||||||
new BigIntFieldDefinition(type, _typescriptType),
|
new BigIntFieldDefinition(type, typeScriptType),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -386,12 +388,12 @@ export class Struct<
|
||||||
TTypeScriptType = BigIntFieldType['TTypeScriptType']
|
TTypeScriptType = BigIntFieldType['TTypeScriptType']
|
||||||
>(
|
>(
|
||||||
name: TName,
|
name: TName,
|
||||||
_typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
) {
|
) {
|
||||||
return this.bigint(
|
return this.bigint(
|
||||||
name,
|
name,
|
||||||
BigIntFieldType.Int64,
|
BigIntFieldType.Int64,
|
||||||
_typescriptType
|
typeScriptType
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -405,12 +407,12 @@ export class Struct<
|
||||||
TTypeScriptType = BigIntFieldType['TTypeScriptType']
|
TTypeScriptType = BigIntFieldType['TTypeScriptType']
|
||||||
>(
|
>(
|
||||||
name: TName,
|
name: TName,
|
||||||
_typescriptType?: TTypeScriptType,
|
typeScriptType?: TTypeScriptType,
|
||||||
) {
|
) {
|
||||||
return this.bigint(
|
return this.bigint(
|
||||||
name,
|
name,
|
||||||
BigIntFieldType.Uint64,
|
BigIntFieldType.Uint64,
|
||||||
_typescriptType
|
typeScriptType
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -445,9 +447,10 @@ export class Struct<
|
||||||
Uint8ArrayBufferFieldSubType
|
Uint8ArrayBufferFieldSubType
|
||||||
> = (
|
> = (
|
||||||
name: PropertyKey,
|
name: PropertyKey,
|
||||||
options: any
|
options: any,
|
||||||
|
typeScriptType: any,
|
||||||
): any => {
|
): any => {
|
||||||
return this.arrayBufferLike(name, Uint8ArrayBufferFieldSubType.Instance, options);
|
return this.arrayBufferLike(name, Uint8ArrayBufferFieldSubType.Instance, options, typeScriptType);
|
||||||
};
|
};
|
||||||
|
|
||||||
public string: BoundArrayBufferLikeFieldDefinitionCreator<
|
public string: BoundArrayBufferLikeFieldDefinitionCreator<
|
||||||
|
@ -458,9 +461,10 @@ export class Struct<
|
||||||
StringBufferFieldSubType
|
StringBufferFieldSubType
|
||||||
> = (
|
> = (
|
||||||
name: PropertyKey,
|
name: PropertyKey,
|
||||||
options: any
|
options: any,
|
||||||
|
typeScriptType: any,
|
||||||
): 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` */
|
/** 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();
|
public static readonly Instance = new Uint8ArrayBufferFieldSubType();
|
||||||
|
|
||||||
protected constructor() {
|
protected constructor() {
|
||||||
|
@ -85,9 +86,10 @@ export abstract class BufferLikeFieldDefinition<
|
||||||
TType extends BufferFieldSubType<any, any> = BufferFieldSubType<unknown, unknown>,
|
TType extends BufferFieldSubType<any, any> = BufferFieldSubType<unknown, unknown>,
|
||||||
TOptions = void,
|
TOptions = void,
|
||||||
TOmitInitKey extends PropertyKey = never,
|
TOmitInitKey extends PropertyKey = never,
|
||||||
|
TTypeScriptType = TType["TTypeScriptType"],
|
||||||
> extends StructFieldDefinition<
|
> extends StructFieldDefinition<
|
||||||
TOptions,
|
TOptions,
|
||||||
TType['TTypeScriptType'],
|
TTypeScriptType,
|
||||||
TOmitInitKey
|
TOmitInitKey
|
||||||
>{
|
>{
|
||||||
public readonly type: TType;
|
public readonly type: TType;
|
||||||
|
|
|
@ -7,9 +7,12 @@ export interface FixedLengthBufferLikeFieldOptions {
|
||||||
export class FixedLengthBufferLikeFieldDefinition<
|
export class FixedLengthBufferLikeFieldDefinition<
|
||||||
TType extends BufferFieldSubType = BufferFieldSubType,
|
TType extends BufferFieldSubType = BufferFieldSubType,
|
||||||
TOptions extends FixedLengthBufferLikeFieldOptions = FixedLengthBufferLikeFieldOptions,
|
TOptions extends FixedLengthBufferLikeFieldOptions = FixedLengthBufferLikeFieldOptions,
|
||||||
|
TTypeScriptType = TType["TTypeScriptType"],
|
||||||
> extends BufferLikeFieldDefinition<
|
> extends BufferLikeFieldDefinition<
|
||||||
TType,
|
TType,
|
||||||
TOptions
|
TOptions,
|
||||||
|
never,
|
||||||
|
TTypeScriptType
|
||||||
> {
|
> {
|
||||||
public getSize(): number {
|
public getSize(): number {
|
||||||
return this.options.length;
|
return this.options.length;
|
||||||
|
|
|
@ -26,11 +26,13 @@ export interface VariableLengthBufferLikeFieldOptions<
|
||||||
|
|
||||||
export class VariableLengthBufferLikeFieldDefinition<
|
export class VariableLengthBufferLikeFieldDefinition<
|
||||||
TType extends BufferFieldSubType = BufferFieldSubType,
|
TType extends BufferFieldSubType = BufferFieldSubType,
|
||||||
TOptions extends VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions
|
TOptions extends VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions,
|
||||||
|
TTypeScriptType = TType["TTypeScriptType"],
|
||||||
> extends BufferLikeFieldDefinition<
|
> extends BufferLikeFieldDefinition<
|
||||||
TType,
|
TType,
|
||||||
TOptions,
|
TOptions,
|
||||||
TOptions['lengthField']
|
TOptions['lengthField'],
|
||||||
|
TTypeScriptType
|
||||||
> {
|
> {
|
||||||
public getSize(): number {
|
public getSize(): number {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -47,7 +49,7 @@ export class VariableLengthBufferLikeFieldDefinition<
|
||||||
public override create(
|
public override create(
|
||||||
options: Readonly<StructOptions>,
|
options: Readonly<StructOptions>,
|
||||||
struct: StructValue,
|
struct: StructValue,
|
||||||
value: TType['TTypeScriptType'],
|
value: TTypeScriptType,
|
||||||
array?: Uint8Array
|
array?: Uint8Array
|
||||||
): VariableLengthBufferLikeStructFieldValue<this> {
|
): VariableLengthBufferLikeStructFieldValue<this> {
|
||||||
return new VariableLengthBufferLikeStructFieldValue(
|
return new VariableLengthBufferLikeStructFieldValue(
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
"composite": true,
|
"composite": true,
|
||||||
/* Basic Options */
|
/* Basic Options */
|
||||||
"target": "ES2022", // /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
|
"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. */
|
"lib": [ // /* Specify library files to be included in the compilation. */
|
||||||
"ESNext"
|
"ESNext"
|
||||||
],
|
],
|
||||||
|
@ -34,7 +34,7 @@
|
||||||
"noUncheckedIndexedAccess": true,
|
"noUncheckedIndexedAccess": true,
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
/* Module Resolution Options */
|
/* 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. */
|
// "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'. */
|
// "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. */
|
// "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