demo-protobuf: Rearrange decorate demo

This commit is contained in:
willclarktech 2020-06-18 13:43:05 +02:00
parent 8f63359962
commit 4f4f0cd682
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
3 changed files with 23 additions and 21 deletions

View File

@ -1,19 +1,30 @@
/* eslint-disable @typescript-eslint/camelcase */
import { assert } from "@cosmjs/utils";
import { Message } from "protobufjs";
import { myRegistry } from "./decorator";
import { CosmosField, CosmosMessage } from "./decorator";
import { Registry } from "./registry";
describe("decorator demo", () => {
it("works with a custom msg", () => {
const typeUrl = "/demo.MsgDemo";
const MsgDemo = myRegistry.lookupType(typeUrl)!;
const myRegistry = new Registry();
@CosmosMessage(myRegistry, typeUrl)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class MsgDemo extends Message<{}> {
@CosmosField.String(1)
public readonly example: string = "";
}
const MsgDemoT = myRegistry.lookupType(typeUrl)!;
const TxBody = myRegistry.lookupType("/cosmos.tx.TxBody")!;
const Any = myRegistry.lookupType("/google.protobuf.Any")!;
const msgDemo = MsgDemo.create({
const msgDemo = MsgDemoT.create({
example: "Some example text",
});
const msgDemoBytes = MsgDemo.encode(msgDemo).finish();
const msgDemoBytes = MsgDemoT.encode(msgDemo).finish();
const msgDemoWrapped = Any.create({
type_url: typeUrl,
value: msgDemoBytes,
@ -31,8 +42,7 @@ describe("decorator demo", () => {
assert(msg.type_url);
assert(msg.value);
const decoder = myRegistry.lookupType(msg.type_url)!;
const msgDemoDecoded = decoder.decode(msg.value);
const msgDemoDecoded = MsgDemoT.decode(msg.value);
expect(msgDemoDecoded.example).toEqual(msgDemo.example);
});
});

View File

@ -2,14 +2,12 @@ import { Constructor, Field, Message, TypeDecorator, util } from "protobufjs";
import { Registry } from "./registry";
export const myRegistry = new Registry();
function getTypeName(typeUrl: string): string {
const parts = typeUrl.split(".");
return parts[parts.length - 1];
}
function CosmosMessage(registry: Registry, typeUrl: string): TypeDecorator<any> {
export function CosmosMessage(registry: Registry, typeUrl: string): TypeDecorator<any> {
return (constructor: Constructor<Message<any>>) => {
const typeName = getTypeName(typeUrl);
const generatedType = util.decorateType(constructor, typeName);
@ -17,12 +15,6 @@ function CosmosMessage(registry: Registry, typeUrl: string): TypeDecorator<any>
};
}
const CosmosField = {
export const CosmosField = {
String: (id: number) => Field.d(id, "string"),
};
@CosmosMessage(myRegistry, "/demo.MsgDemo")
export class MsgDemo extends Message<{}> {
@CosmosField.String(1)
public readonly example: string = "";
}

View File

@ -1,6 +1,6 @@
import { Message } from "protobufjs";
import { TypeDecorator } from "protobufjs";
import { Registry } from "./registry";
export declare const myRegistry: Registry;
export declare class MsgDemo extends Message<{}> {
readonly example: string;
}
export declare function CosmosMessage(registry: Registry, typeUrl: string): TypeDecorator<any>;
export declare const CosmosField: {
String: (id: number) => import("protobufjs").FieldDecorator;
};