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