2 * The ProtoBuf namespace.
\r
10 * @type {!function(new: ByteBuffer, ...[*])}
\r
13 ProtoBuf.ByteBuffer = ByteBuffer;
\r
16 * @type {?function(new: Long, ...[*])}
\r
19 ProtoBuf.Long = ByteBuffer.Long || null;
\r
22 * ProtoBuf.js version.
\r
27 ProtoBuf.VERSION = "/*?= VERSION */";
\r
31 * @type {Object.<string,number>}
\r
35 ProtoBuf.WIRE_TYPES = {};
\r
42 ProtoBuf.WIRE_TYPES.VARINT = 0;
\r
45 * Fixed 64 bits wire type.
\r
50 ProtoBuf.WIRE_TYPES.BITS64 = 1;
\r
53 * Length delimited wire type.
\r
58 ProtoBuf.WIRE_TYPES.LDELIM = 2;
\r
61 * Start group wire type.
\r
66 ProtoBuf.WIRE_TYPES.STARTGROUP = 3;
\r
69 * End group wire type.
\r
74 ProtoBuf.WIRE_TYPES.ENDGROUP = 4;
\r
77 * Fixed 32 bits wire type.
\r
82 ProtoBuf.WIRE_TYPES.BITS32 = 5;
\r
85 * Packable wire types.
\r
86 * @type {!Array.<number>}
\r
90 ProtoBuf.PACKABLE_WIRE_TYPES = [
\r
91 ProtoBuf.WIRE_TYPES.VARINT,
\r
92 ProtoBuf.WIRE_TYPES.BITS64,
\r
93 ProtoBuf.WIRE_TYPES.BITS32
\r
99 * @type {!Object.<string,{name: string, wireType: number, defaultValue: *}>}
\r
104 // According to the protobuf spec.
\r
107 wireType: ProtoBuf.WIRE_TYPES.VARINT,
\r
112 wireType: ProtoBuf.WIRE_TYPES.VARINT,
\r
117 wireType: ProtoBuf.WIRE_TYPES.VARINT,
\r
122 wireType: ProtoBuf.WIRE_TYPES.VARINT,
\r
123 defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined
\r
127 wireType: ProtoBuf.WIRE_TYPES.VARINT,
\r
128 defaultValue: ProtoBuf.Long ? ProtoBuf.Long.UZERO : undefined
\r
132 wireType: ProtoBuf.WIRE_TYPES.VARINT,
\r
133 defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined
\r
137 wireType: ProtoBuf.WIRE_TYPES.VARINT,
\r
138 defaultValue: false
\r
142 wireType: ProtoBuf.WIRE_TYPES.BITS64,
\r
147 wireType: ProtoBuf.WIRE_TYPES.LDELIM,
\r
152 wireType: ProtoBuf.WIRE_TYPES.LDELIM,
\r
153 defaultValue: null // overridden in the code, must be a unique instance
\r
157 wireType: ProtoBuf.WIRE_TYPES.BITS32,
\r
162 wireType: ProtoBuf.WIRE_TYPES.BITS32,
\r
167 wireType: ProtoBuf.WIRE_TYPES.BITS64,
\r
168 defaultValue: ProtoBuf.Long ? ProtoBuf.Long.UZERO : undefined
\r
172 wireType: ProtoBuf.WIRE_TYPES.BITS64,
\r
173 defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined
\r
177 wireType: ProtoBuf.WIRE_TYPES.BITS32,
\r
182 wireType: ProtoBuf.WIRE_TYPES.VARINT,
\r
187 wireType: ProtoBuf.WIRE_TYPES.LDELIM,
\r
192 wireType: ProtoBuf.WIRE_TYPES.STARTGROUP,
\r
198 * Valid map key types.
\r
199 * @type {!Array.<!Object.<string,{name: string, wireType: number, defaultValue: *}>>}
\r
203 ProtoBuf.MAP_KEY_TYPES = [
\r
204 ProtoBuf.TYPES["int32"],
\r
205 ProtoBuf.TYPES["sint32"],
\r
206 ProtoBuf.TYPES["sfixed32"],
\r
207 ProtoBuf.TYPES["uint32"],
\r
208 ProtoBuf.TYPES["fixed32"],
\r
209 ProtoBuf.TYPES["int64"],
\r
210 ProtoBuf.TYPES["sint64"],
\r
211 ProtoBuf.TYPES["sfixed64"],
\r
212 ProtoBuf.TYPES["uint64"],
\r
213 ProtoBuf.TYPES["fixed64"],
\r
214 ProtoBuf.TYPES["bool"],
\r
215 ProtoBuf.TYPES["string"],
\r
216 ProtoBuf.TYPES["bytes"]
\r
220 * Minimum field id.
\r
225 ProtoBuf.ID_MIN = 1;
\r
228 * Maximum field id.
\r
233 ProtoBuf.ID_MAX = 0x1FFFFFFF;
\r
236 * If set to `true`, field names will be converted from underscore notation to camel case. Defaults to `false`.
\r
237 * Must be set prior to parsing.
\r
241 ProtoBuf.convertFieldsToCamelCase = false;
\r
244 * By default, messages are populated with (setX, set_x) accessors for each field. This can be disabled by
\r
245 * setting this to `false` prior to building messages.
\r
249 ProtoBuf.populateAccessors = true;
\r
252 * By default, messages are populated with default values if a field is not present on the wire. To disable
\r
253 * this behavior, set this setting to `false`.
\r
257 ProtoBuf.populateDefaults = true;
\r
259 //? include("ProtoBuf/Util.js");
\r
261 //? include("ProtoBuf/Lang.js");
\r
263 //? if (DOTPROTO) include("ProtoBuf/DotProto.js");
\r
265 //? include("ProtoBuf/Reflect.js");
\r
267 //? include("ProtoBuf/Builder.js");
\r
269 //? include("ProtoBuf/Map.js");
\r
271 //? if (DOTPROTO) {
\r
274 * Loads a .proto string and returns the Builder.
\r
275 * @param {string} proto .proto file contents
\r
276 * @param {(ProtoBuf.Builder|string|{root: string, file: string})=} builder Builder to append to. Will create a new one if omitted.
\r
277 * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
\r
278 * @return {ProtoBuf.Builder} Builder to create new messages
\r
279 * @throws {Error} If the definition cannot be parsed or built
\r
282 ProtoBuf.loadProto = function(proto, builder, filename) {
\r
283 if (typeof builder === 'string' || (builder && typeof builder["file"] === 'string' && typeof builder["root"] === 'string'))
\r
284 filename = builder,
\r
285 builder = undefined;
\r
286 return ProtoBuf.loadJson(ProtoBuf.DotProto.Parser.parse(proto), builder, filename);
\r
290 * Loads a .proto string and returns the Builder. This is an alias of {@link ProtoBuf.loadProto}.
\r
292 * @param {string} proto .proto file contents
\r
293 * @param {(ProtoBuf.Builder|string)=} builder Builder to append to. Will create a new one if omitted.
\r
294 * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
\r
295 * @return {ProtoBuf.Builder} Builder to create new messages
\r
296 * @throws {Error} If the definition cannot be parsed or built
\r
299 ProtoBuf.protoFromString = ProtoBuf.loadProto; // Legacy
\r
302 * Loads a .proto file and returns the Builder.
\r
303 * @param {string|{root: string, file: string}} filename Path to proto file or an object specifying 'file' with
\r
304 * an overridden 'root' path for all imported files.
\r
305 * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
\r
306 * the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
\r
307 * file will be read synchronously and this function will return the Builder.
\r
308 * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
\r
309 * @return {?ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
\r
310 * request has failed), else undefined
\r
313 ProtoBuf.loadProtoFile = function(filename, callback, builder) {
\r
314 if (callback && typeof callback === 'object')
\r
315 builder = callback,
\r
317 else if (!callback || typeof callback !== 'function')
\r
320 return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {
\r
321 if (contents === null) {
\r
322 callback(Error("Failed to fetch file"));
\r
326 callback(null, ProtoBuf.loadProto(contents, builder, filename));
\r
331 var contents = ProtoBuf.Util.fetch(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename);
\r
332 return contents === null ? null : ProtoBuf.loadProto(contents, builder, filename);
\r
336 * Loads a .proto file and returns the Builder. This is an alias of {@link ProtoBuf.loadProtoFile}.
\r
338 * @param {string|{root: string, file: string}} filename Path to proto file or an object specifying 'file' with
\r
339 * an overridden 'root' path for all imported files.
\r
340 * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
\r
341 * the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
\r
342 * file will be read synchronously and this function will return the Builder.
\r
343 * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
\r
344 * @return {!ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
\r
345 * request has failed), else undefined
\r
348 ProtoBuf.protoFromFile = ProtoBuf.loadProtoFile; // Legacy
\r
353 * Constructs a new empty Builder.
\r
354 * @param {Object.<string,*>=} options Builder options, defaults to global options set on ProtoBuf
\r
355 * @return {!ProtoBuf.Builder} Builder
\r
358 ProtoBuf.newBuilder = function(options) {
\r
359 options = options || {};
\r
360 if (typeof options['convertFieldsToCamelCase'] === 'undefined')
\r
361 options['convertFieldsToCamelCase'] = ProtoBuf.convertFieldsToCamelCase;
\r
362 if (typeof options['populateAccessors'] === 'undefined')
\r
363 options['populateAccessors'] = ProtoBuf.populateAccessors;
\r
364 return new ProtoBuf.Builder(options);
\r
368 * Loads a .json definition and returns the Builder.
\r
369 * @param {!*|string} json JSON definition
\r
370 * @param {(ProtoBuf.Builder|string|{root: string, file: string})=} builder Builder to append to. Will create a new one if omitted.
\r
371 * @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
\r
372 * @return {ProtoBuf.Builder} Builder to create new messages
\r
373 * @throws {Error} If the definition cannot be parsed or built
\r
376 ProtoBuf.loadJson = function(json, builder, filename) {
\r
377 if (typeof builder === 'string' || (builder && typeof builder["file"] === 'string' && typeof builder["root"] === 'string'))
\r
378 filename = builder,
\r
380 if (!builder || typeof builder !== 'object')
\r
381 builder = ProtoBuf.newBuilder();
\r
382 if (typeof json === 'string')
\r
383 json = JSON.parse(json);
\r
384 builder["import"](json, filename);
\r
385 builder.resolveAll();
\r
390 * Loads a .json file and returns the Builder.
\r
391 * @param {string|!{root: string, file: string}} filename Path to json file or an object specifying 'file' with
\r
392 * an overridden 'root' path for all imported files.
\r
393 * @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
\r
394 * the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
\r
395 * file will be read synchronously and this function will return the Builder.
\r
396 * @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
\r
397 * @return {?ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
\r
398 * request has failed), else undefined
\r
401 ProtoBuf.loadJsonFile = function(filename, callback, builder) {
\r
402 if (callback && typeof callback === 'object')
\r
403 builder = callback,
\r
405 else if (!callback || typeof callback !== 'function')
\r
408 return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {
\r
409 if (contents === null) {
\r
410 callback(Error("Failed to fetch file"));
\r
414 callback(null, ProtoBuf.loadJson(JSON.parse(contents), builder, filename));
\r
419 var contents = ProtoBuf.Util.fetch(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename);
\r
420 return contents === null ? null : ProtoBuf.loadJson(JSON.parse(contents), builder, filename);
\r