2 module.exports = Method;
4 // extends ReflectionObject
5 var ReflectionObject = require("./object");
6 ((Method.prototype = Object.create(ReflectionObject.prototype)).constructor = Method).className = "Method";
8 var util = require("./util");
11 * Constructs a new service method instance.
12 * @classdesc Reflected service method.
13 * @extends ReflectionObject
15 * @param {string} name Method name
16 * @param {string|undefined} type Method type, usually `"rpc"`
17 * @param {string} requestType Request message type
18 * @param {string} responseType Response message type
19 * @param {boolean|Object.<string,*>} [requestStream] Whether the request is streamed
20 * @param {boolean|Object.<string,*>} [responseStream] Whether the response is streamed
21 * @param {Object.<string,*>} [options] Declared options
22 * @param {string} [comment] The comment for this method
23 * @param {Object.<string,*>} [parsedOptions] Declared options, properly parsed into an object
25 function Method(name, type, requestType, responseType, requestStream, responseStream, options, comment, parsedOptions) {
27 /* istanbul ignore next */
28 if (util.isObject(requestStream)) {
29 options = requestStream;
30 requestStream = responseStream = undefined;
31 } else if (util.isObject(responseStream)) {
32 options = responseStream;
33 responseStream = undefined;
36 /* istanbul ignore if */
37 if (!(type === undefined || util.isString(type)))
38 throw TypeError("type must be a string");
40 /* istanbul ignore if */
41 if (!util.isString(requestType))
42 throw TypeError("requestType must be a string");
44 /* istanbul ignore if */
45 if (!util.isString(responseType))
46 throw TypeError("responseType must be a string");
48 ReflectionObject.call(this, name, options);
54 this.type = type || "rpc"; // toJSON
60 this.requestType = requestType; // toJSON, marker
63 * Whether requests are streamed or not.
64 * @type {boolean|undefined}
66 this.requestStream = requestStream ? true : undefined; // toJSON
72 this.responseType = responseType; // toJSON
75 * Whether responses are streamed or not.
76 * @type {boolean|undefined}
78 this.responseStream = responseStream ? true : undefined; // toJSON
81 * Resolved request type.
84 this.resolvedRequestType = null;
87 * Resolved response type.
90 this.resolvedResponseType = null;
93 * Comment for this method
96 this.comment = comment;
99 * Options properly parsed into an object
101 this.parsedOptions = parsedOptions;
107 * @property {string} [type="rpc"] Method type
108 * @property {string} requestType Request type
109 * @property {string} responseType Response type
110 * @property {boolean} [requestStream=false] Whether requests are streamed
111 * @property {boolean} [responseStream=false] Whether responses are streamed
112 * @property {Object.<string,*>} [options] Method options
113 * @property {string} comment Method comments
114 * @property {Object.<string,*>} [parsedOptions] Method options properly parsed into an object
118 * Constructs a method from a method descriptor.
119 * @param {string} name Method name
120 * @param {IMethod} json Method descriptor
121 * @returns {Method} Created method
122 * @throws {TypeError} If arguments are invalid
124 Method.fromJSON = function fromJSON(name, json) {
125 return new Method(name, json.type, json.requestType, json.responseType, json.requestStream, json.responseStream, json.options, json.comment, json.parsedOptions);
129 * Converts this method to a method descriptor.
130 * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
131 * @returns {IMethod} Method descriptor
133 Method.prototype.toJSON = function toJSON(toJSONOptions) {
134 var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
135 return util.toObject([
136 "type" , this.type !== "rpc" && /* istanbul ignore next */ this.type || undefined,
137 "requestType" , this.requestType,
138 "requestStream" , this.requestStream,
139 "responseType" , this.responseType,
140 "responseStream" , this.responseStream,
141 "options" , this.options,
142 "comment" , keepComments ? this.comment : undefined,
143 "parsedOptions" , this.parsedOptions,
150 Method.prototype.resolve = function resolve() {
152 /* istanbul ignore if */
156 this.resolvedRequestType = this.parent.lookupType(this.requestType);
157 this.resolvedResponseType = this.parent.lookupType(this.responseType);
159 return ReflectionObject.prototype.resolve.call(this);