Skip to content
This repository has been archived by the owner on Nov 28, 2019. It is now read-only.

Commit

Permalink
fix: inheritance v2
Browse files Browse the repository at this point in the history
  • Loading branch information
elderapo committed Feb 13, 2019
1 parent aba79f4 commit aa4a664
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/metadataHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ProtobufLiteMetadata } from "./ProtobufLiteMetadata";
import { getPrototypeChain } from "./utils";

// dont use Reflect.defineMetadata because it's slow AF
let weakMap = new WeakMap<Function, ProtobufLiteMetadata>();
Expand All @@ -16,5 +17,13 @@ export const getMetadataObject = (Class: Function): ProtobufLiteMetadata => {
};

export const hasMetadataObject = (Class: Function): boolean => {
return weakMap.has(Class);
const chain = getPrototypeChain(Class);

for (let prototype of chain) {
if (weakMap.has(prototype.constructor)) {
return true;
}
}

return false;
};
32 changes: 32 additions & 0 deletions test/calculateProtobufLiteClassChecksum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,38 @@ describe("getFieldInfo", () => {
]);
});

it("should return correct fields for inhreited classes v3", () => {
class Parent {
@ProtobufLiteProperty()
someField: string;
}

class Child extends Parent {}
class Child1 extends Child {}
class Child2 extends Child1 {}
class Child3 extends Child1 {
@ProtobufLiteProperty()
c3: string;
}

expect(getFieldInfo(Child)).toMatchObject([
{ propertyKey: "someField", prototype: "string", rule: "required" }
]);

expect(getFieldInfo(Child1)).toMatchObject([
{ propertyKey: "someField", prototype: "string", rule: "required" }
]);

expect(getFieldInfo(Child2)).toMatchObject([
{ propertyKey: "someField", prototype: "string", rule: "required" }
]);

expect(getFieldInfo(Child3)).toMatchObject([
{ propertyKey: "someField", prototype: "string", rule: "required" },
{ propertyKey: "c3", prototype: "string", rule: "required" }
]);
});

it("should throw if child overwrites parent fields", () => {
class Parent {
@ProtobufLiteProperty()
Expand Down

0 comments on commit aa4a664

Please sign in to comment.