1ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format 2ffe3c632Sopenharmony_ci// Copyright 2008 Google Inc. All rights reserved. 3ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/ 4ffe3c632Sopenharmony_ci// 5ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without 6ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are 7ffe3c632Sopenharmony_ci// met: 8ffe3c632Sopenharmony_ci// 9ffe3c632Sopenharmony_ci// * Redistributions of source code must retain the above copyright 10ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer. 11ffe3c632Sopenharmony_ci// * Redistributions in binary form must reproduce the above 12ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer 13ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the 14ffe3c632Sopenharmony_ci// distribution. 15ffe3c632Sopenharmony_ci// * Neither the name of Google Inc. nor the names of its 16ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from 17ffe3c632Sopenharmony_ci// this software without specific prior written permission. 18ffe3c632Sopenharmony_ci// 19ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30ffe3c632Sopenharmony_ci 31ffe3c632Sopenharmony_ci/** 32ffe3c632Sopenharmony_ci * @fileoverview Utilities to debug JSPB based proto objects. 33ffe3c632Sopenharmony_ci */ 34ffe3c632Sopenharmony_ci 35ffe3c632Sopenharmony_cigoog.provide('jspb.debug'); 36ffe3c632Sopenharmony_ci 37ffe3c632Sopenharmony_cigoog.require('goog.array'); 38ffe3c632Sopenharmony_cigoog.require('goog.asserts'); 39ffe3c632Sopenharmony_cigoog.require('goog.object'); 40ffe3c632Sopenharmony_cigoog.require('jspb.Map'); 41ffe3c632Sopenharmony_cigoog.require('jspb.Message'); 42ffe3c632Sopenharmony_ci 43ffe3c632Sopenharmony_ci 44ffe3c632Sopenharmony_ci/** 45ffe3c632Sopenharmony_ci * Turns a proto into a human readable object that can i.e. be written to the 46ffe3c632Sopenharmony_ci * console: `console.log(jspb.debug.dump(myProto))`. 47ffe3c632Sopenharmony_ci * This function makes a best effort and may not work in all cases. It will not 48ffe3c632Sopenharmony_ci * work in obfuscated and or optimized code. 49ffe3c632Sopenharmony_ci * Use this in environments where {@see jspb.Message.prototype.toObject} is 50ffe3c632Sopenharmony_ci * not available for code size reasons. 51ffe3c632Sopenharmony_ci * @param {jspb.Message} message A jspb.Message. 52ffe3c632Sopenharmony_ci * @return {Object} 53ffe3c632Sopenharmony_ci */ 54ffe3c632Sopenharmony_cijspb.debug.dump = function(message) { 55ffe3c632Sopenharmony_ci if (!goog.DEBUG) { 56ffe3c632Sopenharmony_ci return null; 57ffe3c632Sopenharmony_ci } 58ffe3c632Sopenharmony_ci goog.asserts.assert(message instanceof jspb.Message, 59ffe3c632Sopenharmony_ci 'jspb.Message instance expected'); 60ffe3c632Sopenharmony_ci /** @type {Object} */ 61ffe3c632Sopenharmony_ci var object = message; 62ffe3c632Sopenharmony_ci goog.asserts.assert(object['getExtension'], 63ffe3c632Sopenharmony_ci 'Only unobfuscated and unoptimized compilation modes supported.'); 64ffe3c632Sopenharmony_ci return /** @type {Object} */ (jspb.debug.dump_(message)); 65ffe3c632Sopenharmony_ci}; 66ffe3c632Sopenharmony_ci 67ffe3c632Sopenharmony_ci 68ffe3c632Sopenharmony_ci/** 69ffe3c632Sopenharmony_ci * Recursively introspects a message and the values its getters return to 70ffe3c632Sopenharmony_ci * make a best effort in creating a human readable representation of the 71ffe3c632Sopenharmony_ci * message. 72ffe3c632Sopenharmony_ci * @param {?} thing A jspb.Message, Array or primitive type to dump. 73ffe3c632Sopenharmony_ci * @return {*} 74ffe3c632Sopenharmony_ci * @private 75ffe3c632Sopenharmony_ci */ 76ffe3c632Sopenharmony_cijspb.debug.dump_ = function(thing) { 77ffe3c632Sopenharmony_ci var type = goog.typeOf(thing); 78ffe3c632Sopenharmony_ci var message = thing; // Copy because we don't want type inference on thing. 79ffe3c632Sopenharmony_ci if (type == 'number' || type == 'string' || type == 'boolean' || 80ffe3c632Sopenharmony_ci type == 'null' || type == 'undefined') { 81ffe3c632Sopenharmony_ci return thing; 82ffe3c632Sopenharmony_ci } 83ffe3c632Sopenharmony_ci if (typeof Uint8Array !== 'undefined') { 84ffe3c632Sopenharmony_ci // Will fail on IE9, where Uint8Array doesn't exist. 85ffe3c632Sopenharmony_ci if (message instanceof Uint8Array) { 86ffe3c632Sopenharmony_ci return thing; 87ffe3c632Sopenharmony_ci } 88ffe3c632Sopenharmony_ci } 89ffe3c632Sopenharmony_ci 90ffe3c632Sopenharmony_ci if (type == 'array') { 91ffe3c632Sopenharmony_ci goog.asserts.assertArray(thing); 92ffe3c632Sopenharmony_ci return goog.array.map(thing, jspb.debug.dump_); 93ffe3c632Sopenharmony_ci } 94ffe3c632Sopenharmony_ci 95ffe3c632Sopenharmony_ci if (message instanceof jspb.Map) { 96ffe3c632Sopenharmony_ci var mapObject = {}; 97ffe3c632Sopenharmony_ci var entries = message.entries(); 98ffe3c632Sopenharmony_ci for (var entry = entries.next(); !entry.done; entry = entries.next()) { 99ffe3c632Sopenharmony_ci mapObject[entry.value[0]] = jspb.debug.dump_(entry.value[1]); 100ffe3c632Sopenharmony_ci } 101ffe3c632Sopenharmony_ci return mapObject; 102ffe3c632Sopenharmony_ci } 103ffe3c632Sopenharmony_ci 104ffe3c632Sopenharmony_ci goog.asserts.assert(message instanceof jspb.Message, 105ffe3c632Sopenharmony_ci 'Only messages expected: ' + thing); 106ffe3c632Sopenharmony_ci var ctor = message.constructor; 107ffe3c632Sopenharmony_ci var messageName = ctor.name || ctor.displayName; 108ffe3c632Sopenharmony_ci var object = { 109ffe3c632Sopenharmony_ci '$name': messageName 110ffe3c632Sopenharmony_ci }; 111ffe3c632Sopenharmony_ci for (var name in ctor.prototype) { 112ffe3c632Sopenharmony_ci var match = /^get([A-Z]\w*)/.exec(name); 113ffe3c632Sopenharmony_ci if (match && name != 'getExtension' && 114ffe3c632Sopenharmony_ci name != 'getJsPbMessageId') { 115ffe3c632Sopenharmony_ci var has = 'has' + match[1]; 116ffe3c632Sopenharmony_ci if (!thing[has] || thing[has]()) { 117ffe3c632Sopenharmony_ci var val = thing[name](); 118ffe3c632Sopenharmony_ci object[jspb.debug.formatFieldName_(match[1])] = jspb.debug.dump_(val); 119ffe3c632Sopenharmony_ci } 120ffe3c632Sopenharmony_ci } 121ffe3c632Sopenharmony_ci } 122ffe3c632Sopenharmony_ci if (COMPILED && thing['extensionObject_']) { 123ffe3c632Sopenharmony_ci object['$extensions'] = 'Recursive dumping of extensions not supported ' + 124ffe3c632Sopenharmony_ci 'in compiled code. Switch to uncompiled or dump extension object ' + 125ffe3c632Sopenharmony_ci 'directly'; 126ffe3c632Sopenharmony_ci return object; 127ffe3c632Sopenharmony_ci } 128ffe3c632Sopenharmony_ci var extensionsObject; 129ffe3c632Sopenharmony_ci for (var id in ctor['extensions']) { 130ffe3c632Sopenharmony_ci if (/^\d+$/.test(id)) { 131ffe3c632Sopenharmony_ci var ext = ctor['extensions'][id]; 132ffe3c632Sopenharmony_ci var extVal = thing.getExtension(ext); 133ffe3c632Sopenharmony_ci var fieldName = goog.object.getKeys(ext.fieldName)[0]; 134ffe3c632Sopenharmony_ci if (extVal != null) { 135ffe3c632Sopenharmony_ci if (!extensionsObject) { 136ffe3c632Sopenharmony_ci extensionsObject = object['$extensions'] = {}; 137ffe3c632Sopenharmony_ci } 138ffe3c632Sopenharmony_ci extensionsObject[jspb.debug.formatFieldName_(fieldName)] = 139ffe3c632Sopenharmony_ci jspb.debug.dump_(extVal); 140ffe3c632Sopenharmony_ci } 141ffe3c632Sopenharmony_ci } 142ffe3c632Sopenharmony_ci } 143ffe3c632Sopenharmony_ci return object; 144ffe3c632Sopenharmony_ci}; 145ffe3c632Sopenharmony_ci 146ffe3c632Sopenharmony_ci 147ffe3c632Sopenharmony_ci/** 148ffe3c632Sopenharmony_ci * Formats a field name for output as camelCase. 149ffe3c632Sopenharmony_ci * 150ffe3c632Sopenharmony_ci * @param {string} name Name of the field. 151ffe3c632Sopenharmony_ci * @return {string} 152ffe3c632Sopenharmony_ci * @private 153ffe3c632Sopenharmony_ci */ 154ffe3c632Sopenharmony_cijspb.debug.formatFieldName_ = function(name) { 155ffe3c632Sopenharmony_ci // Name may be in TitleCase. 156ffe3c632Sopenharmony_ci return name.replace(/^[A-Z]/, function(c) { 157ffe3c632Sopenharmony_ci return c.toLowerCase(); 158ffe3c632Sopenharmony_ci }); 159ffe3c632Sopenharmony_ci}; 160