basant307/AI_Governance_Project
048
1"use strict";2module.exports = Method;3 4// extends ReflectionObject5var ReflectionObject = require("./object");6((Method.prototype = Object.create(ReflectionObject.prototype)).constructor = Method).className = "Method";7 8var util = require("./util");9 10/**11 * Constructs a new service method instance.12 * @classdesc Reflected service method.13 * @extends ReflectionObject14 * @constructor15 * @param {string} name Method name16 * @param {string|undefined} type Method type, usually `"rpc"`17 * @param {string} requestType Request message type18 * @param {string} responseType Response message type19 * @param {boolean|Object.<string,*>} [requestStream] Whether the request is streamed20 * @param {boolean|Object.<string,*>} [responseStream] Whether the response is streamed21 * @param {Object.<string,*>} [options] Declared options22 * @param {string} [comment] The comment for this method23 * @param {Object.<string,*>} [parsedOptions] Declared options, properly parsed into an object24 */25function Method(name, type, requestType, responseType, requestStream, responseStream, options, comment, parsedOptions) {26 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;34 }35 36 /* istanbul ignore if */37 if (!(type === undefined || util.isString(type)))38 throw TypeError("type must be a string");39 40 /* istanbul ignore if */41 if (!util.isString(requestType))42 throw TypeError("requestType must be a string");43 44 /* istanbul ignore if */45 if (!util.isString(responseType))46 throw TypeError("responseType must be a string");47 48 ReflectionObject.call(this, name, options);49 50 /**51 * Method type.52 * @type {string}53 */54 this.type = type || "rpc"; // toJSON55 56 /**57 * Request type.58 * @type {string}59 */60 this.requestType = requestType; // toJSON, marker61 62 /**63 * Whether requests are streamed or not.64 * @type {boolean|undefined}65 */66 this.requestStream = requestStream ? true : undefined; // toJSON67 68 /**69 * Response type.70 * @type {string}71 */72 this.responseType = responseType; // toJSON73 74 /**75 * Whether responses are streamed or not.76 * @type {boolean|undefined}77 */78 this.responseStream = responseStream ? true : undefined; // toJSON79 80 /**81 * Resolved request type.82 * @type {Type|null}83 */84 this.resolvedRequestType = null;85 86 /**87 * Resolved response type.88 * @type {Type|null}89 */90 this.resolvedResponseType = null;91 92 /**93 * Comment for this method94 * @type {string|null}95 */96 this.comment = comment;97 98 /**99 * Options properly parsed into an object100 */101 this.parsedOptions = parsedOptions;102}103 104/**105 * Method descriptor.106 * @interface IMethod107 * @property {string} [type="rpc"] Method type108 * @property {string} requestType Request type109 * @property {string} responseType Response type110 * @property {boolean} [requestStream=false] Whether requests are streamed111 * @property {boolean} [responseStream=false] Whether responses are streamed112 * @property {Object.<string,*>} [options] Method options113 * @property {string} comment Method comments114 * @property {Object.<string,*>} [parsedOptions] Method options properly parsed into an object115 */116 117/**118 * Constructs a method from a method descriptor.119 * @param {string} name Method name120 * @param {IMethod} json Method descriptor121 * @returns {Method} Created method122 * @throws {TypeError} If arguments are invalid123 */124Method.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);126};127 128/**129 * Converts this method to a method descriptor.130 * @param {IToJSONOptions} [toJSONOptions] JSON conversion options131 * @returns {IMethod} Method descriptor132 */133Method.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,144 ]);145};146 147/**148 * @override149 */150Method.prototype.resolve = function resolve() {151 152 /* istanbul ignore if */153 if (this.resolved)154 return this;155 156 this.resolvedRequestType = this.parent.lookupType(this.requestType);157 this.resolvedResponseType = this.parent.lookupType(this.responseType);158 159 return ReflectionObject.prototype.resolve.call(this);160};161 