OLD | NEW |
(Empty) | |
| 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 /* This code will be inserted into generated code for |
| 32 * google/protobuf/struct.proto. */ |
| 33 |
| 34 /** |
| 35 * Typedef representing plain JavaScript values that can go into a |
| 36 * Struct. |
| 37 * @typedef {null|number|string|boolean|Array|Object} |
| 38 */ |
| 39 proto.google.protobuf.JavaScriptValue; |
| 40 |
| 41 |
| 42 /** |
| 43 * Converts this Value object to a plain JavaScript value. |
| 44 * @return {?proto.google.protobuf.JavaScriptValue} a plain JavaScript |
| 45 * value representing this Struct. |
| 46 */ |
| 47 proto.google.protobuf.Value.prototype.toJavaScript = function() { |
| 48 var kindCase = proto.google.protobuf.Value.KindCase; |
| 49 switch (this.getKindCase()) { |
| 50 case kindCase.NULL_VALUE: |
| 51 return null; |
| 52 case kindCase.NUMBER_VALUE: |
| 53 return this.getNumberValue(); |
| 54 case kindCase.STRING_VALUE: |
| 55 return this.getStringValue(); |
| 56 case kindCase.BOOL_VALUE: |
| 57 return this.getBoolValue(); |
| 58 case kindCase.STRUCT_VALUE: |
| 59 return this.getStructValue().toJavaScript(); |
| 60 case kindCase.LIST_VALUE: |
| 61 return this.getListValue().toJavaScript(); |
| 62 default: |
| 63 throw new Error('Unexpected struct type'); |
| 64 } |
| 65 }; |
| 66 |
| 67 |
| 68 /** |
| 69 * Converts this JavaScript value to a new Value proto. |
| 70 * @param {!proto.google.protobuf.JavaScriptValue} value The value to |
| 71 * convert. |
| 72 * @return {!proto.google.protobuf.Value} The newly constructed value. |
| 73 */ |
| 74 proto.google.protobuf.Value.fromJavaScript = function(value) { |
| 75 var ret = new proto.google.protobuf.Value(); |
| 76 switch (goog.typeOf(value)) { |
| 77 case 'string': |
| 78 ret.setStringValue(/** @type {string} */ (value)); |
| 79 break; |
| 80 case 'number': |
| 81 ret.setNumberValue(/** @type {number} */ (value)); |
| 82 break; |
| 83 case 'boolean': |
| 84 ret.setBoolValue(/** @type {boolean} */ (value)); |
| 85 break; |
| 86 case 'null': |
| 87 ret.setNullValue(proto.google.protobuf.NullValue.NULL_VALUE); |
| 88 break; |
| 89 case 'array': |
| 90 ret.setListValue(proto.google.protobuf.ListValue.fromJavaScript( |
| 91 /** @type{!Array} */ (value))); |
| 92 break; |
| 93 case 'object': |
| 94 ret.setStructValue(proto.google.protobuf.Struct.fromJavaScript( |
| 95 /** @type{!Object} */ (value))); |
| 96 break; |
| 97 default: |
| 98 throw new Error('Unexpected struct type.'); |
| 99 } |
| 100 |
| 101 return ret; |
| 102 }; |
| 103 |
| 104 |
| 105 /** |
| 106 * Converts this ListValue object to a plain JavaScript array. |
| 107 * @return {!Array} a plain JavaScript array representing this List. |
| 108 */ |
| 109 proto.google.protobuf.ListValue.prototype.toJavaScript = function() { |
| 110 var ret = []; |
| 111 var values = this.getValuesList(); |
| 112 |
| 113 for (var i = 0; i < values.length; i++) { |
| 114 ret[i] = values[i].toJavaScript(); |
| 115 } |
| 116 |
| 117 return ret; |
| 118 }; |
| 119 |
| 120 |
| 121 /** |
| 122 * Constructs a ListValue protobuf from this plain JavaScript array. |
| 123 * @param {!Array} array a plain JavaScript array |
| 124 * @return {proto.google.protobuf.ListValue} a new ListValue object |
| 125 */ |
| 126 proto.google.protobuf.ListValue.fromJavaScript = function(array) { |
| 127 var ret = new proto.google.protobuf.ListValue(); |
| 128 |
| 129 for (var i = 0; i < array.length; i++) { |
| 130 ret.addValues(proto.google.protobuf.Value.fromJavaScript(array[i])); |
| 131 } |
| 132 |
| 133 return ret; |
| 134 }; |
| 135 |
| 136 |
| 137 /** |
| 138 * Converts this Struct object to a plain JavaScript object. |
| 139 * @return {!Object<string, !proto.google.protobuf.JavaScriptValue>} a plain |
| 140 * JavaScript object representing this Struct. |
| 141 */ |
| 142 proto.google.protobuf.Struct.prototype.toJavaScript = function() { |
| 143 var ret = {}; |
| 144 |
| 145 this.getFieldsMap().forEach(function(value, key) { |
| 146 ret[key] = value.toJavaScript(); |
| 147 }); |
| 148 |
| 149 return ret; |
| 150 }; |
| 151 |
| 152 |
| 153 /** |
| 154 * Constructs a Struct protobuf from this plain JavaScript object. |
| 155 * @param {!Object} obj a plain JavaScript object |
| 156 * @return {proto.google.protobuf.Struct} a new Struct object |
| 157 */ |
| 158 proto.google.protobuf.Struct.fromJavaScript = function(obj) { |
| 159 var ret = new proto.google.protobuf.Struct(); |
| 160 var map = ret.getFieldsMap(); |
| 161 |
| 162 for (var property in obj) { |
| 163 var val = obj[property]; |
| 164 map.set(property, proto.google.protobuf.Value.fromJavaScript(val)); |
| 165 } |
| 166 |
| 167 return ret; |
| 168 }; |
OLD | NEW |