@elderapo/protobuf-lite
v1.3.1
Published
[](https://github.com/prettier/prettier) [](https://greenkeeper.io/) [
public firstName: string;
@ProtobufLiteProperty()
public secondName: string;
@ProtobufLiteProperty({ optional: true })
public nickname?: string;
@ProtobufLiteProperty()
public isProgrammer: boolean;
@ProtobufLiteProperty()
public birthDate: Date;
@ProtobufLiteProperty({ type: () => String })
public hobbies: string[];
}
const payload: Person = {
firstName: "Joe",
secondName: "Doe",
isProgrammer: true,
hobbies: ["swimming", "eating"],
birthDate: new Date("1990")
};
const encoded = encode(Person, payload);
const decoded = decode(Person, encoded);
expect(Buffer.isBuffer(encoded)).toBe(true);
expect(decoded).toBeInstanceOf(Person);
expect(decoded.firstName).toBe("Joe");
expect(decoded.secondName).toBe("Doe");
expect(decoded.isProgrammer).toBe(true);
expect(decoded.hobbies).toBeInstanceOf(Array);
expect(decoded.hobbies).toMatchObject(["swimming", "eating"]);
expect(decoded.birthDate).toBeInstanceOf(Date);
expect(decoded.birthDate.getTime()).toBe(new Date("1990").getTime());
console.log(`Everything is working as expected 👍`);Check out tests cases for more examples!
Todo
- [ ] add possibility to manually specify protobuf types (bool, uint32 etc..)
- [ ] figure out if it's possible to automatically generate
*.protofiles from fields metadata
