debug.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. * @fileoverview Utilities to debug JSPB based proto objects.
  32. */
  33. goog.provide('jspb.debug');
  34. goog.require('goog.array');
  35. goog.require('goog.asserts');
  36. goog.require('goog.object');
  37. goog.require('jspb.Map');
  38. goog.require('jspb.Message');
  39. /**
  40. * Turns a proto into a human readable object that can i.e. be written to the
  41. * console: `console.log(jspb.debug.dump(myProto))`.
  42. * This function makes a best effort and may not work in all cases. It will not
  43. * work in obfuscated and or optimized code.
  44. * Use this in environments where {@see jspb.Message.prototype.toObject} is
  45. * not available for code size reasons.
  46. * @param {jspb.Message} message A jspb.Message.
  47. * @return {Object}
  48. */
  49. jspb.debug.dump = function(message) {
  50. if (!goog.DEBUG) {
  51. return null;
  52. }
  53. goog.asserts.assert(message instanceof jspb.Message,
  54. 'jspb.Message instance expected');
  55. /** @type {Object} */
  56. var object = message;
  57. goog.asserts.assert(object['getExtension'],
  58. 'Only unobfuscated and unoptimized compilation modes supported.');
  59. return /** @type {Object} */ (jspb.debug.dump_(message));
  60. };
  61. /**
  62. * Recursively introspects a message and the values its getters return to
  63. * make a best effort in creating a human readable representation of the
  64. * message.
  65. * @param {?} thing A jspb.Message, Array or primitive type to dump.
  66. * @return {*}
  67. * @private
  68. */
  69. jspb.debug.dump_ = function(thing) {
  70. var type = goog.typeOf(thing);
  71. var message = thing; // Copy because we don't want type inference on thing.
  72. if (type == 'number' || type == 'string' || type == 'boolean' ||
  73. type == 'null' || type == 'undefined') {
  74. return thing;
  75. }
  76. if (typeof Uint8Array !== 'undefined') {
  77. // Will fail on IE9, where Uint8Array doesn't exist.
  78. if (message instanceof Uint8Array) {
  79. return thing;
  80. }
  81. }
  82. if (type == 'array') {
  83. goog.asserts.assertArray(thing);
  84. return goog.array.map(thing, jspb.debug.dump_);
  85. }
  86. if (message instanceof jspb.Map) {
  87. var mapObject = {};
  88. var entries = message.entries();
  89. for (var entry = entries.next(); !entry.done; entry = entries.next()) {
  90. mapObject[entry.value[0]] = jspb.debug.dump_(entry.value[1]);
  91. }
  92. return mapObject;
  93. }
  94. goog.asserts.assert(message instanceof jspb.Message,
  95. 'Only messages expected: ' + thing);
  96. var ctor = message.constructor;
  97. var messageName = ctor.name || ctor.displayName;
  98. var object = {
  99. '$name': messageName
  100. };
  101. for (var name in ctor.prototype) {
  102. var match = /^get([A-Z]\w*)/.exec(name);
  103. if (match && name != 'getExtension' &&
  104. name != 'getJsPbMessageId') {
  105. var has = 'has' + match[1];
  106. if (!thing[has] || thing[has]()) {
  107. var val = thing[name]();
  108. object[jspb.debug.formatFieldName_(match[1])] = jspb.debug.dump_(val);
  109. }
  110. }
  111. }
  112. if (COMPILED && thing['extensionObject_']) {
  113. object['$extensions'] = 'Recursive dumping of extensions not supported ' +
  114. 'in compiled code. Switch to uncompiled or dump extension object ' +
  115. 'directly';
  116. return object;
  117. }
  118. var extensionsObject;
  119. for (var id in ctor['extensions']) {
  120. if (/^\d+$/.test(id)) {
  121. var ext = ctor['extensions'][id];
  122. var extVal = thing.getExtension(ext);
  123. var fieldName = goog.object.getKeys(ext.fieldName)[0];
  124. if (extVal != null) {
  125. if (!extensionsObject) {
  126. extensionsObject = object['$extensions'] = {};
  127. }
  128. extensionsObject[jspb.debug.formatFieldName_(fieldName)] =
  129. jspb.debug.dump_(extVal);
  130. }
  131. }
  132. }
  133. return object;
  134. };
  135. /**
  136. * Formats a field name for output as camelCase.
  137. *
  138. * @param {string} name Name of the field.
  139. * @return {string}
  140. * @private
  141. */
  142. jspb.debug.formatFieldName_ = function(name) {
  143. // Name may be in TitleCase.
  144. return name.replace(/^[A-Z]/, function(c) {
  145. return c.toLowerCase();
  146. });
  147. };