Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / protobufjs / src / method.js
1 "use strict";
2 module.exports = Method;
3
4 // extends ReflectionObject
5 var ReflectionObject = require("./object");
6 ((Method.prototype = Object.create(ReflectionObject.prototype)).constructor = Method).className = "Method";
7
8 var util = require("./util");
9
10 /**
11  * Constructs a new service method instance.
12  * @classdesc Reflected service method.
13  * @extends ReflectionObject
14  * @constructor
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
24  */
25 function 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"; // toJSON
55
56     /**
57      * Request type.
58      * @type {string}
59      */
60     this.requestType = requestType; // toJSON, marker
61
62     /**
63      * Whether requests are streamed or not.
64      * @type {boolean|undefined}
65      */
66     this.requestStream = requestStream ? true : undefined; // toJSON
67
68     /**
69      * Response type.
70      * @type {string}
71      */
72     this.responseType = responseType; // toJSON
73
74     /**
75      * Whether responses are streamed or not.
76      * @type {boolean|undefined}
77      */
78     this.responseStream = responseStream ? true : undefined; // toJSON
79
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 method
94      * @type {string|null}
95      */
96     this.comment = comment;
97
98     /**
99      * Options properly parsed into an object
100      */
101     this.parsedOptions = parsedOptions;
102 }
103
104 /**
105  * Method descriptor.
106  * @interface IMethod
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
115  */
116
117 /**
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
123  */
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);
126 };
127
128 /**
129  * Converts this method to a method descriptor.
130  * @param {IToJSONOptions} [toJSONOptions] JSON conversion options
131  * @returns {IMethod} Method descriptor
132  */
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,
144     ]);
145 };
146
147 /**
148  * @override
149  */
150 Method.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 };