Source: client/parser/well-known-parsers.js

  1. /*
  2. * Copyright 2023, TeamDev. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Redistribution and use in source and/or binary forms, with or without
  11. * modification, must retain the above copyright notice and the following
  12. * disclaimer.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. "use strict";
  27. import TypeParsers from './type-parsers';
  28. import ObjectParser from './object-parser';
  29. import wrappers from 'google-protobuf/google/protobuf/wrappers_pb';
  30. import struct from 'google-protobuf/google/protobuf/struct_pb';
  31. import {Empty} from 'google-protobuf/google/protobuf/empty_pb';
  32. import {Timestamp} from 'google-protobuf/google/protobuf/timestamp_pb';
  33. import {Duration} from 'google-protobuf/google/protobuf/duration_pb';
  34. import {FieldMask} from 'google-protobuf/google/protobuf/field_mask_pb';
  35. import {Any} from 'google-protobuf/google/protobuf/any_pb';
  36. /**
  37. * The parsers for standard Protobuf types.
  38. *
  39. * <p>For the details about how the parsers should work,
  40. * see {@link https://developers.google.com/protocol-buffers/docs/proto3#json Protobuf documentation}.
  41. */
  42. class BoolValueParser extends ObjectParser {
  43. fromObject(object) {
  44. let boolValue = new wrappers.BoolValue();
  45. boolValue.setValue(object);
  46. return boolValue;
  47. }
  48. }
  49. class BytesValueParser extends ObjectParser {
  50. fromObject(object) {
  51. let bytesValue = new wrappers.BytesValue();
  52. bytesValue.setValue(object);
  53. return bytesValue;
  54. }
  55. }
  56. class DoubleValueParser extends ObjectParser {
  57. fromObject(object) {
  58. let doubleValue = new wrappers.DoubleValue();
  59. doubleValue.setValue(object);
  60. return doubleValue;
  61. }
  62. }
  63. class FloatValueParser extends ObjectParser {
  64. fromObject(object) {
  65. let floatValue = new wrappers.FloatValue();
  66. floatValue.setValue(object);
  67. return floatValue;
  68. }
  69. }
  70. class Int32ValueParser extends ObjectParser {
  71. fromObject(object) {
  72. let int32Value = new wrappers.Int32Value();
  73. int32Value.setValue(object);
  74. return int32Value;
  75. }
  76. }
  77. class Int64ValueParser extends ObjectParser {
  78. fromObject(object) {
  79. let int64Value = new wrappers.Int64Value();
  80. int64Value.setValue(object);
  81. return int64Value;
  82. }
  83. }
  84. class StringValueParser extends ObjectParser {
  85. fromObject(object) {
  86. let stringValue = new wrappers.StringValue();
  87. stringValue.setValue(object);
  88. return stringValue;
  89. }
  90. }
  91. class UInt32ValueParser extends ObjectParser {
  92. fromObject(object) {
  93. let uInt32Value = new wrappers.UInt32Value();
  94. uInt32Value.setValue(object);
  95. return uInt32Value;
  96. }
  97. }
  98. class UInt64ValueParser extends ObjectParser {
  99. fromObject(object) {
  100. let uInt64Value = new wrappers.UInt64Value();
  101. uInt64Value.setValue(object);
  102. return uInt64Value;
  103. }
  104. }
  105. class ListValueParser extends ObjectParser {
  106. fromObject(object) {
  107. let listValue = new struct.ListValue;
  108. object.forEach(
  109. function callback(currentValue, index, array) {
  110. let valueParser = new ValueParser();
  111. array[index] = valueParser.fromObject(currentValue);
  112. }
  113. );
  114. listValue.setValuesList(object);
  115. return listValue;
  116. }
  117. }
  118. class StructParser extends ObjectParser {
  119. fromObject(object) {
  120. let result = new struct.Struct();
  121. let values = new ValueParser();
  122. for (let fieldName in object) {
  123. result.getFieldsMap().set(fieldName, values.fromObject(object[fieldName]));
  124. }
  125. return result;
  126. }
  127. }
  128. class ValueParser extends ObjectParser {
  129. fromObject(object) {
  130. let result = new struct.Value();
  131. if (object === null) {
  132. result.setNullValue(struct.NullValue.NULL_VALUE);
  133. } else if (typeof object === "number") {
  134. result.setNumberValue(object);
  135. } else if (typeof object === "string") {
  136. result.setStringValue(object);
  137. } else if (typeof object === "boolean") {
  138. result.setBoolValue(object);
  139. } else if (Array.isArray(object)) {
  140. let parser = new ListValueParser(object);
  141. let listValue = parser.fromObject(object);
  142. result.setListValue(listValue);
  143. } else {
  144. let parser = new StructParser(object);
  145. let structValue = parser.fromObject(object);
  146. result.setStructValue(structValue);
  147. }
  148. return result;
  149. }
  150. }
  151. class EmptyParser extends ObjectParser {
  152. fromObject(object) {
  153. let emptyValue = new Empty();
  154. return emptyValue;
  155. }
  156. }
  157. class TimestampParser extends ObjectParser {
  158. fromObject(object) {
  159. let date = new Date(object);
  160. let result = new Timestamp();
  161. result.fromDate(date);
  162. return result;
  163. }
  164. }
  165. class DurationParser extends ObjectParser {
  166. fromObject(object) {
  167. object = object.substring(0, object.length - 1);
  168. let values = object.split(".");
  169. let result = new Duration();
  170. if (values.length === 1) {
  171. result.setSeconds(values[0]);
  172. } else if (values.length === 2) {
  173. result.setSeconds(values[0]);
  174. let nanos = values[1];
  175. for (let i = 0; i < 9 - nanos.length; i++) {
  176. nanos += "0";
  177. }
  178. let nanosNumber = parseInt(nanos, 10);
  179. result.setNanos(nanosNumber);
  180. }
  181. return result;
  182. }
  183. }
  184. class FieldMaskParser extends ObjectParser {
  185. fromObject(object) {
  186. let fieldMask = new FieldMask();
  187. fieldMask.setPathsList(object.split(","));
  188. return fieldMask;
  189. }
  190. }
  191. class AnyParser extends ObjectParser {
  192. fromObject(object) {
  193. const typeUrl = object["@type"];
  194. const isWellKnown = wellKnownParsers.has(typeUrl);
  195. const packedValue = isWellKnown
  196. ? object["value"]
  197. : object;
  198. const parser = TypeParsers.parserFor(typeUrl);
  199. const messageValue = parser.fromObject(packedValue);
  200. const bytes = messageValue.serializeBinary();
  201. const anyMsg = new Any();
  202. anyMsg.setTypeUrl(typeUrl);
  203. anyMsg.setValue(bytes);
  204. return anyMsg;
  205. }
  206. }
  207. /**
  208. * The map of parsers for standard Protobuf types.
  209. *
  210. * @type {Map<string, ObjectParser>}
  211. */
  212. export const wellKnownParsers = new Map([
  213. ['type.googleapis.com/google.protobuf.BoolValue', new BoolValueParser()],
  214. ['type.googleapis.com/google.protobuf.BytesValue', new BytesValueParser()],
  215. ['type.googleapis.com/google.protobuf.DoubleValue', new DoubleValueParser()],
  216. ['type.googleapis.com/google.protobuf.FloatValue', new FloatValueParser()],
  217. ['type.googleapis.com/google.protobuf.StringValue', new StringValueParser()],
  218. ['type.googleapis.com/google.protobuf.Int32Value', new Int32ValueParser()],
  219. ['type.googleapis.com/google.protobuf.Int64Value', new Int64ValueParser()],
  220. ['type.googleapis.com/google.protobuf.UInt32Value', new UInt32ValueParser()],
  221. ['type.googleapis.com/google.protobuf.UInt64Value', new UInt64ValueParser()],
  222. ['type.googleapis.com/google.protobuf.Struct', new StructParser()],
  223. ['type.googleapis.com/google.protobuf.Value', new ValueParser()],
  224. ['type.googleapis.com/google.protobuf.ListValue', new ListValueParser()],
  225. ['type.googleapis.com/google.protobuf.Empty', new EmptyParser()],
  226. ['type.googleapis.com/google.protobuf.Timestamp', new TimestampParser()],
  227. ['type.googleapis.com/google.protobuf.Duration', new DurationParser()],
  228. ['type.googleapis.com/google.protobuf.FieldMask', new FieldMaskParser()],
  229. ['type.googleapis.com/google.protobuf.Any', new AnyParser()]
  230. ]);