No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

method.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "use strict";
  2. module.exports = Method;
  3. // extends ReflectionObject
  4. var ReflectionObject = require("./object");
  5. ((Method.prototype = Object.create(ReflectionObject.prototype)).constructor = Method).className = "Method";
  6. var util = require("./util");
  7. /**
  8. * Constructs a new service method instance.
  9. * @classdesc Reflected service method.
  10. * @extends ReflectionObject
  11. * @constructor
  12. * @param {string} name Method name
  13. * @param {string|undefined} type Method type, usually `"rpc"`
  14. * @param {string} requestType Request message type
  15. * @param {string} responseType Response message type
  16. * @param {boolean|Object.<string,*>} [requestStream] Whether the request is streamed
  17. * @param {boolean|Object.<string,*>} [responseStream] Whether the response is streamed
  18. * @param {Object.<string,*>} [options] Declared options
  19. * @param {string} [comment] The comment for this method
  20. * @param {Object.<string,*>} [parsedOptions] Declared options, properly parsed into an object
  21. */
  22. function Method(name, type, requestType, responseType, requestStream, responseStream, options, comment, parsedOptions) {
  23. /* istanbul ignore next */
  24. if (util.isObject(requestStream)) {
  25. options = requestStream;
  26. requestStream = responseStream = undefined;
  27. } else if (util.isObject(responseStream)) {
  28. options = responseStream;
  29. responseStream = undefined;
  30. }
  31. /* istanbul ignore if */
  32. if (!(type === undefined || util.isString(type)))
  33. throw TypeError("type must be a string");
  34. /* istanbul ignore if */
  35. if (!util.isString(requestType))
  36. throw TypeError("requestType must be a string");
  37. /* istanbul ignore if */
  38. if (!util.isString(responseType))
  39. throw TypeError("responseType must be a string");
  40. ReflectionObject.call(this, name, options);
  41. /**
  42. * Method type.
  43. * @type {string}
  44. */
  45. this.type = type || "rpc"; // toJSON
  46. /**
  47. * Request type.
  48. * @type {string}
  49. */
  50. this.requestType = requestType; // toJSON, marker
  51. /**
  52. * Whether requests are streamed or not.
  53. * @type {boolean|undefined}
  54. */
  55. this.requestStream = requestStream ? true : undefined; // toJSON
  56. /**
  57. * Response type.
  58. * @type {string}
  59. */
  60. this.responseType = responseType; // toJSON
  61. /**
  62. * Whether responses are streamed or not.
  63. * @type {boolean|undefined}
  64. */
  65. this.responseStream = responseStream ? true : undefined; // toJSON
  66. /**
  67. * Resolved request type.
  68. * @type {Type|null}
  69. */
  70. this.resolvedRequestType = null;
  71. /**
  72. * Resolved response type.
  73. * @type {Type|null}
  74. */
  75. this.resolvedResponseType = null;
  76. /**
  77. * Comment for this method
  78. * @type {string|null}
  79. */
  80. this.comment = comment;
  81. /**
  82. * Options properly parsed into an object
  83. */
  84. this.parsedOptions = parsedOptions;
  85. }
  86. /**
  87. * Method descriptor.
  88. * @interface IMethod
  89. * @property {string} [type="rpc"] Method type
  90. * @property {string} requestType Request type
  91. * @property {string} responseType Response type
  92. * @property {boolean} [requestStream=false] Whether requests are streamed
  93. * @property {boolean} [responseStream=false] Whether responses are streamed
  94. * @property {Object.<string,*>} [options] Method options
  95. * @property {string} comment Method comments
  96. * @property {Object.<string,*>} [parsedOptions] Method options properly parsed into an object
  97. */
  98. /**
  99. * Constructs a method from a method descriptor.
  100. * @param {string} name Method name
  101. * @param {IMethod} json Method descriptor
  102. * @returns {Method} Created method
  103. * @throws {TypeError} If arguments are invalid
  104. */
  105. Method.fromJSON = function fromJSON(name, json) {
  106. return new Method(name, json.type, json.requestType, json.responseType, json.requestStream, json.responseStream, json.options, json.comment, json.parsedOptions);
  107. };
  108. /**
  109. * Converts this method to a method descriptor.
  110. * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
  111. * @returns {IMethod} Method descriptor
  112. */
  113. Method.prototype.toJSON = function toJSON(toJSONOptions) {
  114. var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
  115. return util.toObject([
  116. "type" , this.type !== "rpc" && /* istanbul ignore next */ this.type || undefined,
  117. "requestType" , this.requestType,
  118. "requestStream" , this.requestStream,
  119. "responseType" , this.responseType,
  120. "responseStream" , this.responseStream,
  121. "options" , this.options,
  122. "comment" , keepComments ? this.comment : undefined,
  123. "parsedOptions" , this.parsedOptions,
  124. ]);
  125. };
  126. /**
  127. * @override
  128. */
  129. Method.prototype.resolve = function resolve() {
  130. /* istanbul ignore if */
  131. if (this.resolved)
  132. return this;
  133. this.resolvedRequestType = this.parent.lookupType(this.requestType);
  134. this.resolvedResponseType = this.parent.lookupType(this.responseType);
  135. return ReflectionObject.prototype.resolve.call(this);
  136. };