Skip to content

Commit

Permalink
Always use first defined enum value as field default, fixes #613
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 3, 2017
1 parent 0643f93 commit b52089e
Show file tree
Hide file tree
Showing 19 changed files with 166 additions and 117 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

**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/)).

**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.
**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.

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

**Recommended read:** [Changes in protobuf.js 6.0](https://github.com/dcodeIO/protobuf.js/wiki/Changes-in-protobuf.js-6.0)
Expand Down
72 changes: 39 additions & 33 deletions dist/noparse/protobuf.js

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

2 changes: 1 addition & 1 deletion dist/noparse/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/noparse/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/noparse/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/noparse/protobuf.min.js.map

Large diffs are not rendered by default.

74 changes: 41 additions & 33 deletions dist/protobuf.js

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

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.js

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

2 changes: 1 addition & 1 deletion dist/runtime/protobuf.min.js

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

Binary file modified dist/runtime/protobuf.min.js.gz
Binary file not shown.
33 changes: 31 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// $> pbts --main --global protobuf --out index.d.ts src
// Generated Tue, 03 Jan 2017 15:34:45 UTC
// Generated Tue, 03 Jan 2017 23:45:16 UTC

export as namespace protobuf;

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

/**
* Creates a message from a JSON object by converting strings and numbers to their respective field types.
* @name Class#from
* @function
* @param {Object.<string,*>} object JSON object
* @param {MessageConversionOptions} [options] Options
* @returns {Message} Message instance
*/
from(object: { [k: string]: any }, options?: MessageConversionOptions): Message;

/**
* Encodes a message of this type.
* @name Class#encode
Expand Down Expand Up @@ -74,6 +84,17 @@ export class Class {
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
verify(message: (Message|Object)): string;

/**
* Converts an object or runtime message of this type.
* @name Class#convert
* @function
* @param {Message|Object} source Source object or runtime message
* @param {ConverterImpl} impl Converter implementation to use, i.e. {@link converters.json} or {@link converters.message}
* @param {Object.<string,*>} [options] Conversion options
* @returns {Message|Object} Converted object or runtime message
*/
convert(source: (Message|Object), impl: ConverterImpl, options?: { [k: string]: any }): (Message|Object);
}

/**
Expand Down Expand Up @@ -375,7 +396,13 @@ export class Field extends ReflectionObject {
partOf: OneOf;

/**
* The field's default value. Only relevant when working with proto2.
* The field type's default value.
* @type {*}
*/
typeDefault: any;

/**
* The field's default value on prototypes.
* @type {*}
*/
defaultValue: any;
Expand Down Expand Up @@ -1136,6 +1163,8 @@ export function parse(source: string, root: Root, options?: ParseOptions): Parse
* @param {string} source Source contents
* @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted.
* @returns {ParserResult} Parser result
* @property {string} filename=null Currently processing file name for error reporting, if known
* @property {ParseOptions} defaults Default {@link ParseOptions}
* @variation 2
*/
export function parse(source: string, options?: ParseOptions): ParserResult;
Expand Down
Binary file modified pbjs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function genConvert(field, fieldIndex, prop) {
if (field.resolvedType)
return field.resolvedType instanceof Enum
// enums
? sprintf("f.enums(s%s,%d,types[%d].values,o)", prop, 0, fieldIndex)
? sprintf("f.enums(s%s,%d,types[%d].values,o)", prop, field.typeDefault, fieldIndex)
// recurse into messages
: sprintf("types[%d].convert(s%s,f,o)", fieldIndex, prop);
switch (field.type) {
Expand All @@ -24,7 +24,7 @@ function genConvert(field, fieldIndex, prop) {
return sprintf("f.longs(s%s,%d,%d,%j,o)", prop, 0, 0, field.type.charAt(0) === "u");
case "bytes":
// bytes
return sprintf("f.bytes(s%s,%j,o)", prop, Array.prototype.slice.call(field.defaultValue));
return sprintf("f.bytes(s%s,%j,o)", prop, Array.prototype.slice.call(field.typeDefault));
}
return null;
}
Expand Down Expand Up @@ -68,7 +68,7 @@ function converter(mtype) {
("d%s=%s", prop, convert);
else gen
("if(d%s===undefined&&o.defaults)", prop)
("d%s=%j", prop, field.defaultValue);
("d%s=%j", prop, field.typeDefault /* == field.defaultValue */);

});
gen
Expand Down
Loading

0 comments on commit b52089e

Please sign in to comment.