Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
feat: allow any number of arguments in polycon constructors (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr authored Aug 24, 2022
1 parent 19abfd2 commit 4cbcd4f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
12 changes: 6 additions & 6 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class PetFactory implements IPolyconFactory {
type: string,
scope: Construct,
id: string,
props?: any
...args: any[]
): Construct {
switch (type) {
case DOG_ID:
return new Labrador(scope, id, props);
return new Labrador(scope, id, ...args);
case CAT_ID:
return new Kitten(scope, id, props);
return new Kitten(scope, id, ...args);
default:
throw new Error(`Type "${type}" not implemented.`);
}
Expand Down
15 changes: 10 additions & 5 deletions src/polycon-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ export class Polycons {
* @param type The type identifier
* @param scope The construct scope
* @param id The construct identifier
* @param props The construct props
* @param args The rest of the construct's arguments
* @returns The resolved construct
*/
public static newInstance(
type: string,
scope: IConstruct,
id: string,
props?: any
...args: any[]
) {
const factory = polyconFactoryOf(scope);

Expand All @@ -54,7 +54,7 @@ export class Polycons {
);
}

return factory.resolve(type, scope, id, props);
return factory.resolve(type, scope, id, ...args);
}

private constructor() {}
Expand Down Expand Up @@ -91,8 +91,13 @@ export interface IPolyconFactory {
* @param type The type identifier
* @param scope The construct scope
* @param id The construct identifier
* @param props The construct props
* @param args The rest of the construct's arguments
* @returns The resolved construct
*/
resolve(type: string, scope: IConstruct, id: string, props?: any): IConstruct;
resolve(
type: string,
scope: IConstruct,
id: string,
...args: any[]
): IConstruct;
}
20 changes: 12 additions & 8 deletions test/polycon-factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("a polycon", () => {
it("cannot be instantiated if the registered factory does not support it", () => {
const app = new App();
Polycons.register(app, new PoodleFactory());
expect(() => new Cat(app, "cat", { scritches: 5 })).toThrowError(
expect(() => new Cat(app, "cat", "alice", { scritches: 5 })).toThrowError(
/Type test\.cat not implemented/
);
});
Expand Down Expand Up @@ -107,7 +107,7 @@ describe("polycons", () => {
it("polycons can be created without base classes", () => {
const app = new App();
Polycons.register(app, new ShorthairFactory());
const cat = new Shorthair(app, "muffins", { scritches: 5 });
const cat = new Shorthair(app, "muffins", "alice", { scritches: 5 });

expect(cat.toString()).toEqual("Shorthair cat with 5 scritches.");
expect(app.synth()).toStrictEqual(["root", "root/muffins"]);
Expand Down Expand Up @@ -287,8 +287,8 @@ interface CatProps {
// example of a polycon with no base class

class Cat {
constructor(scope: Construct, id: string, props: CatProps) {
return Polycons.newInstance(CAT_ID, scope, id, props) as Cat;
constructor(scope: Construct, id: string, owner: string, props: CatProps) {
return Polycons.newInstance(CAT_ID, scope, id, owner, props) as Cat;
}

public toString(): string {
Expand All @@ -297,10 +297,12 @@ class Cat {
}

class Shorthair extends Construct {
public readonly owner: string;
public readonly scritches: number;

constructor(scope: Construct, id: string, props: CatProps) {
constructor(scope: Construct, id: string, owner: string, props: CatProps) {
super(scope, id);
this.owner = owner;
this.scritches = props.scritches;
}

Expand Down Expand Up @@ -338,10 +340,11 @@ class LabradorFactory implements IPolyconFactory {
type: string,
scope: IConstruct,
id: string,
props?: any
...args: any[]
): IConstruct {
switch (type) {
case DOG_ID:
const props = args[0];
return new Labrador(scope, id, props);
default:
throw new Error(`Type ${type} not implemented.`);
Expand All @@ -354,11 +357,12 @@ class ShorthairFactory implements IPolyconFactory {
type: string,
scope: IConstruct,
id: string,
props?: any
...args: any[]
): IConstruct {
switch (type) {
case CAT_ID:
return new Shorthair(scope, id, props);
const [owner, props] = args;
return new Shorthair(scope, id, owner, props);
default:
throw new Error(`Type ${type} not implemented.`);
}
Expand Down

0 comments on commit 4cbcd4f

Please sign in to comment.