Source: client/parser/type-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 ObjectParser from './object-parser';
  28. import {wellKnownParsers} from './well-known-parsers';
  29. /**
  30. * The registry of parsers for known Protobuf types.
  31. *
  32. * <p>Parsers for {@link wellKnownParsers standard} Protobuf types are provided by the Spine Web
  33. * and automatically registered.
  34. *
  35. * <p>Other parsers are generated by the Protobuf plugin for Javascript and should be registered
  36. * by the users of the class.
  37. */
  38. export default class TypeParsers {
  39. constructor() {
  40. throw new Error('TypeParsers is not supposed to be instantiated.');
  41. }
  42. /**
  43. * Registers the parser for a type.
  44. *
  45. * @param {!ObjectParser} parser the parser instance to register
  46. * @param {!string} typeUrl the URL of the type to register the parser for
  47. */
  48. static register(parser, typeUrl) {
  49. this._checkParser(parser, typeUrl);
  50. if (!parsers.has(typeUrl)) {
  51. parsers.set(typeUrl, parser);
  52. }
  53. }
  54. /**
  55. * Obtains a parser by the specified type URL.
  56. *
  57. * @param {!string} typeUrl the type URL to get the parser
  58. * @returns {!ObjectParser} the parser instance for the type
  59. */
  60. static parserFor(typeUrl) {
  61. const parser = parsers.get(typeUrl);
  62. if (parser === undefined) {
  63. throw new Error(`The parser for ${typeUrl} was not found.`);
  64. }
  65. return parser;
  66. }
  67. static _checkParser(parser, typeUrl) {
  68. if (!ObjectParser.isParser(parser)) {
  69. throw new Error(`Unable to register parser for ${typeUrl}. Should extend ObjectParser.`);
  70. }
  71. }
  72. }
  73. /**
  74. * Register parsers for standard Protobuf types.
  75. */
  76. function registerWellKnownParsers() {
  77. for (let [typeUrl, parser] of wellKnownParsers) {
  78. TypeParsers.register(parser, typeUrl);
  79. }
  80. }
  81. /**
  82. * The map of all Protobuf parsers known to the application.
  83. *
  84. * <p>It is intended to be a static variable, but ES6 doesn't provide an easy way to do it.
  85. *
  86. * @type {Map<String, ObjectParser>}
  87. * @private
  88. */
  89. const parsers = new Map();
  90. registerWellKnownParsers();