Skip to content

Commit

Permalink
Merge pull request #37 from luk3thomas/support-jstype-overrides
Browse files Browse the repository at this point in the history
Support jstype overrides
  • Loading branch information
agreatfool authored Feb 11, 2019
2 parents df6e121 + 5881b1a commit 4e86331
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,39 @@ export class BookServiceClient extends grpc.Client implements IBookServiceClient
}
```

## Gotchas

JavaScript can safely handle numbers below `Number.MAX_SAFE_INTEGER`. Beyond the
limit, it begins to overflow:

```
> 90071992547409912131 + 1
90071992547409920000
```

If you are expecting large, 64bit fields consider using a `jstype` option to
override the field type.

```proto
# example.proto
message Example {
fixed64 id = 1 [jstype = JS_STRING];
}
```

```typescript
// example_pb.d.ts
export namespace Example {
export type AsObject = {
id: string
}
}
```

## Changes
### 2.4.3
Add support for `[jstype = JS_STRING]` overrides

### 2.4.2
Update grpc version in package.json of examples, to keep consistent.
Update handlebars-helpers version to 0.10.0 to fix vulnerability version of randomatic.
Expand Down
11 changes: 11 additions & 0 deletions build/lib/format/partial/FieldTypesFormatter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion build/lib/format/partial/MessageFormatter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion src/lib/format/partial/FieldTypesFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import {Utility} from "../../Utility";
export const MESSAGE_TYPE = 11;
export const BYTES_TYPE = 12;
export const ENUM_TYPE = 14;
export const JS_NORMAL = 0;
export const JS_STRING = 1;
export const JS_NUMBER = 2;

const TypeNumToTypeString: { [key: number]: string } = {};
interface TypeMap {
[key: number]: string
}

const TypeNumToTypeString = <TypeMap>{};
TypeNumToTypeString[1] = "number"; // TYPE_DOUBLE
TypeNumToTypeString[2] = "number"; // TYPE_FLOAT
TypeNumToTypeString[3] = "number"; // TYPE_INT64
Expand All @@ -26,12 +33,21 @@ TypeNumToTypeString[16] = "number"; // TYPE_SFIXED64
TypeNumToTypeString[17] = "number"; // TYPE_SINT32 - Uses ZigZag encoding.
TypeNumToTypeString[18] = "number"; // TYPE_SINT64 - Uses ZigZag encoding.

const JsTypeNumToTypeString = <TypeMap>{};
JsTypeNumToTypeString[JS_NORMAL] = null; // [jstype = JS_NORMAL]
JsTypeNumToTypeString[JS_STRING] = "string"; // [jstype = JS_STRING]
JsTypeNumToTypeString[JS_NUMBER] = "number"; // [jstype = JS_NUMBER]

export namespace FieldTypesFormatter {

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

export function getJsTypeName(fieldTypeNum: number): string {
return fieldTypeNum === JS_NORMAL ? null : JsTypeNumToTypeString[fieldTypeNum];
}

export function getFieldType(type: FieldDescriptorProto.Type,
typeName: string,
currentFileName: string,
Expand Down
12 changes: 11 additions & 1 deletion src/lib/format/partial/MessageFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,18 @@ export namespace MessageFormatter {
fieldData.exportType = exportType;

} else {
let type = FieldTypesFormatter.getTypeName(fieldData.type);

// Check for [jstype = JS_STRING] overrides
const options = field.getOptions()
if (options && options.hasJstype()) {
const jstype = FieldTypesFormatter.getJsTypeName(options.getJstype());
if (jstype) {
type = jstype
}
}

exportType = fieldData.exportType = FieldTypesFormatter.getTypeName(fieldData.type);
exportType = fieldData.exportType = type

}

Expand Down

0 comments on commit 4e86331

Please sign in to comment.