Skip to content
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

Rework inheritance #159

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions spec/inheritance-circular-references/barrel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './person.model';
export * from './employee.model';
export * from './part-time-employee.model';
9 changes: 9 additions & 0 deletions spec/inheritance-circular-references/company.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {jsonMember, jsonObject} from '../../src';
import {Person} from './barrel';

@jsonObject
export class Company {

@jsonMember
owner: Person;
}
6 changes: 6 additions & 0 deletions spec/inheritance-circular-references/employee.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {discriminatorProperty, jsonInheritance, jsonObject} from '../../src';
import {Person} from './barrel';

@jsonObject
export class Employee extends Person {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {TypedJSON} from '../../src';
import {Company} from './company.model';
import {PartTimeEmployee} from './part-time-employee.model';

describe('inheritance with circular references', () => {
it('should deserialize correctly', () => {
const result = TypedJSON.parse({
owner: {
name: 'George',
type: 'PartTimeEmployee',
},
}, Company);

expect(result.owner).toBeInstanceOf(PartTimeEmployee);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {jsonObject} from '../../src';
import {Employee} from './barrel';

@jsonObject
export class PartTimeEmployee extends Employee {
}
16 changes: 16 additions & 0 deletions spec/inheritance-circular-references/person.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {discriminatorProperty, jsonInheritance, jsonMember, jsonObject} from '../../src';
import {Employee, PartTimeEmployee} from './barrel';

@jsonInheritance(discriminatorProperty({
property: 'type',
types: () => ({
Employee: Employee,
PartTimeEmployee: PartTimeEmployee,
}),
}))
@jsonObject
export class Person {

@jsonMember
name: string;
}
199 changes: 199 additions & 0 deletions spec/json-inheritance.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import {
discriminatorProperty,
jsonInheritance,
jsonMember,
jsonObject,
TypedJSON,
} from '../src';

describe('jsonInheritance', () => {
describe('with custom resolvers', () => {
@jsonInheritance({
onSerializeType: (source, result) => {
result['test'] = 'hey';
return result;
},
resolveType: data => {
if ('badgeId' in data) {
return Employee;
} else if ('capital' in data) {
return Investor;
}

return Person;
},
})
@jsonObject
class Person {
name: string;
}

@jsonObject
class Employee extends Person {
badgeId: string;
}

@jsonObject
class Investor extends Person {
capital: number;
}

@jsonObject
class Company {

@jsonMember
owner: Person;
}

const typedJson = new TypedJSON(Company);

it('should use resolveType', () => {
const result = typedJson.parse({owner: {name: 'Bob', badgeId: 'avx5'}});
expect(result.owner).toBeInstanceOf(Employee);
});

it('should use onSerializeType', () => {
const company = new Company();
company.owner = new Employee();
company.owner.name = 'Lewis';
const result: any = typedJson.toPlainJson(company);
expect(result.owner.test).toEqual('hey');
});

it('onSerializeType should not modify source object', () => {
const company = new Company();
company.owner = new Employee();
typedJson.toPlainJson(company);
expect((company.owner as any).test).toBeUndefined();
});
});

describe('with discriminator', () => {
describe('and one jsonInheritance decorator', () => {
@jsonInheritance(discriminatorProperty({
property: 'type',
types: () => ({
Employee: Employee,
Investor: Investor,
PartTimeEmployee: PartTimeEmployee,
}),
}))
@jsonObject
class Person {
name: string;
}

@jsonObject
class Employee extends Person {
}

@jsonObject
class PartTimeEmployee extends Employee {
}

@jsonObject
class Investor extends Person {
}

@jsonObject
class Company {

@jsonMember
owner: Person;
}

it('should deserialize into correct type', () => {
const result = TypedJSON.parse({
owner: {
name: 'Jeff',
type: 'Investor',
},
}, Company);

expect(result.owner).toBeInstanceOf(Investor);
});

it('should have correct type property on serialization', () => {
const company = new Company();
company.owner = new Investor();

const result: any = TypedJSON.toPlainJson(company, Company);
expect(result.owner.type).toEqual('Investor');
});

describe('and nested inheritance', () => {
it('should deserialize into correct type', () => {
const result = TypedJSON.parse({
owner: {
name: 'George',
type: 'PartTimeEmployee',
},
}, Company);

expect(result.owner).toBeInstanceOf(PartTimeEmployee);
});

it('should have correct type property on serialization', () => {
const company = new Company();
company.owner = new PartTimeEmployee();

const result: any = TypedJSON.toPlainJson(company, Company);
expect(result.owner.type).toEqual('PartTimeEmployee');
});
});
});

describe('and multiple jsonInheritance decorators', () => {
@jsonInheritance(discriminatorProperty({
property: 'type',
types: () => ({
Employee: Employee,
}),
}))
@jsonObject
class Person {
name: string;
}

@jsonInheritance(discriminatorProperty({
property: 'type',
types: () => ({
PartTimeEmployee: PartTimeEmployee,
}),
}))
@jsonObject
class Employee extends Person {
}

@jsonObject
class PartTimeEmployee extends Employee {
}

@jsonObject
class Company {

@jsonMember
owner: Person;
}

it('should deserialize into correct type', () => {
const result = TypedJSON.parse({
owner: {
name: 'George',
type: 'PartTimeEmployee',
},
}, Company);

expect(result.owner).toBeInstanceOf(PartTimeEmployee);
});

it('should have correct type property on serialization', () => {
const company = new Company();
company.owner = new PartTimeEmployee();

const result: any = TypedJSON.toPlainJson(company, Company);
expect(result.owner.type).toEqual('PartTimeEmployee');
});
});
});
});
24 changes: 15 additions & 9 deletions spec/lazy-types/polymorphism-abstract-class.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import {jsonArrayMember, jsonMember, jsonObject, TypedJSON} from '../../src';
import {jsonArrayMember, jsonInheritance, jsonMember, jsonObject, TypedJSON} from '../../src';
import {isEqual} from '../utils/object-compare';

describe('lazy, polymorphic abstract classes', () => {
@jsonInheritance({
resolveType: data => {
if ('inputType' in data) {
return SmallNode;
} else {
return BigNode;
}
},
})
@jsonObject()
abstract class Node {
@jsonMember
name: string;
Expand Down Expand Up @@ -31,11 +41,9 @@ describe('lazy, polymorphic abstract classes', () => {
}
}

@jsonObject({
knownTypes: [BigNode, SmallNode],
})
@jsonObject()
class Graph {
@jsonArrayMember(() => Node)
@jsonArrayMember(Node)
nodes: Array<Node>;

@jsonMember
Expand All @@ -46,8 +54,6 @@ describe('lazy, polymorphic abstract classes', () => {
}
}

let portTypeIndex = 0;

function randPortType() {
const types = [
'string',
Expand All @@ -57,7 +63,7 @@ describe('lazy, polymorphic abstract classes', () => {
'void',
];

return types[portTypeIndex++ % types.length];
return types[Math.floor(Math.random() * types.length)];
}

function test(log: boolean) {
Expand All @@ -66,7 +72,7 @@ describe('lazy, polymorphic abstract classes', () => {
for (let i = 0; i < 20; i++) {
let node: Node;

if (i % 2 === 0) {
if (Math.random() < 0.25) {
const bigNode = new BigNode();

bigNode.inputs = [
Expand Down
26 changes: 22 additions & 4 deletions spec/lazy-types/polymorphism-custom-names.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import {jsonArrayMember, jsonMember, jsonObject, TypedJSON} from '../../src';
import {jsonArrayMember, jsonInheritance, jsonMember, jsonObject, TypedJSON} from '../../src';
import {isEqual} from '../utils/object-compare';

describe('lazy, polymorphic custom names', () => {
@jsonObject
@jsonInheritance({
resolveType: data => {
if ('invest-amount' in data) {
return Investor;
}

return Employee;
},
})
@jsonObject()
class Person {
@jsonMember({name: 'first-name'})
firstName: string;
Expand All @@ -18,7 +27,16 @@ describe('lazy, polymorphic custom names', () => {
}
}

@jsonObject
@jsonInheritance({
resolveType: data => {
if ('work-hours' in data) {
return PartTimeEmployee;
}

return Employee;
},
})
@jsonObject()
class Employee extends Person {
@jsonMember
salary: number;
Expand Down Expand Up @@ -59,7 +77,7 @@ describe('lazy, polymorphic custom names', () => {
}
}

@jsonObject({name: 'company', knownTypes: [PartTimeEmployee, Investor]})
@jsonObject({name: 'company'})
class Company {
@jsonMember
name: string;
Expand Down
Loading