Skip to content

Commit 4e86331

Browse files
authored
Merge pull request #37 from luk3thomas/support-jstype-overrides
Support jstype overrides
2 parents df6e121 + 5881b1a commit 4e86331

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,39 @@ export class BookServiceClient extends grpc.Client implements IBookServiceClient
357357
}
358358
```
359359

360+
## Gotchas
361+
362+
JavaScript can safely handle numbers below `Number.MAX_SAFE_INTEGER`. Beyond the
363+
limit, it begins to overflow:
364+
365+
```
366+
> 90071992547409912131 + 1
367+
90071992547409920000
368+
```
369+
370+
If you are expecting large, 64bit fields consider using a `jstype` option to
371+
override the field type.
372+
373+
```proto
374+
# example.proto
375+
message Example {
376+
fixed64 id = 1 [jstype = JS_STRING];
377+
}
378+
```
379+
380+
```typescript
381+
// example_pb.d.ts
382+
export namespace Example {
383+
export type AsObject = {
384+
id: string
385+
}
386+
}
387+
```
388+
360389
## Changes
390+
### 2.4.3
391+
Add support for `[jstype = JS_STRING]` overrides
392+
361393
### 2.4.2
362394
Update grpc version in package.json of examples, to keep consistent.
363395
Update handlebars-helpers version to 0.10.0 to fix vulnerability version of randomatic.

build/lib/format/partial/FieldTypesFormatter.js

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/lib/format/partial/MessageFormatter.js

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/format/partial/FieldTypesFormatter.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ import {Utility} from "../../Utility";
55
export const MESSAGE_TYPE = 11;
66
export const BYTES_TYPE = 12;
77
export const ENUM_TYPE = 14;
8+
export const JS_NORMAL = 0;
9+
export const JS_STRING = 1;
10+
export const JS_NUMBER = 2;
811

9-
const TypeNumToTypeString: { [key: number]: string } = {};
12+
interface TypeMap {
13+
[key: number]: string
14+
}
15+
16+
const TypeNumToTypeString = <TypeMap>{};
1017
TypeNumToTypeString[1] = "number"; // TYPE_DOUBLE
1118
TypeNumToTypeString[2] = "number"; // TYPE_FLOAT
1219
TypeNumToTypeString[3] = "number"; // TYPE_INT64
@@ -26,12 +33,21 @@ TypeNumToTypeString[16] = "number"; // TYPE_SFIXED64
2633
TypeNumToTypeString[17] = "number"; // TYPE_SINT32 - Uses ZigZag encoding.
2734
TypeNumToTypeString[18] = "number"; // TYPE_SINT64 - Uses ZigZag encoding.
2835

36+
const JsTypeNumToTypeString = <TypeMap>{};
37+
JsTypeNumToTypeString[JS_NORMAL] = null; // [jstype = JS_NORMAL]
38+
JsTypeNumToTypeString[JS_STRING] = "string"; // [jstype = JS_STRING]
39+
JsTypeNumToTypeString[JS_NUMBER] = "number"; // [jstype = JS_NUMBER]
40+
2941
export namespace FieldTypesFormatter {
3042

3143
export function getTypeName(fieldTypeNum: number): string {
3244
return TypeNumToTypeString[fieldTypeNum];
3345
}
3446

47+
export function getJsTypeName(fieldTypeNum: number): string {
48+
return fieldTypeNum === JS_NORMAL ? null : JsTypeNumToTypeString[fieldTypeNum];
49+
}
50+
3551
export function getFieldType(type: FieldDescriptorProto.Type,
3652
typeName: string,
3753
currentFileName: string,

src/lib/format/partial/MessageFormatter.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,18 @@ export namespace MessageFormatter {
192192
fieldData.exportType = exportType;
193193

194194
} else {
195+
let type = FieldTypesFormatter.getTypeName(fieldData.type);
196+
197+
// Check for [jstype = JS_STRING] overrides
198+
const options = field.getOptions()
199+
if (options && options.hasJstype()) {
200+
const jstype = FieldTypesFormatter.getJsTypeName(options.getJstype());
201+
if (jstype) {
202+
type = jstype
203+
}
204+
}
195205

196-
exportType = fieldData.exportType = FieldTypesFormatter.getTypeName(fieldData.type);
206+
exportType = fieldData.exportType = type
197207

198208
}
199209

0 commit comments

Comments
 (0)