|
"use strict"; |
|
module.exports = OneOf; |
|
|
|
|
|
var ReflectionObject = require("./object"); |
|
((OneOf.prototype = Object.create(ReflectionObject.prototype)).constructor = OneOf).className = "OneOf"; |
|
|
|
var Field = require("./field"), |
|
util = require("./util"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function OneOf(name, fieldNames, options, comment) { |
|
if (!Array.isArray(fieldNames)) { |
|
options = fieldNames; |
|
fieldNames = undefined; |
|
} |
|
ReflectionObject.call(this, name, options); |
|
|
|
|
|
if (!(fieldNames === undefined || Array.isArray(fieldNames))) |
|
throw TypeError("fieldNames must be an Array"); |
|
|
|
|
|
|
|
|
|
|
|
this.oneof = fieldNames || []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
this.fieldsArray = []; |
|
|
|
|
|
|
|
|
|
|
|
this.comment = comment; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OneOf.fromJSON = function fromJSON(name, json) { |
|
return new OneOf(name, json.oneof, json.options, json.comment); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
]); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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]); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
OneOf.prototype.add = function add(field) { |
|
|
|
|
|
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; |
|
addFieldsToParent(this); |
|
return this; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
OneOf.prototype.remove = function remove(field) { |
|
|
|
|
|
if (!(field instanceof Field)) |
|
throw TypeError("field must be a Field"); |
|
|
|
var index = this.fieldsArray.indexOf(field); |
|
|
|
|
|
if (index < 0) |
|
throw Error(field + " is not a member of " + this); |
|
|
|
this.fieldsArray.splice(index, 1); |
|
index = this.oneof.indexOf(field.name); |
|
|
|
|
|
if (index > -1) |
|
this.oneof.splice(index, 1); |
|
|
|
field.partOf = null; |
|
return this; |
|
}; |
|
|
|
|
|
|
|
|
|
OneOf.prototype.onAdd = function onAdd(parent) { |
|
ReflectionObject.prototype.onAdd.call(this, parent); |
|
var self = this; |
|
|
|
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); |
|
} |
|
} |
|
|
|
addFieldsToParent(this); |
|
}; |
|
|
|
|
|
|
|
|
|
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); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(OneOf.prototype, "isProto3Optional", { |
|
get: function() { |
|
if (this.fieldsArray == null || this.fieldsArray.length !== 1) { |
|
return false; |
|
} |
|
|
|
var field = this.fieldsArray[0]; |
|
return field.options != null && field.options["proto3_optional"] === true; |
|
} |
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |
|
}); |
|
}; |
|
}; |
|
|