diff --git a/README.md b/README.md index 9dcdcb22b..a3499904e 100644 --- a/README.md +++ b/README.md @@ -315,10 +315,9 @@ Documentation ------------- * [Google's Developer Guide](https://developers.google.com/protocol-buffers/docs/overview) - * [protobuf.js API Documentation](http://dcode.io/protobuf.js/) and [CHANGELOG](https://github.com/dcodeIO/protobuf.js/blob/master/CHANGELOG.md) - -* [Questions and answers on StackOverflow](http://stackoverflow.com/questions/tagged/protobuf.js) +* [Frequently asked questions](https://github.com/dcodeIO/protobuf.js/wiki) on our wiki +* [More questions and answers](http://stackoverflow.com/questions/tagged/protobuf.js) on StackOverflow Command line ------------ diff --git a/index.d.ts b/index.d.ts index 3ac0451e6..5ce9eb9fb 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,5 @@ // $> pbts --main --global protobuf --out index.d.ts src -// Generated Mon, 02 Jan 2017 15:34:42 UTC +// Generated Tue, 03 Jan 2017 02:10:19 UTC export as namespace protobuf; diff --git a/lib/tape-adapter.js b/lib/tape-adapter.js index c3744ff62..a4c9aa285 100644 --- a/lib/tape-adapter.js +++ b/lib/tape-adapter.js @@ -13,8 +13,8 @@ Test.prototype.deepEqual = Test.prototype.same = function (a, b, msg, extra) { this._assert(deepEqual(a, b, { strict: true }), { - message : msg || 'should be equivalent', - operator : 'deepEqual', + message : msg || "should be equivalent", + operator : "deepEqual", actual : a, expected : b, extra : extra diff --git a/src/converter.js b/src/converter.js index 81f7c5873..f98b6fc8b 100644 --- a/src/converter.js +++ b/src/converter.js @@ -10,16 +10,20 @@ var sprintf = util.codegen.sprintf; function genConvert(field, fieldIndex, prop) { if (field.resolvedType) return field.resolvedType instanceof Enum - ? sprintf("f.enums(s%s,%d,types[%d].values,o)", prop, 0, fieldIndex) - : sprintf("types[%d].convert(s%s,f,o)", fieldIndex, prop); + // enums + ? sprintf("f.enums(s%s,%d,types[%d].values,o)", prop, 0, fieldIndex) + // recurse into messages + : sprintf("types[%d].convert(s%s,f,o)", fieldIndex, prop); switch (field.type) { case "int64": case "uint64": case "sint64": case "fixed64": case "sfixed64": + // longs 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 null; @@ -45,6 +49,8 @@ function converter(mtype) { var convert; fields.forEach(function(field, i) { var prop = util.safeProp(field.resolve().name); + + // repeated if (field.repeated) { gen ("if(s%s&&s%s.length){", prop, prop) ("d%s=[]", prop) @@ -56,11 +62,14 @@ function converter(mtype) { gen ("}else if(o.defaults||o.arrays)") ("d%s=[]", prop); + + // non-repeated } else if (convert = genConvert(field, i, prop)) gen ("d%s=%s", prop, convert); else gen ("if(d%s===undefined&&o.defaults)", prop) ("d%s=%j", prop, field.defaultValue); + }); gen ("}"); diff --git a/src/index.js b/src/index.js index ba674ffbf..da818b9c9 100644 --- a/src/index.js +++ b/src/index.js @@ -16,6 +16,7 @@ var protobuf = global.protobuf = exports; * @param {Root} root Root namespace, defaults to create a new one if omitted. * @param {LoadCallback} callback Callback function * @returns {undefined} + * @see {@link Root#load} */ function load(filename, root, callback) { if (typeof root === "function") { @@ -34,6 +35,7 @@ function load(filename, root, callback) { * @param {string|string[]} filename One or multiple files to load * @param {LoadCallback} callback Callback function * @returns {undefined} + * @see {@link Root#load} * @variation 2 */ // function load(filename:string, callback:LoadCallback):undefined @@ -45,6 +47,7 @@ function load(filename, root, callback) { * @param {string|string[]} filename One or multiple files to load * @param {Root} [root] Root namespace, defaults to create a new one if omitted. * @returns {Promise} Promise + * @see {@link Root#load} * @variation 3 */ // function load(filename:string, [root:Root]):Promise @@ -57,6 +60,7 @@ protobuf.load = load; * @param {Root} [root] Root namespace, defaults to create a new one if omitted. * @returns {Root} Root namespace * @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid + * @see {@link Root#loadSync} */ function loadSync(filename, root) { if (!root) diff --git a/src/parse.js b/src/parse.js index fb8439ee1..765463a27 100644 --- a/src/parse.js +++ b/src/parse.js @@ -1,7 +1,8 @@ "use strict"; module.exports = parse; -parse.keepCase = false; +parse.filename = null; +parse.defaults = { keepCase: false }; var tokenize = require("./tokenize"), Root = require("./root"), @@ -60,10 +61,10 @@ function camelCase(str) { * @function * @param {string} source Source contents * @param {Root} root Root to populate - * @param {ParseOptions} [options] Parse options + * @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 {boolean} keepCase=false When set to `true`, always keeps field casing instead of converting to camel case when no {@link ParseOptions} are specified + * @property {ParseOptions} defaults Default {@link ParseOptions} */ function parse(source, root, options) { /* eslint-disable callback-return */ @@ -72,7 +73,7 @@ function parse(source, root, options) { root = new Root(); } if (!options) - options = { keepCase: parse.keepCase }; + options = parse.defaults; var tn = tokenize(source), next = tn.next, @@ -664,7 +665,7 @@ function parse(source, root, options) { * @name parse * @function * @param {string} source Source contents - * @param {ParseOptions} [options] Parse options + * @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted. * @returns {ParserResult} Parser result * @variation 2 */ diff --git a/src/root.js b/src/root.js index 3f91f2a20..7d19f4b1e 100644 --- a/src/root.js +++ b/src/root.js @@ -215,7 +215,7 @@ RootPrototype.load = function load(filename, options, callback) { * @name Root#load * @function * @param {string|string[]} filename Names of one or multiple files to load - * @param {ParseOptions} [options] Parse options + * @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted. * @returns {Promise} Promise * @variation 3 */ @@ -224,7 +224,7 @@ RootPrototype.load = function load(filename, options, callback) { /** * Synchronously loads one or multiple .proto or preprocessed .json files into this root namespace. * @param {string|string[]} filename Names of one or multiple files to load - * @param {ParseOptions} [options] Parse options + * @param {ParseOptions} [options] Parse options. Defaults to {@link parse.defaults} when omitted. * @returns {Root} Root namespace * @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid */