Permalink
Browse files

Always use first defined enum value as field default, fixes #613

  • Loading branch information...
dcodeIO committed Jan 3, 2017
1 parent 0643f93 commit b52089efcb9827537012bebe83d1a15738e214f4
@@ -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
@@ -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)

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +20 Bytes (100%) dist/noparse/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +23 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
BIN +2 Bytes (100%) dist/runtime/protobuf.min.js.gz
Binary file not shown.
@@ -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;
@@ -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
@@ -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);
}
/**
@@ -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;
@@ -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;
BIN -78 Bytes (98%) pbjs.png
Binary file not shown.
@@ -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) {
@@ -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;
}
@@ -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
Oops, something went wrong.

0 comments on commit b52089e

Please sign in to comment.