Skip to content

Commit b52089e

Browse files
committed
Always use first defined enum value as field default, fixes #613
1 parent 0643f93 commit b52089e

19 files changed

+166
-117
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
**Protocol Buffers** are a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more, originally designed at Google ([see](https://developers.google.com/protocol-buffers/)).
66

7-
**protobuf.js** is a pure JavaScript implementation with TypeScript support for node and the browser. It efficiently encodes plain objects and custom classes and works out of the box with .proto files.
7+
**protobuf.js** is a pure JavaScript implementation with TypeScript support for node and the browser. It efficiently encodes all teh codez and works out of the box with .proto files.
88

99
[travis-image]: https://img.shields.io/travis/dcodeIO/protobuf.js.svg
1010
[travis-url]: https://travis-ci.org/dcodeIO/protobuf.js
@@ -13,7 +13,7 @@
1313
[npm-image]: https://img.shields.io/npm/v/protobufjs.svg
1414
[npm-url]: https://npmjs.org/package/protobufjs
1515
[npm-dl-image]: https://img.shields.io/npm/dm/protobufjs.svg
16-
[paypal-image]: https://img.shields.io/badge/paypal-donate-yellow.svg
16+
[paypal-image]: https://img.shields.io/badge/donate-feels%20good%2C%20I%20promise-333333.svg
1717
[paypal-url]: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=dcode%40dcode.io&item_name=%3C3%20protobuf.js
1818

1919
**Recommended read:** [Changes in protobuf.js 6.0](https://github.com/dcodeIO/protobuf.js/wiki/Changes-in-protobuf.js-6.0)

dist/noparse/protobuf.js

+39-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.min.js.gz

20 Bytes
Binary file not shown.

dist/noparse/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js

+41-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

23 Bytes
Binary file not shown.

dist/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/runtime/protobuf.min.js.gz

2 Bytes
Binary file not shown.

index.d.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// $> pbts --main --global protobuf --out index.d.ts src
2-
// Generated Tue, 03 Jan 2017 15:34:45 UTC
2+
// Generated Tue, 03 Jan 2017 23:45:16 UTC
33

44
export as namespace protobuf;
55

@@ -28,6 +28,16 @@ export class Class {
2828
*/
2929
static create(type: Type, ctor?: any): Message;
3030

31+
/**
32+
* Creates a message from a JSON object by converting strings and numbers to their respective field types.
33+
* @name Class#from
34+
* @function
35+
* @param {Object.<string,*>} object JSON object
36+
* @param {MessageConversionOptions} [options] Options
37+
* @returns {Message} Message instance
38+
*/
39+
from(object: { [k: string]: any }, options?: MessageConversionOptions): Message;
40+
3141
/**
3242
* Encodes a message of this type.
3343
* @name Class#encode
@@ -74,6 +84,17 @@ export class Class {
7484
* @returns {?string} `null` if valid, otherwise the reason why it is not
7585
*/
7686
verify(message: (Message|Object)): string;
87+
88+
/**
89+
* Converts an object or runtime message of this type.
90+
* @name Class#convert
91+
* @function
92+
* @param {Message|Object} source Source object or runtime message
93+
* @param {ConverterImpl} impl Converter implementation to use, i.e. {@link converters.json} or {@link converters.message}
94+
* @param {Object.<string,*>} [options] Conversion options
95+
* @returns {Message|Object} Converted object or runtime message
96+
*/
97+
convert(source: (Message|Object), impl: ConverterImpl, options?: { [k: string]: any }): (Message|Object);
7798
}
7899

79100
/**
@@ -375,7 +396,13 @@ export class Field extends ReflectionObject {
375396
partOf: OneOf;
376397

377398
/**
378-
* The field's default value. Only relevant when working with proto2.
399+
* The field type's default value.
400+
* @type {*}
401+
*/
402+
typeDefault: any;
403+
404+
/**
405+
* The field's default value on prototypes.
379406
* @type {*}
380407
*/
381408
defaultValue: any;
@@ -1136,6 +1163,8 @@ export function parse(source: string, root: Root, options?: ParseOptions): Parse
11361163
* @param {string} source Source contents
11371164
* @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
11381165
* @returns {ParserResult} Parser result
1166+
* @property {string} filename=null Currently processing file name for error reporting, if known
1167+
* @property {ParseOptions} defaults Default {@link ParseOptions}
11391168
* @variation 2
11401169
*/
11411170
export function parse(source: string, options?: ParseOptions): ParserResult;

pbjs.png

-78 Bytes
Loading

src/converter.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function genConvert(field, fieldIndex, prop) {
1111
if (field.resolvedType)
1212
return field.resolvedType instanceof Enum
1313
// enums
14-
? sprintf("f.enums(s%s,%d,types[%d].values,o)", prop, 0, fieldIndex)
14+
? sprintf("f.enums(s%s,%d,types[%d].values,o)", prop, field.typeDefault, fieldIndex)
1515
// recurse into messages
1616
: sprintf("types[%d].convert(s%s,f,o)", fieldIndex, prop);
1717
switch (field.type) {
@@ -24,7 +24,7 @@ function genConvert(field, fieldIndex, prop) {
2424
return sprintf("f.longs(s%s,%d,%d,%j,o)", prop, 0, 0, field.type.charAt(0) === "u");
2525
case "bytes":
2626
// bytes
27-
return sprintf("f.bytes(s%s,%j,o)", prop, Array.prototype.slice.call(field.defaultValue));
27+
return sprintf("f.bytes(s%s,%j,o)", prop, Array.prototype.slice.call(field.typeDefault));
2828
}
2929
return null;
3030
}
@@ -68,7 +68,7 @@ function converter(mtype) {
6868
("d%s=%s", prop, convert);
6969
else gen
7070
("if(d%s===undefined&&o.defaults)", prop)
71-
("d%s=%j", prop, field.defaultValue);
71+
("d%s=%j", prop, field.typeDefault /* == field.defaultValue */);
7272

7373
});
7474
gen

0 commit comments

Comments
 (0)