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