forked from near/borsh-js
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d8bc8c
commit c74522e
Showing
7 changed files
with
440 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
syntax = "proto3"; | ||
|
||
enum Enum { | ||
X = 1; | ||
Y = 2; | ||
Z = 3; | ||
} | ||
|
||
message Message { | ||
Enum enum = 1; | ||
uint32 uint32 = 2; | ||
sint32 sint32 = 3; | ||
int32 int32 = 4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import protobuf from "protobufjs"; | ||
import { field, serialize, variant } from "../index.js"; | ||
const protoRoot = protobuf.loadSync("src/__tests__/compat.proto"); | ||
const ProtoMessage = protoRoot.lookupType("Message"); | ||
|
||
enum Enum { | ||
X = 0, | ||
Y = 1, | ||
Z = 2, | ||
} | ||
class Message { | ||
@field({ type: "vi32" }) | ||
enum: number; | ||
|
||
@field({ type: "vu32" }) | ||
uint32: number; | ||
|
||
@field({ type: "vsi32" }) | ||
sint32: number; | ||
|
||
@field({ type: "vi32" }) | ||
int32: number; | ||
|
||
constructor(message: Message) { | ||
Object.assign(this, message); | ||
} | ||
} | ||
describe("protobuf", () => { | ||
it("protobuf compat", () => { | ||
const obj = { | ||
enum: 1, | ||
uint32: 567, | ||
sint32: -1234, | ||
int32: -5677, | ||
}; | ||
const proto = ProtoMessage.encode(obj).finish(); | ||
const borsh = serialize(new Message(obj)); | ||
expect(proto).toEqual(borsh); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.