mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
demo-protobuf: Rearrange decorate demo
This commit is contained in:
parent
8f63359962
commit
4f4f0cd682
@ -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);
|
||||
});
|
||||
});
|
||||
|
@ -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 = "";
|
||||
}
|
||||
|
10
packages/demo-protobuf/types/decorator.d.ts
vendored
10
packages/demo-protobuf/types/decorator.d.ts
vendored
@ -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;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user