Skip to content
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

Support jstype overrides #37

Merged
merged 2 commits into from
Feb 11, 2019
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
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