Imported Upstream version 3.8.0
[platform/upstream/protobuf.git] / src / google / protobuf / compiler / js / well_known_types_embed.cc
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 #include <google/protobuf/compiler/js/well_known_types_embed.h>
32
33 struct FileToc well_known_types_js[] = {
34     {"any.js",
35      "/* This code will be inserted into generated code for\n"
36      " * google/protobuf/any.proto. */\n"
37      "\n"
38      "/**\n"
39      " * Returns the type name contained in this instance, if any.\n"
40      " * @return {string|undefined}\n"
41      " */\n"
42      "proto.google.protobuf.Any.prototype.getTypeName = function() {\n"
43      "  return this.getTypeUrl().split('/').pop();\n"
44      "};\n"
45      "\n"
46      "\n"
47      "/**\n"
48      " * Packs the given message instance into this Any.\n"
49      " * For binary format usage only.\n"
50      " * @param {!Uint8Array} serialized The serialized data to pack.\n"
51      " * @param {string} name The type name of this message object.\n"
52      " * @param {string=} opt_typeUrlPrefix the type URL prefix.\n"
53      " */\n"
54      "proto.google.protobuf.Any.prototype.pack = function(serialized, name,\n"
55      "                                                    opt_typeUrlPrefix) "
56      "{\n"
57      "  if (!opt_typeUrlPrefix) {\n"
58      "    opt_typeUrlPrefix = 'type.googleapis.com/';\n"
59      "  }\n"
60      "\n"
61      "  if (opt_typeUrlPrefix.substr(-1) != '/') {\n"
62      "    this.setTypeUrl(opt_typeUrlPrefix + '/' + name);\n"
63      "  } else {\n"
64      "    this.setTypeUrl(opt_typeUrlPrefix + name);\n"
65      "  }\n"
66      "\n"
67      "  this.setValue(serialized);\n"
68      "};\n"
69      "\n"
70      "\n"
71      "/**\n"
72      " * @template T\n"
73      " * Unpacks this Any into the given message object.\n"
74      " * @param {function(Uint8Array):T} deserialize Function that will "
75      "deserialize\n"
76      " *     the binary data properly.\n"
77      " * @param {string} name The expected type name of this message object.\n"
78      " * @return {?T} If the name matched the expected name, returns the "
79      "deserialized\n"
80      " *     object, otherwise returns null.\n"
81      " */\n"
82      "proto.google.protobuf.Any.prototype.unpack = function(deserialize, name) "
83      "{\n"
84      "  if (this.getTypeName() == name) {\n"
85      "    return deserialize(this.getValue_asU8());\n"
86      "  } else {\n"
87      "    return null;\n"
88      "  }\n"
89      "};\n"
90     },
91     {"timestamp.js",
92      "/* This code will be inserted into generated code for\n"
93      " * google/protobuf/timestamp.proto. */\n"
94      "\n"
95      "/**\n"
96      " * Returns a JavaScript 'Date' object corresponding to this Timestamp.\n"
97      " * @return {!Date}\n"
98      " */\n"
99      "proto.google.protobuf.Timestamp.prototype.toDate = function() {\n"
100      "  var seconds = this.getSeconds();\n"
101      "  var nanos = this.getNanos();\n"
102      "\n"
103      "  return new Date((seconds * 1000) + (nanos / 1000000));\n"
104      "};\n"
105      "\n"
106      "\n"
107      "/**\n"
108      " * Sets the value of this Timestamp object to be the given Date.\n"
109      " * @param {!Date} value The value to set.\n"
110      " */\n"
111      "proto.google.protobuf.Timestamp.prototype.fromDate = function(value) {\n"
112      "  this.setSeconds(Math.floor(value.getTime() / 1000));\n"
113      "  this.setNanos(value.getMilliseconds() * 1000000);\n"
114      "};\n"
115      "\n"
116      "\n"
117      "/**\n"
118      " * Factory method that returns a Timestamp object with value equal to\n"
119      " * the given Date.\n"
120      " * @param {!Date} value The value to set.\n"
121      " * @return {!proto.google.protobuf.Timestamp}\n"
122      " */\n"
123      "proto.google.protobuf.Timestamp.fromDate = function(value) {\n"
124      "  var timestamp = new proto.google.protobuf.Timestamp();\n"
125      "  timestamp.fromDate(value);\n"
126      "  return timestamp;\n"
127      "};\n"},
128     {"struct.js",
129      "/* This code will be inserted into generated code for\n"
130      " * google/protobuf/struct.proto. */\n"
131      "\n"
132      "/**\n"
133      " * Typedef representing plain JavaScript values that can go into a\n"
134      " *     Struct.\n"
135      " * @typedef {null|number|string|boolean|Array|Object}\n"
136      " */\n"
137      "proto.google.protobuf.JavaScriptValue;\n"
138      "\n"
139      "\n"
140      "/**\n"
141      " * Converts this Value object to a plain JavaScript value.\n"
142      " * @return {?proto.google.protobuf.JavaScriptValue} a plain JavaScript\n"
143      " *     value representing this Struct.\n"
144      " */\n"
145      "proto.google.protobuf.Value.prototype.toJavaScript = function() {\n"
146      "  var kindCase = proto.google.protobuf.Value.KindCase;\n"
147      "  switch (this.getKindCase()) {\n"
148      "    case kindCase.NULL_VALUE:\n"
149      "      return null;\n"
150      "    case kindCase.NUMBER_VALUE:\n"
151      "      return this.getNumberValue();\n"
152      "    case kindCase.STRING_VALUE:\n"
153      "      return this.getStringValue();\n"
154      "    case kindCase.BOOL_VALUE:\n"
155      "      return this.getBoolValue();\n"
156      "    case kindCase.STRUCT_VALUE:\n"
157      "      return this.getStructValue().toJavaScript();\n"
158      "    case kindCase.LIST_VALUE:\n"
159      "      return this.getListValue().toJavaScript();\n"
160      "    default:\n"
161      "      throw new Error('Unexpected struct type');\n"
162      "  }\n"
163      "};\n"
164      "\n"
165      "\n"
166      "/**\n"
167      " * Converts this JavaScript value to a new Value proto.\n"
168      " * @param {!proto.google.protobuf.JavaScriptValue} value The value to\n"
169      " *     convert.\n"
170      " * @return {!proto.google.protobuf.Value} The newly constructed value.\n"
171      " */\n"
172      "proto.google.protobuf.Value.fromJavaScript = function(value) {\n"
173      "  var ret = new proto.google.protobuf.Value();\n"
174      "  switch (goog.typeOf(value)) {\n"
175      "    case 'string':\n"
176      "      ret.setStringValue(/** @type {string} */ (value));\n"
177      "      break;\n"
178      "    case 'number':\n"
179      "      ret.setNumberValue(/** @type {number} */ (value));\n"
180      "      break;\n"
181      "    case 'boolean':\n"
182      "      ret.setBoolValue(/** @type {boolean} */ (value));\n"
183      "      break;\n"
184      "    case 'null':\n"
185      "      ret.setNullValue(proto.google.protobuf.NullValue.NULL_VALUE);\n"
186      "      break;\n"
187      "    case 'array':\n"
188      "      ret.setListValue(proto.google.protobuf.ListValue.fromJavaScript(\n"
189      "          /** @type{!Array} */ (value)));\n"
190      "      break;\n"
191      "    case 'object':\n"
192      "      ret.setStructValue(proto.google.protobuf.Struct.fromJavaScript(\n"
193      "          /** @type{!Object} */ (value)));\n"
194      "      break;\n"
195      "    default:\n"
196      "      throw new Error('Unexpected struct type.');\n"
197      "  }\n"
198      "\n"
199      "  return ret;\n"
200      "};\n"
201      "\n"
202      "\n"
203      "/**\n"
204      " * Converts this ListValue object to a plain JavaScript array.\n"
205      " * @return {!Array} a plain JavaScript array representing this List.\n"
206      " */\n"
207      "proto.google.protobuf.ListValue.prototype.toJavaScript = function() {\n"
208      "  var ret = [];\n"
209      "  var values = this.getValuesList();\n"
210      "\n"
211      "  for (var i = 0; i < values.length; i++) {\n"
212      "    ret[i] = values[i].toJavaScript();\n"
213      "  }\n"
214      "\n"
215      "  return ret;\n"
216      "};\n"
217      "\n"
218      "\n"
219      "/**\n"
220      " * Constructs a ListValue protobuf from this plain JavaScript array.\n"
221      " * @param {!Array} array a plain JavaScript array\n"
222      " * @return {proto.google.protobuf.ListValue} a new ListValue object\n"
223      " */\n"
224      "proto.google.protobuf.ListValue.fromJavaScript = function(array) {\n"
225      "  var ret = new proto.google.protobuf.ListValue();\n"
226      "\n"
227      "  for (var i = 0; i < array.length; i++) {\n"
228      "    "
229      "ret.addValues(proto.google.protobuf.Value.fromJavaScript(array[i]));\n"
230      "  }\n"
231      "\n"
232      "  return ret;\n"
233      "};\n"
234      "\n"
235      "\n"
236      "/**\n"
237      " * Converts this Struct object to a plain JavaScript object.\n"
238      " * @return {!Object<string, !proto.google.protobuf.JavaScriptValue>} a "
239      "plain\n"
240      " *     JavaScript object representing this Struct.\n"
241      " */\n"
242      "proto.google.protobuf.Struct.prototype.toJavaScript = function() {\n"
243      "  var ret = {};\n"
244      "\n"
245      "  this.getFieldsMap().forEach(function(value, key) {\n"
246      "    ret[key] = value.toJavaScript();\n"
247      "  });\n"
248      "\n"
249      "  return ret;\n"
250      "};\n"
251      "\n"
252      "\n"
253      "/**\n"
254      " * Constructs a Struct protobuf from this plain JavaScript object.\n"
255      " * @param {!Object} obj a plain JavaScript object\n"
256      " * @return {proto.google.protobuf.Struct} a new Struct object\n"
257      " */\n"
258      "proto.google.protobuf.Struct.fromJavaScript = function(obj) {\n"
259      "  var ret = new proto.google.protobuf.Struct();\n"
260      "  var map = ret.getFieldsMap();\n"
261      "\n"
262      "  for (var property in obj) {\n"
263      "    var val = obj[property];\n"
264      "    map.set(property, proto.google.protobuf.Value.fromJavaScript(val));\n"
265      "  }\n"
266      "\n"
267      "  return ret;\n"
268      "};\n"},
269     {NULL, NULL}  // Terminate the list.
270 };