-
Notifications
You must be signed in to change notification settings - Fork 349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add js type support #1030
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
...fieldoption-jstype-with-forcelong-bigint/fieldoption-jstype-with-forcelong-bigint-test.ts
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,110 @@ | ||
import { FieldOption } from "./fieldoption-jstype-with-forcelong-bigint"; | ||
import { hexToUint8Array, uint8ArrayToHex } from "../utils"; | ||
|
||
describe("FieldOption jstype with ForceLong bigint", () => { | ||
describe("encode", () => { | ||
it("should encode the message", () => { | ||
const message: FieldOption = { | ||
normalField: BigInt(123), | ||
numberField: 456, | ||
stringField: "789", | ||
}; | ||
|
||
const writer = FieldOption.encode(message); | ||
const buffer = writer.finish(); | ||
|
||
expect(uint8ArrayToHex(buffer)).toEqual("087b10c803189506"); | ||
}); | ||
}); | ||
|
||
describe("decode", () => { | ||
it("should decode the message", () => { | ||
const buffer = hexToUint8Array("087b10c803189506"); | ||
|
||
const decodedMessage = FieldOption.decode(buffer); | ||
|
||
expect(decodedMessage).toEqual({ | ||
normalField: BigInt(123), | ||
numberField: 456, | ||
stringField: "789", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("fromJSON", () => { | ||
it("should create a message from JSON", () => { | ||
const json = { | ||
normalField: "123", | ||
numberField: "456", | ||
stringField: "789", | ||
}; | ||
|
||
const message = FieldOption.fromJSON(json); | ||
|
||
expect(message).toEqual({ | ||
normalField: BigInt(123), | ||
numberField: 456, | ||
stringField: "789", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("toJSON", () => { | ||
it("should convert the message to JSON", () => { | ||
const message: FieldOption = { | ||
normalField: BigInt(123), | ||
numberField: 456, | ||
stringField: "789", | ||
}; | ||
|
||
const json = FieldOption.toJSON(message); | ||
|
||
expect(json).toEqual({ | ||
normalField: "123", | ||
numberField: 456, | ||
stringField: "789", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("create", () => { | ||
it("should create a message with default values", () => { | ||
const message = FieldOption.create(); | ||
|
||
expect(message).toEqual({ | ||
normalField: BigInt(0), | ||
numberField: 0, | ||
stringField: "0", | ||
}); | ||
}); | ||
|
||
it("should create a message with provided values", () => { | ||
const message = FieldOption.create({ | ||
normalField: BigInt(123), | ||
numberField: 456, | ||
stringField: "789", | ||
}); | ||
|
||
expect(message).toEqual({ | ||
normalField: BigInt(123), | ||
numberField: 456, | ||
stringField: "789", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("fromPartial", () => { | ||
it("should create a message from a partial object", () => { | ||
const partial = FieldOption.fromPartial({ | ||
normalField: BigInt(123), | ||
stringField: "789", | ||
}); | ||
|
||
expect(partial).toEqual({ | ||
normalField: BigInt(123), | ||
numberField: 0, | ||
stringField: "789", | ||
}); | ||
}); | ||
}); | ||
}); |
Binary file added
BIN
+558 Bytes
...ion/fieldoption-jstype-with-forcelong-bigint/fieldoption-jstype-with-forcelong-bigint.bin
Binary file not shown.
9 changes: 9 additions & 0 deletions
9
...n/fieldoption-jstype-with-forcelong-bigint/fieldoption-jstype-with-forcelong-bigint.proto
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,9 @@ | ||
syntax = "proto3"; | ||
|
||
package foo; | ||
|
||
message FieldOption { | ||
int64 normalField = 1 [jstype = JS_NORMAL]; | ||
int64 numberField = 2 [jstype = JS_NUMBER]; | ||
int64 stringField = 3 [jstype = JS_STRING]; | ||
} |
145 changes: 145 additions & 0 deletions
145
...tion/fieldoption-jstype-with-forcelong-bigint/fieldoption-jstype-with-forcelong-bigint.ts
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,145 @@ | ||
/* eslint-disable */ | ||
import * as _m0 from "protobufjs/minimal"; | ||
import Long = require("long"); | ||
|
||
export const protobufPackage = "foo"; | ||
|
||
export interface FieldOption { | ||
normalField: bigint; | ||
numberField: number; | ||
stringField: string; | ||
} | ||
|
||
function createBaseFieldOption(): FieldOption { | ||
return { normalField: BigInt("0"), numberField: 0, stringField: "0" }; | ||
} | ||
|
||
export const FieldOption = { | ||
encode(message: FieldOption, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { | ||
if (message.normalField !== BigInt("0")) { | ||
if (BigInt.asIntN(64, message.normalField) !== message.normalField) { | ||
throw new globalThis.Error("value provided for field message.normalField of type int64 too large"); | ||
} | ||
writer.uint32(8).int64(message.normalField.toString()); | ||
} | ||
if (message.numberField !== 0) { | ||
if (BigInt.asIntN(64, BigInt(message.numberField)) !== BigInt(message.numberField)) { | ||
throw new globalThis.Error("value provided for field BigInt(message.numberField) of type int64 too large"); | ||
} | ||
writer.uint32(16).int64(BigInt(message.numberField).toString()); | ||
} | ||
if (message.stringField !== "0") { | ||
if (BigInt.asIntN(64, BigInt(message.stringField)) !== BigInt(message.stringField)) { | ||
throw new globalThis.Error("value provided for field BigInt(message.stringField) of type int64 too large"); | ||
} | ||
writer.uint32(24).int64(BigInt(message.stringField).toString()); | ||
} | ||
return writer; | ||
}, | ||
|
||
decode(input: _m0.Reader | Uint8Array, length?: number): FieldOption { | ||
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input); | ||
let end = length === undefined ? reader.len : reader.pos + length; | ||
const message = createBaseFieldOption(); | ||
while (reader.pos < end) { | ||
const tag = reader.uint32(); | ||
switch (tag >>> 3) { | ||
case 1: | ||
if (tag !== 8) { | ||
break; | ||
} | ||
|
||
message.normalField = longToBigint(reader.int64() as Long); | ||
continue; | ||
case 2: | ||
if (tag !== 16) { | ||
break; | ||
} | ||
|
||
message.numberField = longToNumber(reader.int64() as Long); | ||
continue; | ||
case 3: | ||
if (tag !== 24) { | ||
break; | ||
} | ||
|
||
message.stringField = longToString(reader.int64() as Long); | ||
continue; | ||
} | ||
if ((tag & 7) === 4 || tag === 0) { | ||
break; | ||
} | ||
reader.skipType(tag & 7); | ||
} | ||
return message; | ||
}, | ||
|
||
fromJSON(object: any): FieldOption { | ||
return { | ||
normalField: isSet(object.normalField) ? BigInt(object.normalField) : BigInt("0"), | ||
numberField: isSet(object.numberField) ? globalThis.Number(object.numberField) : 0, | ||
stringField: isSet(object.stringField) ? globalThis.String(object.stringField) : "0", | ||
}; | ||
}, | ||
|
||
toJSON(message: FieldOption): unknown { | ||
const obj: any = {}; | ||
if (message.normalField !== BigInt("0")) { | ||
obj.normalField = message.normalField.toString(); | ||
} | ||
if (message.numberField !== 0) { | ||
obj.numberField = globalThis.Number(message.numberField); | ||
} | ||
if (message.stringField !== "0") { | ||
obj.stringField = globalThis.String(message.stringField); | ||
} | ||
return obj; | ||
}, | ||
|
||
create<I extends Exact<DeepPartial<FieldOption>, I>>(base?: I): FieldOption { | ||
return FieldOption.fromPartial(base ?? ({} as any)); | ||
}, | ||
fromPartial<I extends Exact<DeepPartial<FieldOption>, I>>(object: I): FieldOption { | ||
const message = createBaseFieldOption(); | ||
message.normalField = object.normalField ?? BigInt("0"); | ||
message.numberField = object.numberField ?? 0; | ||
message.stringField = object.stringField ?? "0"; | ||
return message; | ||
}, | ||
}; | ||
|
||
type Builtin = Date | Function | Uint8Array | string | number | boolean | bigint | undefined; | ||
|
||
export type DeepPartial<T> = T extends Builtin ? T | ||
: T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>> | ||
: T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> | ||
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> } | ||
: Partial<T>; | ||
|
||
type KeysOfUnion<T> = T extends T ? keyof T : never; | ||
export type Exact<P, I extends P> = P extends Builtin ? P | ||
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never }; | ||
|
||
function longToNumber(long: Long): number { | ||
if (long.gt(globalThis.Number.MAX_SAFE_INTEGER)) { | ||
throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER"); | ||
} | ||
return long.toNumber(); | ||
} | ||
|
||
function longToString(long: Long) { | ||
return long.toString(); | ||
} | ||
|
||
function longToBigint(long: Long) { | ||
return BigInt(long.toString()); | ||
} | ||
|
||
if (_m0.util.Long !== Long) { | ||
_m0.util.Long = Long as any; | ||
_m0.configure(); | ||
} | ||
|
||
function isSet(value: any): boolean { | ||
return value !== null && value !== undefined; | ||
} |
8 changes: 8 additions & 0 deletions
8
integration/fieldoption-jstype-with-forcelong-bigint/jest.config.js
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,8 @@ | ||
const base = require("../../jest.config"); | ||
|
||
// Set maxWorkers for assertion failures involving BigInt | ||
// https://github.com/jestjs/jest/issues/11617 | ||
module.exports = { | ||
...base, | ||
maxWorkers: 1, | ||
}; |
1 change: 1 addition & 0 deletions
1
integration/fieldoption-jstype-with-forcelong-bigint/parameters.txt
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 @@ | ||
forceLong=bigint,useJsTypeOverride=true |
112 changes: 112 additions & 0 deletions
112
...ion/fieldoption-jstype-with-forcelong-long/fieldoption-jstype-with-forcelong-long-test.ts
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,112 @@ | ||
import { FieldOption } from "./fieldoption-jstype-with-forcelong-long"; | ||
import { hexToUint8Array, uint8ArrayToHex } from "../utils"; | ||
// @ts-ignore | ||
import Long = require("long"); | ||
|
||
describe("FieldOption jstype with ForceLong long", () => { | ||
describe("encode", () => { | ||
it("should encode the message", () => { | ||
const message: FieldOption = { | ||
normalField: Long.fromValue(123), | ||
numberField: 456, | ||
stringField: "789", | ||
}; | ||
|
||
const writer = FieldOption.encode(message); | ||
const buffer = writer.finish(); | ||
|
||
expect(uint8ArrayToHex(buffer)).toEqual("087b10c803189506"); | ||
}); | ||
}); | ||
|
||
describe("decode", () => { | ||
it("should decode the message", () => { | ||
const buffer = hexToUint8Array("087b10c803189506"); | ||
|
||
const decodedMessage = FieldOption.decode(buffer); | ||
|
||
expect(decodedMessage).toEqual({ | ||
normalField: Long.fromValue(123), | ||
numberField: 456, | ||
stringField: "789", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("fromJSON", () => { | ||
it("should create a message from JSON", () => { | ||
const json = { | ||
normalField: "123", | ||
numberField: "456", | ||
stringField: "789", | ||
}; | ||
|
||
const message = FieldOption.fromJSON(json); | ||
|
||
expect(message).toEqual({ | ||
normalField: Long.fromValue(123), | ||
numberField: 456, | ||
stringField: "789", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("toJSON", () => { | ||
it("should convert the message to JSON", () => { | ||
const message: FieldOption = { | ||
normalField: Long.fromValue(123), | ||
numberField: 456, | ||
stringField: "789", | ||
}; | ||
|
||
const json = FieldOption.toJSON(message); | ||
|
||
expect(json).toEqual({ | ||
normalField: "123", | ||
numberField: 456, | ||
stringField: "789", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("create", () => { | ||
it("should create a message with default values", () => { | ||
const message = FieldOption.create(); | ||
|
||
expect(message).toEqual({ | ||
normalField: Long.fromValue(0), | ||
numberField: 0, | ||
stringField: "0", | ||
}); | ||
}); | ||
|
||
it("should create a message with provided values", () => { | ||
const message = FieldOption.create({ | ||
normalField: Long.fromValue(123), | ||
numberField: 456, | ||
stringField: "789", | ||
}); | ||
|
||
expect(message).toEqual({ | ||
normalField: Long.fromValue(123), | ||
numberField: 456, | ||
stringField: "789", | ||
}); | ||
}); | ||
}); | ||
|
||
describe("fromPartial", () => { | ||
it("should create a message from a partial object", () => { | ||
const partial = FieldOption.fromPartial({ | ||
normalField: Long.fromValue(123), | ||
stringField: "789", | ||
}); | ||
|
||
expect(partial).toEqual({ | ||
normalField: Long.fromValue(123), | ||
numberField: 0, | ||
stringField: "789", | ||
}); | ||
}); | ||
}); | ||
}); |
Binary file added
BIN
+554 Bytes
...gration/fieldoption-jstype-with-forcelong-long/fieldoption-jstype-with-forcelong-long.bin
Binary file not shown.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how to resolve this at the moment. In my editor it errors, stating that the tsconfig
esModuleInterop
is enabled for this file and I can't use an Import assignment, but from what I can see at runtime it does not haveesModuleInterop
enabled.If we import using a default import
then the editor no longer reports an error, but tests fail due to it not being able to resolve the import.