Rename decorator cosmosMessage -> registered

This commit is contained in:
Simon Warta 2020-08-06 13:05:39 +02:00
parent c2e0efd158
commit feb33aec3d
6 changed files with 21 additions and 13 deletions

View File

@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Message } from "protobufjs";
import { cosmosField, cosmosMessage } from "./decorator";
import { cosmosField, registered } from "./decorator";
import { defaultRegistry } from "./msgs";
@cosmosMessage(defaultRegistry, "/cosmos.auth.BaseAccount")
@registered(defaultRegistry, "/cosmos.auth.BaseAccount")
export class BaseAccount extends Message {
@cosmosField.bytes(1)
public readonly address?: Uint8Array;

View File

@ -2,7 +2,7 @@
import { assert } from "@cosmjs/utils";
import { Message } from "protobufjs";
import { cosmosField, cosmosMessage } from "./decorator";
import { cosmosField, registered } from "./decorator";
import { cosmos, google } from "./generated/codecimpl";
import { Registry } from "./registry";
@ -15,13 +15,13 @@ describe("decorator demo", () => {
const typeUrl = "/demo.MsgDemo";
const myRegistry = new Registry();
@cosmosMessage(myRegistry, nestedTypeUrl)
@registered(myRegistry, nestedTypeUrl)
class MsgNestedDemo extends Message {
@cosmosField.string(1)
public readonly foo?: string;
}
@cosmosMessage(myRegistry, typeUrl)
@registered(myRegistry, typeUrl)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class MsgDemo extends Message {
@cosmosField.boolean(1)

View File

@ -7,7 +7,11 @@ function getTypeName(typeUrl: string): string {
return parts[parts.length - 1];
}
export function cosmosMessage(registry: Registry, typeUrl: string): TypeDecorator<any> {
/**
* A class decorator to register this type under the given type URL
* in the given registry.
*/
export function registered(registry: Registry, typeUrl: string): TypeDecorator<any> {
return (ctor: Constructor<Message<any>>) => {
const typeName = getTypeName(typeUrl);
const generatedType = util.decorateType(ctor, typeName);

View File

@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Message } from "protobufjs";
import { cosmosField, cosmosMessage } from "./decorator";
import { cosmosField, registered } from "./decorator";
import { Registry } from "./registry";
describe("registry magic demo", () => {
@ -10,13 +10,13 @@ describe("registry magic demo", () => {
const typeUrl = "/demo.MsgMagic";
const myRegistry = new Registry();
@cosmosMessage(myRegistry, nestedTypeUrl)
@registered(myRegistry, nestedTypeUrl)
class MsgNestedMagic extends Message {
@cosmosField.string(1)
public readonly foo?: string;
}
@cosmosMessage(myRegistry, typeUrl)
@registered(myRegistry, typeUrl)
class MsgMagic extends Message {
@cosmosField.boolean(1)
public readonly booleanDemo?: boolean;

View File

@ -1,12 +1,12 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Message } from "protobufjs";
import { cosmosField, cosmosMessage } from "./decorator";
import { cosmosField, registered } from "./decorator";
import { Registry } from "./registry";
export const defaultRegistry = new Registry();
@cosmosMessage(defaultRegistry, "/cosmos.Coin")
@registered(defaultRegistry, "/cosmos.Coin")
export class Coin extends Message {
@cosmosField.string(1)
public readonly denom?: string;
@ -15,7 +15,7 @@ export class Coin extends Message {
public readonly amount?: string;
}
@cosmosMessage(defaultRegistry, "/cosmos.bank.MsgSend")
@registered(defaultRegistry, "/cosmos.bank.MsgSend")
export class MsgSend extends Message {
@cosmosField.bytes(1)
public readonly from_address?: Uint8Array;

View File

@ -1,6 +1,10 @@
import { Constructor, Message, TypeDecorator } from "protobufjs";
import { Registry } from "./registry";
export declare function cosmosMessage(registry: Registry, typeUrl: string): TypeDecorator<any>;
/**
* A class decorator to register this type under the given type URL
* in the given registry.
*/
export declare function registered(registry: Registry, typeUrl: string): TypeDecorator<any>;
/**
* Like PropertyDecorator from lib.es5.d.ts but without symbol support in propertyKey.
*/