oneof.js
5.64 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"use strict";
module.exports = OneOf;
// extends ReflectionObject
var ReflectionObject = require("./object");
((OneOf.prototype = Object.create(ReflectionObject.prototype)).constructor = OneOf).className = "OneOf";
var Field = require("./field"),
util = require("./util");
/**
* Constructs a new oneof instance.
* @classdesc Reflected oneof.
* @extends ReflectionObject
* @constructor
* @param {string} name Oneof name
* @param {string[]|Object.<string,*>} [fieldNames] Field names
* @param {Object.<string,*>} [options] Declared options
* @param {string} [comment] Comment associated with this field
*/
function OneOf(name, fieldNames, options, comment) {
if (!Array.isArray(fieldNames)) {
options = fieldNames;
fieldNames = undefined;
}
ReflectionObject.call(this, name, options);
/* istanbul ignore if */
if (!(fieldNames === undefined || Array.isArray(fieldNames)))
throw TypeError("fieldNames must be an Array");
/**
* Field names that belong to this oneof.
* @type {string[]}
*/
this.oneof = fieldNames || []; // toJSON, marker
/**
* Fields that belong to this oneof as an array for iteration.
* @type {Field[]}
* @readonly
*/
this.fieldsArray = []; // declared readonly for conformance, possibly not yet added to parent
/**
* Comment for this field.
* @type {string|null}
*/
this.comment = comment;
}
/**
* Oneof descriptor.
* @interface IOneOf
* @property {Array.<string>} oneof Oneof field names
* @property {Object.<string,*>} [options] Oneof options
*/
/**
* Constructs a oneof from a oneof descriptor.
* @param {string} name Oneof name
* @param {IOneOf} json Oneof descriptor
* @returns {OneOf} Created oneof
* @throws {TypeError} If arguments are invalid
*/
OneOf.fromJSON = function fromJSON(name, json) {
return new OneOf(name, json.oneof, json.options, json.comment);
};
/**
* Converts this oneof to a oneof descriptor.
* @param {IToJSONOptions} [toJSONOptions] JSON conversion options
* @returns {IOneOf} Oneof descriptor
*/
OneOf.prototype.toJSON = function toJSON(toJSONOptions) {
var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
return util.toObject([
"options" , this.options,
"oneof" , this.oneof,
"comment" , keepComments ? this.comment : undefined
]);
};
/**
* Adds the fields of the specified oneof to the parent if not already done so.
* @param {OneOf} oneof The oneof
* @returns {undefined}
* @inner
* @ignore
*/
function addFieldsToParent(oneof) {
if (oneof.parent)
for (var i = 0; i < oneof.fieldsArray.length; ++i)
if (!oneof.fieldsArray[i].parent)
oneof.parent.add(oneof.fieldsArray[i]);
}
/**
* Adds a field to this oneof and removes it from its current parent, if any.
* @param {Field} field Field to add
* @returns {OneOf} `this`
*/
OneOf.prototype.add = function add(field) {
/* istanbul ignore if */
if (!(field instanceof Field))
throw TypeError("field must be a Field");
if (field.parent && field.parent !== this.parent)
field.parent.remove(field);
this.oneof.push(field.name);
this.fieldsArray.push(field);
field.partOf = this; // field.parent remains null
addFieldsToParent(this);
return this;
};
/**
* Removes a field from this oneof and puts it back to the oneof's parent.
* @param {Field} field Field to remove
* @returns {OneOf} `this`
*/
OneOf.prototype.remove = function remove(field) {
/* istanbul ignore if */
if (!(field instanceof Field))
throw TypeError("field must be a Field");
var index = this.fieldsArray.indexOf(field);
/* istanbul ignore if */
if (index < 0)
throw Error(field + " is not a member of " + this);
this.fieldsArray.splice(index, 1);
index = this.oneof.indexOf(field.name);
/* istanbul ignore else */
if (index > -1) // theoretical
this.oneof.splice(index, 1);
field.partOf = null;
return this;
};
/**
* @override
*/
OneOf.prototype.onAdd = function onAdd(parent) {
ReflectionObject.prototype.onAdd.call(this, parent);
var self = this;
// Collect present fields
for (var i = 0; i < this.oneof.length; ++i) {
var field = parent.get(this.oneof[i]);
if (field && !field.partOf) {
field.partOf = self;
self.fieldsArray.push(field);
}
}
// Add not yet present fields
addFieldsToParent(this);
};
/**
* @override
*/
OneOf.prototype.onRemove = function onRemove(parent) {
for (var i = 0, field; i < this.fieldsArray.length; ++i)
if ((field = this.fieldsArray[i]).parent)
field.parent.remove(field);
ReflectionObject.prototype.onRemove.call(this, parent);
};
/**
* Decorator function as returned by {@link OneOf.d} (TypeScript).
* @typedef OneOfDecorator
* @type {function}
* @param {Object} prototype Target prototype
* @param {string} oneofName OneOf name
* @returns {undefined}
*/
/**
* OneOf decorator (TypeScript).
* @function
* @param {...string} fieldNames Field names
* @returns {OneOfDecorator} Decorator function
* @template T extends string
*/
OneOf.d = function decorateOneOf() {
var fieldNames = new Array(arguments.length),
index = 0;
while (index < arguments.length)
fieldNames[index] = arguments[index++];
return function oneOfDecorator(prototype, oneofName) {
util.decorateType(prototype.constructor)
.add(new OneOf(oneofName, fieldNames));
Object.defineProperty(prototype, oneofName, {
get: util.oneOfGetter(fieldNames),
set: util.oneOfSetter(fieldNames)
});
};
};