Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ deopt
transpiles
ints
jstype
jskeytype
jsvaluetype
oneofs
arraylist
bigintify
Expand Down
13 changes: 8 additions & 5 deletions packages/protons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,15 @@ By default 64 bit types are implemented as [BigInt](https://developer.mozilla.or

Sometimes this is undesirable due to [performance issues](https://betterprogramming.pub/the-downsides-of-bigints-in-javascript-6350fd807d) or code legibility.

It's possible to override the JavaScript type 64 bit fields will deserialize to:
It's possible to override the JavaScript type 64 bit fields will deserialize
to, including repeated fields and map key/values:

```
message MyMessage {
repeated int64 bigintField = 1;
repeated int64 numberField = 2 [jstype = JS_NUMBER];
repeated int64 stringField = 3 [jstype = JS_STRING];
int64 bigintField = 1;
int64 numberField = 2 [jstype = JS_NUMBER];
repeated int64 stringArray = 3 [jstype = JS_STRING];
map<int64, int64, bigintMap = 4 [jskeytype = JS_STRING, jsvaluetype = JS_STRING];
}
```

Expand All @@ -204,7 +206,8 @@ const message = MyMessage.decode(buf)

console.info(typeof message.bigintField) // bigint
console.info(typeof message.numberField) // number
console.info(typeof message.stringField) // string
console.info(typeof message.stringArray[0]) // string
console.info(typeof message.bigintMap.get('5')) // string
```

## Missing features
Expand Down
2 changes: 1 addition & 1 deletion packages/protons/src/fields/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const codecTypes: Record<string, number> = {
uint64: CODEC_TYPES.VARINT
}

const jsTypeOverrides: Record<string, 'number' | 'string'> = {
export const jsTypeOverrides: Record<string, 'number' | 'string'> = {
JS_NUMBER: 'number',
JS_STRING: 'string'
}
Expand Down
22 changes: 19 additions & 3 deletions packages/protons/src/fields/map-field.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { codecTypes, Field } from './field.ts'
import { codecTypes, Field, jsTypeOverrides } from './field.ts'
import type { FieldDef } from './field.ts'
import type { Parent } from '../types/index.ts'

Expand All @@ -15,6 +15,8 @@ export class MapField extends Field {
public keyType: string
public valueType: string
public entryType: string
public jsKeyTypeOverride?: 'string' | 'number'
public jsValueTypeOverride?: 'string' | 'number'
private lengthLimit?: number

constructor (name: string, def: MapFieldDef, parent: Parent) {
Expand All @@ -25,14 +27,28 @@ export class MapField extends Field {
this.valueType = def.valueType
this.entryType = def.type
this.lengthLimit = def.options?.['(protons.options).limit']

if (def.options?.jskeytype != null) {
this.jsKeyTypeOverride = jsTypeOverrides[def.options.jskeytype]
}

if (def.options?.jsvaluetype != null) {
this.jsValueTypeOverride = jsTypeOverrides[def.options.jsvaluetype]
}
}

getInterfaceField (parent: Parent): string {
return `${this.name}: Map<${parent.findType(this.keyType).jsType}, ${parent.findType(this.valueType).jsType}>`
const keyType = this.jsKeyTypeOverride ?? parent.findType(this.keyType).jsType
const valueType = this.jsValueTypeOverride ?? parent.findType(this.valueType).jsType

return `${this.name}: Map<${keyType}, ${valueType}>`
}

getDefaultField (parent: Parent): string {
return `${this.name}: new Map<${parent.findType(this.keyType).jsType}, ${parent.findType(this.valueType).jsType}>()`
const keyType = this.jsKeyTypeOverride ?? parent.findType(this.keyType).jsType
const valueType = this.jsValueTypeOverride ?? parent.findType(this.valueType).jsType

return `${this.name}: new Map<${keyType}, ${valueType}>()`
}

getEncoder (parent: Parent): string {
Expand Down
13 changes: 8 additions & 5 deletions packages/protons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,15 @@
*
* Sometimes this is undesirable due to [performance issues](https://betterprogramming.pub/the-downsides-of-bigints-in-javascript-6350fd807d) or code legibility.
*
* It's possible to override the JavaScript type 64 bit fields will deserialize to:
* It's possible to override the JavaScript type 64 bit fields will deserialize
* to, including repeated fields and map key/values:
*
* ```
* message MyMessage {
* repeated int64 bigintField = 1;
* repeated int64 numberField = 2 [jstype = JS_NUMBER];
* repeated int64 stringField = 3 [jstype = JS_STRING];
* int64 bigintField = 1;
* int64 numberField = 2 [jstype = JS_NUMBER];
* repeated int64 stringArray = 3 [jstype = JS_STRING];
* map<int64, int64, bigintMap = 4 [jskeytype = JS_STRING, jsvaluetype = JS_STRING];
* }
* ```
*
Expand All @@ -181,7 +183,8 @@
*
* console.info(typeof message.bigintField) // bigint
* console.info(typeof message.numberField) // number
* console.info(typeof message.stringField) // string
* console.info(typeof message.stringArray[0]) // string
* console.info(typeof message.bigintMap.get('5')) // string
* ```
*
* ## Missing features
Expand Down
22 changes: 14 additions & 8 deletions packages/protons/src/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@ export class Message implements Type {
fields: {
key: {
type: fieldDef.keyType,
id: 1
id: 1,
options: {
jstype: fieldDef.options?.jskeytype
}
},
value: {
type: fieldDef.type,
id: 2
id: 2,
options: {
jstype: fieldDef.options?.jsvaluetype
}
}
}
}
Expand Down Expand Up @@ -506,15 +512,15 @@ ${fields
name: `${fieldPrefix === '$.' ? this.pbType : ''}${camelize(field.name)}FieldEvent`,
fields: [
`field: '${fieldPrefix}${field.name}{}'`,
`key: ${keyType.jsType}`,
`value: ${valueType.jsType}`
`key: ${field.jsKeyTypeOverride ?? keyType.jsType}`,
`value: ${field.jsValueTypeOverride ?? valueType.jsType}`
],
type: 'collection-primitive-member'
})
} else if (valueType instanceof Message) {
addMessageFields(field, valueType, [
`key: ${keyType.jsType}`,
`value: ${valueType.jsType}`
`key: ${field.jsKeyTypeOverride ?? keyType.jsType}`,
`value: ${field.jsValueTypeOverride ?? valueType.jsType}`
])
}
} else if (field instanceof ArrayField) {
Expand All @@ -526,7 +532,7 @@ ${fields
fields: [
`field: '${fieldPrefix}${field.name}[]'`,
'index: number',
`value: ${type.jsType}`
`value: ${field.jsTypeOverride ?? type.jsType}`
],
type: 'collection-primitive-member'
})
Expand All @@ -543,7 +549,7 @@ ${fields
name: `${fieldPrefix === '$.' ? this.pbType : ''}${camelize(field.name)}FieldEvent`,
fields: [
`field: '${fieldPrefix}${field.name}'`,
`value: ${type.jsType}`
`value: ${field.jsTypeOverride ?? type.jsType}`
],
type: 'field'
})
Expand Down
8 changes: 6 additions & 2 deletions packages/protons/test/custom-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ describe('custom options', () => {
ui64: 5,
si64: 5,
f64: 5,
sf64: 5
sf64: 5,
i64Array: [],
i64Map: new Map()
}

expect(CustomOptionNumber.decode(CustomOptionNumber.encode(obj)))
Expand All @@ -23,7 +25,9 @@ describe('custom options', () => {
ui64: '5',
si64: '5',
f64: '5',
sf64: '5'
sf64: '5',
i64Array: ['5'],
i64Map: new Map([['5', '5']])
}

expect(CustomOptionString.decode(CustomOptionString.encode(obj)))
Expand Down
4 changes: 4 additions & 0 deletions packages/protons/test/fixtures/custom-option-jstype.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ message CustomOptionNumber {
sint64 si64 = 4 [jstype = JS_NUMBER];
fixed64 f64 = 5 [jstype = JS_NUMBER];
sfixed64 sf64 = 6 [jstype = JS_NUMBER];
repeated int64 i64Array = 7 [jstype = JS_NUMBER];
map<int64, int64> i64Map = 8 [jskeytype = JS_NUMBER, jsvaluetype = JS_NUMBER];
}

message CustomOptionString {
Expand All @@ -16,4 +18,6 @@ message CustomOptionString {
sint64 si64 = 4 [jstype = JS_STRING];
fixed64 f64 = 5 [jstype = JS_STRING];
sfixed64 sf64 = 6 [jstype = JS_STRING];
repeated int64 i64Array = 7 [jstype = JS_STRING];
map<int64, int64> i64Map = 8 [jskeytype = JS_STRING, jsvaluetype = JS_STRING];
}
Loading
Loading