-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow overriding 64 bit number types with strings or regular js numbers. Closes #112
- Loading branch information
1 parent
1d6e843
commit a6dfb0a
Showing
4 changed files
with
270 additions
and
13 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
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()) | ||
} | ||
} |