Source: client/object-to-proto.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 {Message} from 'google-protobuf';
  28. import {Observable} from 'rxjs';
  29. import {map} from 'rxjs/operators';
  30. import TypeParsers from './parser/type-parsers';
  31. import KnownTypes from './known-types';
  32. /**
  33. * A utility which converts the JS object to its Protobuf counterpart.
  34. */
  35. export default class ObjectToProto {
  36. constructor() {
  37. throw new Error('ObjectToProto is not supposed to be instantiated.');
  38. }
  39. /**
  40. * Converts the object to the corresponding Protobuf message.
  41. *
  42. * The input object is supposed to be a Protobuf message representation, i.e. all of its attributes should
  43. * correspond to the fields of the specified message type.
  44. *
  45. * @param {!Object} object an object to convert
  46. * @param {!string} typeUrl a type URL of the corresponding Protobuf message
  47. */
  48. static convert(object, typeUrl) {
  49. if (!KnownTypes.hasType(typeUrl)) {
  50. throw new Error(`Unable to convert object of unknown type ${typeUrl}`);
  51. }
  52. const parser = TypeParsers.parserFor(typeUrl);
  53. const proto = parser.fromObject(object);
  54. return proto;
  55. }
  56. /**
  57. * Convert the given observable of objects to the observable of the
  58. * corresponding Protobuf messages.
  59. *
  60. * @param {!Observable<Object>} observable an observable to convert
  61. * @param {!string} typeUrl a type URL of the corresponding Protobuf message
  62. * @return {Observable<Message>} an observable of converted Protobuf messages
  63. */
  64. static map(observable, typeUrl) {
  65. return observable.pipe(
  66. map(object => ObjectToProto.convert(object, typeUrl))
  67. );
  68. }
  69. }