demo-protobuf: Add more protobuf field types to decorator demo
This commit is contained in:
parent
26af817420
commit
00efb4ad9c
@ -13,17 +13,38 @@ describe("decorator demo", () => {
|
||||
@CosmosMessage(myRegistry, typeUrl)
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
class MsgDemo extends Message<{}> {
|
||||
@CosmosField.String(1)
|
||||
public readonly example: string = "";
|
||||
@CosmosField.Boolean(1)
|
||||
public readonly booleanDemo?: boolean;
|
||||
|
||||
@CosmosField.String(2)
|
||||
public readonly stringDemo?: string;
|
||||
|
||||
@CosmosField.Bytes(3)
|
||||
public readonly bytesDemo?: Uint8Array;
|
||||
|
||||
@CosmosField.Int64(4)
|
||||
public readonly int64Demo?: number;
|
||||
|
||||
@CosmosField.UInt64(5)
|
||||
public readonly uint64Demo?: number;
|
||||
|
||||
@CosmosField.Repeated(6)
|
||||
public readonly listDemo?: readonly string[];
|
||||
}
|
||||
|
||||
const MsgDemoT = myRegistry.lookupType(typeUrl)!;
|
||||
const TxBody = myRegistry.lookupType("/cosmos.tx.TxBody")!;
|
||||
const Any = myRegistry.lookupType("/google.protobuf.Any")!;
|
||||
|
||||
const msgDemo = MsgDemoT.create({
|
||||
example: "Some example text",
|
||||
});
|
||||
const msgDemoFields = {
|
||||
booleanDemo: true,
|
||||
stringDemo: "example text",
|
||||
bytesDemo: Uint8Array.from([1, 2, 3, 4, 5, 6, 7, 8]),
|
||||
int64Demo: -123,
|
||||
uint64Demo: 123,
|
||||
listDemo: ["this", "is", "a", "list"],
|
||||
};
|
||||
const msgDemo = MsgDemoT.create(msgDemoFields);
|
||||
const msgDemoBytes = MsgDemoT.encode(msgDemo).finish();
|
||||
const msgDemoWrapped = Any.create({
|
||||
type_url: typeUrl,
|
||||
@ -43,6 +64,14 @@ describe("decorator demo", () => {
|
||||
assert(msg.value);
|
||||
|
||||
const msgDemoDecoded = MsgDemoT.decode(msg.value);
|
||||
expect(msgDemoDecoded.example).toEqual(msgDemo.example);
|
||||
expect(msgDemoDecoded.booleanDemo).toEqual(msgDemoFields.booleanDemo);
|
||||
expect(msgDemoDecoded.stringDemo).toEqual(msgDemoFields.stringDemo);
|
||||
// bytesDemo decodes to a Buffer in Node
|
||||
expect(Uint8Array.from(msgDemoDecoded.bytesDemo)).toEqual(msgDemoFields.bytesDemo);
|
||||
// int64Demo and uint64Demo decode to Long
|
||||
expect(msgDemoDecoded.int64Demo.toNumber()).toEqual(msgDemoFields.int64Demo);
|
||||
expect(msgDemoDecoded.uint64Demo.toNumber()).toEqual(msgDemoFields.uint64Demo);
|
||||
|
||||
expect(msgDemoDecoded.listDemo).toEqual(msgDemoFields.listDemo);
|
||||
});
|
||||
});
|
||||
|
||||
@ -16,5 +16,13 @@ export function CosmosMessage(registry: Registry, typeUrl: string): TypeDecorato
|
||||
}
|
||||
|
||||
export const CosmosField = {
|
||||
String: (id: number) => Field.d(id, "string"),
|
||||
Boolean: (id: number) => Field.d<boolean>(id, "bool"),
|
||||
|
||||
String: (id: number) => Field.d<string>(id, "string"),
|
||||
Bytes: (id: number) => Field.d<Uint8Array>(id, "bytes"),
|
||||
|
||||
Int64: (id: number) => Field.d<number>(id, "int64"),
|
||||
UInt64: (id: number) => Field.d<number>(id, "uint64"),
|
||||
|
||||
Repeated: (id: number) => Field.d<string[]>(id, "string", "repeated"),
|
||||
};
|
||||
|
||||
5
packages/demo-protobuf/types/decorator.d.ts
vendored
5
packages/demo-protobuf/types/decorator.d.ts
vendored
@ -2,5 +2,10 @@ import { TypeDecorator } from "protobufjs";
|
||||
import { Registry } from "./registry";
|
||||
export declare function CosmosMessage(registry: Registry, typeUrl: string): TypeDecorator<any>;
|
||||
export declare const CosmosField: {
|
||||
Boolean: (id: number) => import("protobufjs").FieldDecorator;
|
||||
String: (id: number) => import("protobufjs").FieldDecorator;
|
||||
Bytes: (id: number) => import("protobufjs").FieldDecorator;
|
||||
Int64: (id: number) => import("protobufjs").FieldDecorator;
|
||||
UInt64: (id: number) => import("protobufjs").FieldDecorator;
|
||||
Repeated: (id: number) => import("protobufjs").FieldDecorator;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user