demo-protobuf: Add nested msg field to decorator demo

This commit is contained in:
willclarktech 2020-06-23 11:03:56 +02:00
parent 00efb4ad9c
commit 94ecbca474
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
3 changed files with 21 additions and 1 deletions

View File

@ -7,9 +7,16 @@ import { Registry } from "./registry";
describe("decorator demo", () => {
it("works with a custom msg", () => {
const nestedTypeUrl = "/demo.MsgNestedDemo";
const typeUrl = "/demo.MsgDemo";
const myRegistry = new Registry();
@CosmosMessage(myRegistry, nestedTypeUrl)
class MsgNestedDemo extends Message<{}> {
@CosmosField.String(1)
public readonly foo?: string;
}
@CosmosMessage(myRegistry, typeUrl)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class MsgDemo extends Message<{}> {
@ -30,12 +37,21 @@ describe("decorator demo", () => {
@CosmosField.Repeated(6)
public readonly listDemo?: readonly string[];
@CosmosField.Nested(7, MsgNestedDemo)
public readonly nestedDemo?: MsgNestedDemo;
}
const MsgNestedDemoT = myRegistry.lookupType(nestedTypeUrl)!;
const MsgDemoT = myRegistry.lookupType(typeUrl)!;
const TxBody = myRegistry.lookupType("/cosmos.tx.TxBody")!;
const Any = myRegistry.lookupType("/google.protobuf.Any")!;
const msgNestedDemoFields = {
foo: "bar",
};
const msgNestedDemo = MsgNestedDemoT.create(msgNestedDemoFields);
const msgDemoFields = {
booleanDemo: true,
stringDemo: "example text",
@ -43,6 +59,7 @@ describe("decorator demo", () => {
int64Demo: -123,
uint64Demo: 123,
listDemo: ["this", "is", "a", "list"],
nestedDemo: msgNestedDemo,
};
const msgDemo = MsgDemoT.create(msgDemoFields);
const msgDemoBytes = MsgDemoT.encode(msgDemo).finish();
@ -73,5 +90,6 @@ describe("decorator demo", () => {
expect(msgDemoDecoded.uint64Demo.toNumber()).toEqual(msgDemoFields.uint64Demo);
expect(msgDemoDecoded.listDemo).toEqual(msgDemoFields.listDemo);
expect(msgDemoDecoded.nestedDemo).toEqual(msgDemoFields.nestedDemo);
});
});

View File

@ -25,4 +25,5 @@ export const CosmosField = {
UInt64: (id: number) => Field.d<number>(id, "uint64"),
Repeated: (id: number) => Field.d<string[]>(id, "string", "repeated"),
Nested: (id: number, ctor: Constructor<Message<{}>>) => Field.d(id, ctor),
};

View File

@ -1,4 +1,4 @@
import { TypeDecorator } from "protobufjs";
import { Constructor, Message, TypeDecorator } from "protobufjs";
import { Registry } from "./registry";
export declare function CosmosMessage(registry: Registry, typeUrl: string): TypeDecorator<any>;
export declare const CosmosField: {
@ -8,4 +8,5 @@ export declare const CosmosField: {
Int64: (id: number) => import("protobufjs").FieldDecorator;
UInt64: (id: number) => import("protobufjs").FieldDecorator;
Repeated: (id: number) => import("protobufjs").FieldDecorator;
Nested: (id: number, ctor: Constructor<Message<{}>>) => import("protobufjs").FieldDecorator;
};