-
Notifications
You must be signed in to change notification settings - Fork 23
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: support jstype custom options #117
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a6dfb0a
feat: support jstype custom options
achingbrain 284ff25
chore: add specific number/string methods
achingbrain 06c5b47
chore: update reader comments
achingbrain 820b466
chore: add number benchmark
achingbrain 1038e0d
chore: check lower bound
achingbrain 06ac59c
chore: remove unused code
achingbrain a9f0d0f
Merge remote-tracking branch 'origin/main' into feat/support-jstype-c…
achingbrain 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
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
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,26 @@ | ||
/* eslint-env mocha */ | ||
|
||
import { expect } from 'aegir/chai' | ||
import { CustomOptionNumber, CustomOptionString } from './fixtures/custom-option-jstype.js' | ||
|
||
describe('custom options', () => { | ||
it('should allow overriding 64 bit numbers with numbers', () => { | ||
const obj: CustomOptionNumber = { | ||
num: 5, | ||
bignum: 5 | ||
} | ||
|
||
expect(CustomOptionNumber.decode(CustomOptionNumber.encode(obj))) | ||
.to.deep.equal(obj) | ||
}) | ||
|
||
it('should allow overriding 64 bit numbers with strings', () => { | ||
const obj: CustomOptionString = { | ||
num: 5, | ||
bignum: '5' | ||
} | ||
|
||
expect(CustomOptionString.decode(CustomOptionString.encode(obj))) | ||
.to.deep.equal(obj) | ||
}) | ||
}) |
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,11 @@ | ||
syntax = "proto3"; | ||
|
||
message CustomOptionNumber { | ||
int32 num = 1; | ||
int64 bignum = 2 [jstype = JS_NUMBER]; | ||
} | ||
|
||
message CustomOptionString { | ||
int32 num = 1; | ||
int64 bignum = 2 [jstype = JS_STRING]; | ||
} |
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/export */ | ||
/* eslint-disable complexity */ | ||
/* eslint-disable @typescript-eslint/no-namespace */ | ||
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */ | ||
/* eslint-disable @typescript-eslint/no-empty-interface */ | ||
|
||
import { encodeMessage, decodeMessage, message } from 'protons-runtime' | ||
import type { Codec } from 'protons-runtime' | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
|
||
export interface CustomOptionNumber { | ||
num: number | ||
bignum: number | ||
} | ||
|
||
export namespace CustomOptionNumber { | ||
let _codec: Codec<CustomOptionNumber> | ||
|
||
export const codec = (): Codec<CustomOptionNumber> => { | ||
if (_codec == null) { | ||
_codec = message<CustomOptionNumber>((obj, w, opts = {}) => { | ||
if (opts.lengthDelimited !== false) { | ||
w.fork() | ||
} | ||
|
||
if ((obj.num != null && obj.num !== 0)) { | ||
w.uint32(8) | ||
w.int32(obj.num) | ||
} | ||
|
||
if ((obj.bignum != null && obj.bignum !== 0)) { | ||
w.uint32(16) | ||
w.int64(BigInt(obj.bignum)) | ||
} | ||
|
||
if (opts.lengthDelimited !== false) { | ||
w.ldelim() | ||
} | ||
}, (reader, length) => { | ||
const obj: any = { | ||
num: 0, | ||
bignum: 0 | ||
} | ||
|
||
const end = length == null ? reader.len : reader.pos + length | ||
|
||
while (reader.pos < end) { | ||
const tag = reader.uint32() | ||
|
||
switch (tag >>> 3) { | ||
case 1: | ||
obj.num = reader.int32() | ||
break | ||
case 2: | ||
obj.bignum = Number(reader.int64()) | ||
break | ||
default: | ||
reader.skipType(tag & 7) | ||
break | ||
} | ||
} | ||
|
||
return obj | ||
}) | ||
} | ||
|
||
return _codec | ||
} | ||
|
||
export const encode = (obj: Partial<CustomOptionNumber>): Uint8Array => { | ||
return encodeMessage(obj, CustomOptionNumber.codec()) | ||
} | ||
|
||
export const decode = (buf: Uint8Array | Uint8ArrayList): CustomOptionNumber => { | ||
return decodeMessage(buf, CustomOptionNumber.codec()) | ||
} | ||
} | ||
|
||
export interface CustomOptionString { | ||
num: number | ||
bignum: string | ||
} | ||
|
||
export namespace CustomOptionString { | ||
let _codec: Codec<CustomOptionString> | ||
|
||
export const codec = (): Codec<CustomOptionString> => { | ||
if (_codec == null) { | ||
_codec = message<CustomOptionString>((obj, w, opts = {}) => { | ||
if (opts.lengthDelimited !== false) { | ||
w.fork() | ||
} | ||
|
||
if ((obj.num != null && obj.num !== 0)) { | ||
w.uint32(8) | ||
w.int32(obj.num) | ||
} | ||
|
||
if ((obj.bignum != null && obj.bignum !== '')) { | ||
w.uint32(16) | ||
w.int64(BigInt(obj.bignum)) | ||
} | ||
|
||
if (opts.lengthDelimited !== false) { | ||
w.ldelim() | ||
} | ||
}, (reader, length) => { | ||
const obj: any = { | ||
num: 0, | ||
bignum: '' | ||
} | ||
|
||
const end = length == null ? reader.len : reader.pos + length | ||
|
||
while (reader.pos < end) { | ||
const tag = reader.uint32() | ||
|
||
switch (tag >>> 3) { | ||
case 1: | ||
obj.num = reader.int32() | ||
break | ||
case 2: | ||
obj.bignum = String(reader.int64()) | ||
break | ||
default: | ||
reader.skipType(tag & 7) | ||
break | ||
} | ||
} | ||
|
||
return obj | ||
}) | ||
} | ||
|
||
return _codec | ||
} | ||
|
||
export const encode = (obj: Partial<CustomOptionString>): Uint8Array => { | ||
return encodeMessage(obj, CustomOptionString.codec()) | ||
} | ||
|
||
export const decode = (buf: Uint8Array | Uint8ArrayList): CustomOptionString => { | ||
return decodeMessage(buf, CustomOptionString.codec()) | ||
} | ||
} |
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 haven't looked at exactly whats going on here but this doesn't seem ideal.
One goal of this feature is to take advantage of speedups available without using BigInt, but if we're going thru BigInt then that isn't happening.
Somehow I thought this feature would require creating some additional types eg
w.number64
where we could have optimized encoder/decoders.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.
Yeah, that occurred to me after opening the PR, feel free to push some more commits otherwise I'll take a look in a week or two.
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.
Ok, I've ported the original
LongBits.fromNumber
method fromprotobuf.js
and split the codepaths out for bigints, numbers and strings so we have single call sites for future optimisations.