Skip to content

Commit

Permalink
✨ support condition
Browse files Browse the repository at this point in the history
  • Loading branch information
XiYang6666 committed Apr 27, 2024
1 parent c56be15 commit 7c42b36
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/struct/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { DataType } from "../dataType";
import { definePackage } from "../package/definePackages";
import { PackageType } from "../package/package";

export type StructConf = Array<{ name: string; type: DataType<any>; default?: any }>;
export type StructConf = Array<{ name: string; type: DataType<any>; default?: any; condition?: ItemCondition }>;
export type StructData = { [row_name: string]: any };

export type ItemCondition = (data: StructData) => boolean;

export class Struct<T extends StructData> implements DataType<StructData & T> {
readonly config: StructConf;

Expand All @@ -14,16 +16,18 @@ export class Struct<T extends StructData> implements DataType<StructData & T> {

write(value: T): Buffer {
const result: Buffer[] = [];
for (let { name, type, default: defaultValue } of this.config) {
const fixedValue = value[name];
result.push(type.write(fixedValue ?? defaultValue));
for (let { name, type, default: defaultValue, condition } of this.config) {
if (condition && !condition(value)) continue;
const itemValue = value[name] ?? defaultValue;
result.push(type.write(itemValue));
}
return Buffer.concat(result);
}
read(buffer: Buffer, offset: number): [T, number] {
let structOffset = 0;
let result: T = {} as T;
for (let { name, type } of this.config) {
for (let { name, type, condition } of this.config) {
if (condition && !condition(result)) continue;
let [value, rowOffset] = type.read(buffer, offset + structOffset);
(result as StructData)[name] = value;
structOffset += rowOffset;
Expand Down
20 changes: 19 additions & 1 deletion src/struct/structBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseTypes } from "../baseTypes";
import { DataType } from "../dataType";
import { TypeGenerator } from "../defineType";
import { Struct, StructConf, StructData } from "./struct";
import { ItemCondition, Struct, StructConf, StructData } from "./struct";

export class StructBuilder {
static new() {
Expand All @@ -18,13 +18,31 @@ export class StructBuilder {
this.structConf.push({ name, type, ...(default_value === undefined ? null : { default: default_value }) });
return this;
}

generatorRow<C extends any[], T>(name: string, generator: TypeGenerator<C, T>, default_value?: T) {
return (...param: C) => {
this.row(name, generator(...param), default_value);
return this;
};
}

setCondition(row_name: string, condition: ItemCondition): this;
setCondition(condition: ItemCondition): this;
setCondition(...args: [string, ItemCondition] | [ItemCondition]) {
if (typeof args[0] === "function") {
const [condition] = args;
this.structConf[this.structConf.length - 1].condition = condition;
} else {
const [row_name, condition] = args;
for (let [index, row] of this.structConf.entries())
if (row.name == row_name) {
this.structConf[index].condition = condition;
break;
}
}
return this;
}

// Int8

rowInt8(name: string, default_value?: number) {
Expand Down

0 comments on commit 7c42b36

Please sign in to comment.