convert.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. "use strict";
  2. /*!
  3. * Copyright 2019 Google Inc. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.timestampFromJson = timestampFromJson;
  19. exports.detectValueType = detectValueType;
  20. exports.detectGoogleProtobufValueType = detectGoogleProtobufValueType;
  21. exports.valueFromJson = valueFromJson;
  22. exports.fieldsFromJson = fieldsFromJson;
  23. const validate_1 = require("./validate");
  24. const map_type_1 = require("./map-type");
  25. /*!
  26. * @module firestore/convert
  27. * @private
  28. * @internal
  29. *
  30. * This module contains utility functions to convert
  31. * `firestore.v1.Documents` from Proto3 JSON to their equivalent
  32. * representation in Protobuf JS. Protobuf JS is the only encoding supported by
  33. * this client, and dependencies that use Proto3 JSON (such as the Google Cloud
  34. * Functions SDK) are supported through this conversion and its usage in
  35. * {@see Firestore#snapshot_}.
  36. */
  37. /**
  38. * Converts an ISO 8601 or google.protobuf.Timestamp proto into Protobuf JS.
  39. *
  40. * @private
  41. * @internal
  42. * @param timestampValue The value to convert.
  43. * @param argumentName The argument name to use in the error message if the
  44. * conversion fails. If omitted, 'timestampValue' is used.
  45. * @return The value as expected by Protobuf JS or undefined if no input was
  46. * provided.
  47. */
  48. function timestampFromJson(timestampValue, argumentName) {
  49. let timestampProto = {};
  50. if (typeof timestampValue === 'string') {
  51. const date = new Date(timestampValue);
  52. const seconds = Math.floor(date.getTime() / 1000);
  53. let nanos = 0;
  54. if (timestampValue.length > 20) {
  55. const nanoString = timestampValue.substring(20, timestampValue.length - 1);
  56. const trailingZeroes = 9 - nanoString.length;
  57. nanos = Number(nanoString) * Math.pow(10, trailingZeroes);
  58. }
  59. if (isNaN(seconds) || isNaN(nanos)) {
  60. argumentName = argumentName || 'timestampValue';
  61. throw new Error(`Specify a valid ISO 8601 timestamp for "${argumentName}".`);
  62. }
  63. timestampProto = {
  64. seconds: seconds || undefined,
  65. nanos: nanos || undefined,
  66. };
  67. }
  68. else if (timestampValue !== undefined) {
  69. (0, validate_1.validateObject)('timestampValue', timestampValue);
  70. timestampProto = {
  71. seconds: timestampValue.seconds || undefined,
  72. nanos: timestampValue.nanos || undefined,
  73. };
  74. }
  75. return timestampProto;
  76. }
  77. /**
  78. * Converts a Proto3 JSON 'bytesValue' field into Protobuf JS.
  79. *
  80. * @private
  81. * @internal
  82. * @param bytesValue The value to convert.
  83. * @return The value as expected by Protobuf JS.
  84. */
  85. function bytesFromJson(bytesValue) {
  86. if (typeof bytesValue === 'string') {
  87. return Buffer.from(bytesValue, 'base64');
  88. }
  89. else {
  90. return bytesValue;
  91. }
  92. }
  93. /**
  94. * Detects 'valueType' from a Proto3 JSON `firestore.v1.Value` proto.
  95. *
  96. * @private
  97. * @internal
  98. * @param proto The `firestore.v1.Value` proto.
  99. * @return The string value for 'valueType'.
  100. */
  101. function detectValueType(proto) {
  102. var _a;
  103. let valueType;
  104. if (proto.valueType) {
  105. valueType = proto.valueType;
  106. }
  107. else {
  108. const detectedValues = [];
  109. if (proto.stringValue !== undefined) {
  110. detectedValues.push('stringValue');
  111. }
  112. if (proto.booleanValue !== undefined) {
  113. detectedValues.push('booleanValue');
  114. }
  115. if (proto.integerValue !== undefined) {
  116. detectedValues.push('integerValue');
  117. }
  118. if (proto.doubleValue !== undefined) {
  119. detectedValues.push('doubleValue');
  120. }
  121. if (proto.timestampValue !== undefined) {
  122. detectedValues.push('timestampValue');
  123. }
  124. if (proto.referenceValue !== undefined) {
  125. detectedValues.push('referenceValue');
  126. }
  127. if (proto.arrayValue !== undefined) {
  128. detectedValues.push('arrayValue');
  129. }
  130. if (proto.nullValue !== undefined) {
  131. detectedValues.push('nullValue');
  132. }
  133. if (proto.mapValue !== undefined) {
  134. detectedValues.push('mapValue');
  135. }
  136. if (proto.geoPointValue !== undefined) {
  137. detectedValues.push('geoPointValue');
  138. }
  139. if (proto.bytesValue !== undefined) {
  140. detectedValues.push('bytesValue');
  141. }
  142. if (detectedValues.length !== 1) {
  143. throw new Error(`Unable to infer type value from '${JSON.stringify(proto)}'.`);
  144. }
  145. valueType = detectedValues[0];
  146. }
  147. // Special handling of mapValues used to represent other data types
  148. if (valueType === 'mapValue') {
  149. const fields = (_a = proto.mapValue) === null || _a === void 0 ? void 0 : _a.fields;
  150. if (fields) {
  151. const props = Object.keys(fields);
  152. if (props.indexOf(map_type_1.RESERVED_MAP_KEY) !== -1 &&
  153. detectValueType(fields[map_type_1.RESERVED_MAP_KEY]) === 'stringValue' &&
  154. fields[map_type_1.RESERVED_MAP_KEY].stringValue === map_type_1.RESERVED_MAP_KEY_VECTOR_VALUE) {
  155. valueType = 'vectorValue';
  156. }
  157. }
  158. }
  159. return valueType;
  160. }
  161. /**
  162. * Detects the value kind from a Proto3 JSON `google.protobuf.Value` proto.
  163. *
  164. * @private
  165. * @internal
  166. * @param proto The `firestore.v1.Value` proto.
  167. * @return The string value for 'valueType'.
  168. */
  169. function detectGoogleProtobufValueType(proto) {
  170. const detectedValues = [];
  171. if (proto.nullValue !== undefined) {
  172. detectedValues.push('nullValue');
  173. }
  174. if (proto.numberValue !== undefined) {
  175. detectedValues.push('numberValue');
  176. }
  177. if (proto.stringValue !== undefined) {
  178. detectedValues.push('stringValue');
  179. }
  180. if (proto.boolValue !== undefined) {
  181. detectedValues.push('boolValue');
  182. }
  183. if (proto.structValue !== undefined) {
  184. detectedValues.push('structValue');
  185. }
  186. if (proto.listValue !== undefined) {
  187. detectedValues.push('listValue');
  188. }
  189. if (detectedValues.length !== 1) {
  190. throw new Error(`Unable to infer type value from '${JSON.stringify(proto)}'.`);
  191. }
  192. return detectedValues[0];
  193. }
  194. /**
  195. * Converts a `firestore.v1.Value` in Proto3 JSON encoding into the
  196. * Protobuf JS format expected by this client.
  197. *
  198. * @private
  199. * @internal
  200. * @param fieldValue The `firestore.v1.Value` in Proto3 JSON format.
  201. * @return The `firestore.v1.Value` in Protobuf JS format.
  202. */
  203. function valueFromJson(fieldValue) {
  204. const valueType = detectValueType(fieldValue);
  205. switch (valueType) {
  206. case 'timestampValue':
  207. return {
  208. timestampValue: timestampFromJson(fieldValue.timestampValue),
  209. };
  210. case 'bytesValue':
  211. return {
  212. bytesValue: bytesFromJson(fieldValue.bytesValue),
  213. };
  214. case 'doubleValue':
  215. return {
  216. doubleValue: Number(fieldValue.doubleValue),
  217. };
  218. case 'arrayValue': {
  219. const arrayValue = [];
  220. if (Array.isArray(fieldValue.arrayValue.values)) {
  221. for (const value of fieldValue.arrayValue.values) {
  222. arrayValue.push(valueFromJson(value));
  223. }
  224. }
  225. return {
  226. arrayValue: {
  227. values: arrayValue,
  228. },
  229. };
  230. }
  231. case 'mapValue':
  232. case 'vectorValue': {
  233. const mapValue = {};
  234. const fields = fieldValue.mapValue.fields;
  235. if (fields) {
  236. for (const prop of Object.keys(fields)) {
  237. mapValue[prop] = valueFromJson(fieldValue.mapValue.fields[prop]);
  238. }
  239. }
  240. return {
  241. mapValue: {
  242. fields: mapValue,
  243. },
  244. };
  245. }
  246. default:
  247. return fieldValue;
  248. }
  249. }
  250. /**
  251. * Converts a map of IValues in Proto3 JSON encoding into the Protobuf JS format
  252. * expected by this client. This conversion creates a copy of the underlying
  253. * fields.
  254. *
  255. * @private
  256. * @internal
  257. * @param document An object with IValues in Proto3 JSON format.
  258. * @return The object in Protobuf JS format.
  259. */
  260. function fieldsFromJson(document) {
  261. const result = {};
  262. for (const prop of Object.keys(document)) {
  263. result[prop] = valueFromJson(document[prop]);
  264. }
  265. return result;
  266. }
  267. //# sourceMappingURL=convert.js.map