Skip to content

Commit

Permalink
inheritance support (#7)
Browse files Browse the repository at this point in the history
* use prototype chain for searching fields
  • Loading branch information
vcfvct authored Dec 10, 2021
1 parent cfe0415 commit 1e0e1e7
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
plugins: ["@typescript-eslint"],
extends: [
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This lib provide decorator for fixed width file so that the domain class and spe
## Example
This lib uses [reflect-metadata](https://github.com/rbuckton/reflect-metadata) for Meta programming.

Inheritance is supported that fields decorated in parent classes will also be included/processed.

### Model definition

```typescript
Expand All @@ -17,7 +19,7 @@ export class Transaction extends FixedWidthConvertible {

@FixedWidth({ start: 5, width: 40 })
parentName: string;

@FixedWidth({ start: 45, width: 40, format: { type: DataType.Float } })
taxAmount: number;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fixed-width-ts-decorator",
"version": "1.1.0",
"version": "1.2.0",
"description": "Fixed width file handler with TypeScript Decorator",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
14 changes: 13 additions & 1 deletion src/fixed-width-convertible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export abstract class FixedWidthConvertible {
}

static convertFixedWidth<T extends Record<string, any>>(line: string, target: T): T {
const fields = Reflect.getMetadata(fixedWidthVariableKey, target.constructor, fixedWidthVariableKey);
const fields = this.getAllFields(target.constructor);
for (const field of fields) {
const options: FixedWidthOptions = Reflect.getMetadata(fixedWidthMetadataKey, target, field);
const value = line.substring(options.start, options.start + options.width).trim();
Expand All @@ -21,5 +21,17 @@ export abstract class FixedWidthConvertible {
}
return target;
}

/**
* @param clz the class/constructor
* @returns the fields decorated with @FixedWidth all the way up the prototype chain.
*/
static getAllFields(clz: Record<string, any>): string[] {
if(!clz) return [];
const fields: string[] | undefined = Reflect.getMetadata(fixedWidthVariableKey, clz, fixedWidthVariableKey);
// get `__proto__` and (recursively) all parent classes
const rs = new Set([...(fields || []), ...this.getAllFields(Object.getPrototypeOf(clz))]);
return Array.from(rs);
}
}

18 changes: 18 additions & 0 deletions src/fixed-width-decorator.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import { FixedWidthConvertible } from './fixed-width-convertible';
import { ChildTransaction } from './scripts/ChildTransaction';
import { Transaction } from './scripts/Transaction';

describe('fixed width file test', () => {
Expand Down Expand Up @@ -35,5 +36,22 @@ describe('fixed width file test', () => {
expect(rs[1].taxAmount).toBe(123.45);
expect(rs[2].taxAmount).toBe(12.35);
expect(rs[0].clientId).toBe('20000');
// should not have child class field
expect((rs[0] as any).paymentId).toBeUndefined();
});

test('should get Field from parent class', () => {
const rs: Array<ChildTransaction> = lines.map(line => {
const trans = new ChildTransaction();
FixedWidthConvertible.convertFixedWidth(line, trans);
return trans;
});
expect(rs.length).toBe(4);
// should have parent class fields
expect(rs[1].taxAmount).toBe(123.45);
expect(rs[2].taxAmount).toBe(12.35);
expect(rs[0].clientId).toBe('20000');
expect(rs[0].paymentId).not.toBeUndefined();
});

});
7 changes: 7 additions & 0 deletions src/scripts/ChildTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { FixedWidth } from '../fixed-width-decorator';
import { Transaction } from './Transaction';

export class ChildTransaction extends Transaction {
@FixedWidth({ start: 225, width: 40 })
paymentId: string;
}
4 changes: 1 addition & 3 deletions src/scripts/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Transaction extends FixedWidthConvertible {

@FixedWidth({ start: 5, width: 40 })
parentName: string;

@FixedWidth({ start: 45, width: 40, format: { type: DataType.Float } })
taxAmount: number;

Expand All @@ -31,8 +31,6 @@ export class Transaction extends FixedWidthConvertible {
@FixedWidth({ start: 145, width: 40 })
userId: string;

@FixedWidth({ start: 225, width: 40 })
paymentId: string;
// other non-decorated fields
otherField: string;
}
3 changes: 2 additions & 1 deletion src/scripts/trigger.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'reflect-metadata';
import fs from 'fs';
import { Transaction } from './Transaction';
import { ChildTransaction } from './ChildTransaction';

(() => {
const file = fs.readFileSync('data/sample.txt', { encoding: 'utf8' });
const lines = file.split('\n').filter(l => l);
const rs: Array<Transaction> = lines.map(line => {
const trans = new Transaction();
const trans = new ChildTransaction();
trans.convertFixedWidth(line);
// FixedWidthConvertible.convertFixedWidth(line, trans);
return trans;
Expand Down

0 comments on commit 1e0e1e7

Please sign in to comment.