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

Commit

Permalink
feat: remove internals from exports and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
elderapo committed Feb 16, 2019
1 parent 1b376ee commit 1e72acc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 70 deletions.
45 changes: 25 additions & 20 deletions src/ProtobufLiteMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Field, Type } from "protobufjs/light";
import { ICustomFieldCodec, InternalCodecs } from "./codecs";
import { getMetadataObject, hasMetadataObject } from "./metadataHelpers";
import { defaultProtobufLitePropertyOptions } from "./ProtobufLiteProperty";
import { Constructable, getPrototypeChain, ensureBuffer } from "./utils";
import { Constructable, ensureBuffer, getPrototypeChain } from "./utils";

export interface IProtobufLitePropertyOptions {
optional?: boolean;
Expand All @@ -14,7 +14,7 @@ export interface IRegisterPropertyOptions {
targetProperty: Object;
propertyKey: string;
MessageClass: Function;
decoratorOptions?: IProtobufLitePropertyOptions;
decoratorOptions: IProtobufLitePropertyOptions;
}

const jsToProtobufTypesMap: { [key: string]: string } = {
Expand All @@ -24,7 +24,7 @@ const jsToProtobufTypesMap: { [key: string]: string } = {
Buffer: "bytes"
};

interface IFieldOptions {
export interface IFieldOptions {
key: string;
MessageClass: Function;
decoratorOptions: IProtobufLitePropertyOptions;
Expand All @@ -33,7 +33,7 @@ interface IFieldOptions {

export interface IFieldInfo {
propertyKey: string;
prototype: any;
prototype: string;
rule: string;
}

Expand Down Expand Up @@ -69,10 +69,6 @@ export class ProtobufLiteMetadata {
throw new Error("Tried to register property on wrong ProtobufLiteMetadata instance?");
}

decoratorOptions = decoratorOptions
? Object.assign({}, defaultProtobufLitePropertyOptions, decoratorOptions)
: defaultProtobufLitePropertyOptions;

let type = Reflect.getMetadata<Function>("design:type", targetProperty, propertyKey);
let typeFromDecoratorOptions =
decoratorOptions && decoratorOptions.type ? decoratorOptions.type() : null;
Expand All @@ -82,11 +78,11 @@ export class ProtobufLiteMetadata {
const isBuffer = type === Buffer;

if (isSymbol) {
throw new Error(`Sorry Symbol is not serializable...`);
throw new Error(`Sorry, Symbol is not serializable...`);
}

if (isArray && decoratorOptions.optional) {
throw new Error(`Field cannot be optional and array at the same time!`);
throw new Error(`Field cannot be optional and be and array at the same time!`);
}

if (isArray) {
Expand All @@ -103,6 +99,11 @@ export class ProtobufLiteMetadata {
if (typeFromDecoratorOptions && typeFromDecoratorOptions !== type) {
// "classProperty: ISomeInterface" <- most likely so we fallback to typeFromDecoratorOptions type
if (type === Object) {
/* istanbul ignore next line */
if (!typeFromDecoratorOptions) {
throw new Error(`For interfaces { type: () => TYPE } has to be specified in options!`);
}

type = typeFromDecoratorOptions;
} else {
throw new Error(
Expand Down Expand Up @@ -165,10 +166,6 @@ export class ProtobufLiteMetadata {
const className = this.getMessageClassName();
const t = new Type(className);

const prototypes = getPrototypeChain(this.MessageClass)
.reverse()
.filter(p => p !== this.MessageClass.prototype);

let fieldIndex = 0;

const collectedFieldsInfo = this.collectFieldsInfo();
Expand Down Expand Up @@ -206,7 +203,7 @@ export class ProtobufLiteMetadata {
}

public runCustomEncoders(payload: any) {
// optimization so copy is not done if not needed
// optimization so copy is not made if not needed
if (this.customCodecs.length === 0) {
return payload;
}
Expand Down Expand Up @@ -273,10 +270,22 @@ export class ProtobufLiteMetadata {
}
}

private getFieldsInfo() {
public getFieldsInfo() {
return this.fieldsInfo;
}

public getChildTypes() {
return this.childTypes;
}

public getMessageClass() {
return this.MessageClass;
}

public getMessageClassName() {
return this.MessageClass.name;
}

public collectFieldsInfo() {
const prototypes = getPrototypeChain(this.MessageClass)
.reverse()
Expand Down Expand Up @@ -308,8 +317,4 @@ export class ProtobufLiteMetadata {

return fieldsInfo;
}

private getMessageClassName() {
return this.MessageClass.name;
}
}
4 changes: 4 additions & 0 deletions src/ProtobufLiteProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export const ProtobufLiteProperty = (decoratorOptions?: IProtobufLitePropertyOpt
const MessageClass = targetProperty.constructor;
const metadata = getMetadataObject(MessageClass);

decoratorOptions = decoratorOptions
? Object.assign({}, defaultProtobufLitePropertyOptions, decoratorOptions)
: defaultProtobufLitePropertyOptions;

metadata.registerProperty({
targetProperty,
propertyKey,
Expand Down
15 changes: 0 additions & 15 deletions src/getFieldInfo.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./getFieldInfo";
export * from "./encoderDecoderFunctions";
export * from "./ProtobufLiteProperty";
63 changes: 29 additions & 34 deletions test/getFieldInfo.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import "@abraham/reflection";
import { getFieldInfo, ProtobufLiteProperty } from "../src";

describe("getFieldInfo", () => {
it("should throw if provided class with no metadata", () => {
class C1 {}

expect(() => getFieldInfo(C1)).toThrowError();
});
import { ProtobufLiteProperty } from "../src";
import { getMetadataObject } from "../src/metadataHelpers";

describe("getMetadataObject(XXX).getFieldInfo", () => {
it("should return correct fields for strings", () => {
class C1 {
@ProtobufLiteProperty()
Expand All @@ -24,15 +19,15 @@ describe("getFieldInfo", () => {
names: string[];
}

expect(getFieldInfo(C1)).toMatchObject([
expect(getMetadataObject(C1).collectFieldsInfo()).toMatchObject([
{ propertyKey: "name", prototype: "string", rule: "required" }
]);

expect(getFieldInfo(C2)).toMatchObject([
expect(getMetadataObject(C2).collectFieldsInfo()).toMatchObject([
{ propertyKey: "name", prototype: "string", rule: "optional" }
]);

expect(getFieldInfo(C3)).toMatchObject([
expect(getMetadataObject(C3).collectFieldsInfo()).toMatchObject([
{ propertyKey: "names", prototype: "string", rule: "repeated" }
]);
});
Expand All @@ -53,15 +48,15 @@ describe("getFieldInfo", () => {
ages: number[];
}

expect(getFieldInfo(C1)).toMatchObject([
expect(getMetadataObject(C1).collectFieldsInfo()).toMatchObject([
{ propertyKey: "age", prototype: "int32", rule: "required" }
]);

expect(getFieldInfo(C2)).toMatchObject([
expect(getMetadataObject(C2).collectFieldsInfo()).toMatchObject([
{ propertyKey: "age", prototype: "int32", rule: "optional" }
]);

expect(getFieldInfo(C3)).toMatchObject([
expect(getMetadataObject(C3).collectFieldsInfo()).toMatchObject([
{ propertyKey: "ages", prototype: "int32", rule: "repeated" }
]);
});
Expand All @@ -82,15 +77,15 @@ describe("getFieldInfo", () => {
isTrues: boolean[];
}

expect(getFieldInfo(C1)).toMatchObject([
expect(getMetadataObject(C1).collectFieldsInfo()).toMatchObject([
{ propertyKey: "isTrue", prototype: "bool", rule: "required" }
]);

expect(getFieldInfo(C2)).toMatchObject([
expect(getMetadataObject(C2).collectFieldsInfo()).toMatchObject([
{ propertyKey: "isTrue", prototype: "bool", rule: "optional" }
]);

expect(getFieldInfo(C3)).toMatchObject([
expect(getMetadataObject(C3).collectFieldsInfo()).toMatchObject([
{ propertyKey: "isTrues", prototype: "bool", rule: "repeated" }
]);
});
Expand All @@ -111,15 +106,15 @@ describe("getFieldInfo", () => {
buffers: Buffer[];
}

expect(getFieldInfo(C1)).toMatchObject([
expect(getMetadataObject(C1).collectFieldsInfo()).toMatchObject([
{ propertyKey: "buffer", prototype: "bytes", rule: "required" }
]);

expect(getFieldInfo(C2)).toMatchObject([
expect(getMetadataObject(C2).collectFieldsInfo()).toMatchObject([
{ propertyKey: "buffer", prototype: "bytes", rule: "optional" }
]);

expect(getFieldInfo(C3)).toMatchObject([
expect(getMetadataObject(C3).collectFieldsInfo()).toMatchObject([
{ propertyKey: "buffers", prototype: "bytes", rule: "repeated" }
]);
});
Expand All @@ -140,15 +135,15 @@ describe("getFieldInfo", () => {
dates: Date[];
}

expect(getFieldInfo(C1)).toMatchObject([
expect(getMetadataObject(C1).collectFieldsInfo()).toMatchObject([
{ propertyKey: "date", prototype: "bytes", rule: "required" }
]);

expect(getFieldInfo(C2)).toMatchObject([
expect(getMetadataObject(C2).collectFieldsInfo()).toMatchObject([
{ propertyKey: "date", prototype: "bytes", rule: "optional" }
]);

expect(getFieldInfo(C3)).toMatchObject([
expect(getMetadataObject(C3).collectFieldsInfo()).toMatchObject([
{ propertyKey: "dates", prototype: "bytes", rule: "repeated" }
]);
});
Expand All @@ -164,7 +159,7 @@ describe("getFieldInfo", () => {
someOtherField: string;
}

expect(getFieldInfo(Child)).toMatchObject([
expect(getMetadataObject(Child).collectFieldsInfo()).toMatchObject([
{ propertyKey: "someField", prototype: "string", rule: "required" },
{ propertyKey: "someOtherField", prototype: "string", rule: "required" }
]);
Expand Down Expand Up @@ -196,29 +191,29 @@ describe("getFieldInfo", () => {
c5: string;
}

expect(getFieldInfo(C1)).toMatchObject([
expect(getMetadataObject(C1).collectFieldsInfo()).toMatchObject([
{ propertyKey: "c1", prototype: "string", rule: "required" }
]);

expect(getFieldInfo(C2)).toMatchObject([
expect(getMetadataObject(C2).collectFieldsInfo()).toMatchObject([
{ propertyKey: "c1", prototype: "string", rule: "required" },
{ propertyKey: "c2", prototype: "string", rule: "required" }
]);

expect(getFieldInfo(C3)).toMatchObject([
expect(getMetadataObject(C3).collectFieldsInfo()).toMatchObject([
{ propertyKey: "c1", prototype: "string", rule: "required" },
{ propertyKey: "c2", prototype: "string", rule: "required" },
{ propertyKey: "c3", prototype: "string", rule: "required" }
]);

expect(getFieldInfo(C4)).toMatchObject([
expect(getMetadataObject(C4).collectFieldsInfo()).toMatchObject([
{ propertyKey: "c1", prototype: "string", rule: "required" },
{ propertyKey: "c2", prototype: "string", rule: "required" },
{ propertyKey: "c3", prototype: "string", rule: "required" },
{ propertyKey: "c4", prototype: "string", rule: "required" }
]);

expect(getFieldInfo(C5)).toMatchObject([
expect(getMetadataObject(C5).collectFieldsInfo()).toMatchObject([
{ propertyKey: "c1", prototype: "string", rule: "required" },
{ propertyKey: "c2", prototype: "string", rule: "required" },
{ propertyKey: "c3", prototype: "string", rule: "required" },
Expand All @@ -241,19 +236,19 @@ describe("getFieldInfo", () => {
c3: string;
}

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

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

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

expect(getFieldInfo(Child3)).toMatchObject([
expect(getMetadataObject(Child3).collectFieldsInfo()).toMatchObject([
{ propertyKey: "someField", prototype: "string", rule: "required" },
{ propertyKey: "c3", prototype: "string", rule: "required" }
]);
Expand All @@ -270,6 +265,6 @@ describe("getFieldInfo", () => {
someField: string;
}

expect(() => getFieldInfo(Child)).toThrowError();
expect(() => getMetadataObject(Child).collectFieldsInfo()).toThrowError();
});
});

0 comments on commit 1e72acc

Please sign in to comment.