Comparing version 0.0.7 to 0.0.8
@@ -92,3 +92,2 @@ "use strict"; | ||
this.variable = variable; | ||
this.name = childReference.name; | ||
} | ||
@@ -101,7 +100,17 @@ | ||
value: function resolve(context) { | ||
var resolvedChild = this.childReference.resolve(context); | ||
var resolvedChild = this.childReference; | ||
var length = this.length; | ||
if (resolvedChild instanceof Reference) { | ||
resolvedChild = resolvedChild.resolve(context); | ||
} | ||
if (length instanceof Reference) { | ||
length = length.resolve(context); | ||
} | ||
if (this.variable) { | ||
return new XDR.VarArray(resolvedChild, this.length); | ||
return new XDR.VarArray(resolvedChild, length); | ||
} else { | ||
return new XDR.Array(resolvedChild, this.length); | ||
return new XDR.Array(resolvedChild, length); | ||
} | ||
@@ -128,3 +137,8 @@ } | ||
value: function resolve(context) { | ||
var resolvedChild = this.childReference.resolve(context); | ||
var resolvedChild = this.childReference; | ||
if (resolvedChild instanceof Reference) { | ||
resolvedChild = resolvedChild.resolve(context); | ||
} | ||
return new XDR.Option(resolvedChild); | ||
@@ -138,2 +152,29 @@ } | ||
var SizedReference = (function (_Reference4) { | ||
function SizedReference(sizedType, length) { | ||
_classCallCheck(this, SizedReference); | ||
this.sizedType = sizedType; | ||
this.length = length; | ||
} | ||
_inherits(SizedReference, _Reference4); | ||
_createClass(SizedReference, { | ||
resolve: { | ||
value: function resolve(context) { | ||
var length = this.length; | ||
if (length instanceof Reference) { | ||
length = length.resolve(context); | ||
} | ||
return new this.sizedType(length); | ||
} | ||
} | ||
}); | ||
return SizedReference; | ||
})(Reference); | ||
var Definition = (function () { | ||
@@ -270,3 +311,3 @@ function Definition(constructor, name, config) { | ||
value: function string(length) { | ||
return new XDR.String(length); | ||
return new SizedReference(XDR.String, length); | ||
} | ||
@@ -276,3 +317,3 @@ }, | ||
value: function opaque(length) { | ||
return new XDR.Opaque(length); | ||
return new SizedReference(XDR.Opaque, length); | ||
} | ||
@@ -282,3 +323,3 @@ }, | ||
value: function varOpaque(length) { | ||
return new XDR.VarOpaque(length); | ||
return new SizedReference(XDR.VarOpaque, length); | ||
} | ||
@@ -288,7 +329,3 @@ }, | ||
value: function array(childType, length) { | ||
if (childType instanceof Reference) { | ||
return new ArrayReference(childType, length); | ||
} else { | ||
return new XDR.Array(childType, length); | ||
} | ||
return new ArrayReference(childType, length); | ||
} | ||
@@ -298,7 +335,3 @@ }, | ||
value: function varArray(childType, maxLength) { | ||
if (childType instanceof Reference) { | ||
return new ArrayReference(childType, maxLength, true); | ||
} else { | ||
return new XDR.VarArray(childType, maxLength); | ||
} | ||
return new ArrayReference(childType, maxLength, true); | ||
} | ||
@@ -308,7 +341,3 @@ }, | ||
value: function option(childType) { | ||
if (childType instanceof Reference) { | ||
return new OptionReference(childType); | ||
} else { | ||
return new XDR.Option(childType); | ||
} | ||
return new OptionReference(childType); | ||
} | ||
@@ -315,0 +344,0 @@ }, |
@@ -11,2 +11,4 @@ "use strict"; | ||
var _slicedToArray = require("babel-runtime/helpers/sliced-to-array")["default"]; | ||
var _core = require("babel-runtime/core-js")["default"]; | ||
@@ -24,2 +26,3 @@ | ||
var isUndefined = _lodash.isUndefined; | ||
var isString = _lodash.isString; | ||
@@ -85,10 +88,9 @@ var Void = require("./void").Void; | ||
value: function armForSwitch(aSwitch) { | ||
var arm = this._switches.get(aSwitch); | ||
if (isUndefined(arm)) { | ||
if (this._switches.has(aSwitch)) { | ||
return this._switches.get(aSwitch); | ||
} else if (this._defaultArm) { | ||
return this._defaultArm; | ||
} else { | ||
throw new Error("Bad union switch: " + aSwitch); | ||
} | ||
return arm; | ||
} | ||
@@ -173,19 +175,35 @@ }, | ||
each(ChildUnion._switchOn.values(), function (aSwitch) { | ||
ChildUnion._defaultArm = defaultArm; | ||
// build the enum => arm map | ||
var arm = config.switches[aSwitch.name] || defaultArm; | ||
ChildUnion._switches.set(aSwitch, arm); | ||
each(config.switches, function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2); | ||
// Add enum-based constrocutors | ||
ChildUnion[aSwitch.name] = function (value) { | ||
return new ChildUnion(aSwitch, value); | ||
}; | ||
var aSwitch = _ref2[0]; | ||
var armName = _ref2[1]; | ||
// Add enum-based "set" helpers | ||
ChildUnion.prototype[aSwitch.name] = function (value) { | ||
return this.set(aSwitch, value); | ||
}; | ||
if (isString(aSwitch)) { | ||
aSwitch = ChildUnion._switchOn.fromName(aSwitch); | ||
} | ||
ChildUnion._switches.set(aSwitch, armName); | ||
}); | ||
// add enum-based helpers | ||
// NOTE: we don't have good notation for "is a subclass of XDR.Enum", | ||
// and so we use the following check (does _switchOn have a `values` | ||
// attribute) to approximate the intent. | ||
if (!isUndefined(ChildUnion._switchOn.values)) { | ||
each(ChildUnion._switchOn.values(), function (aSwitch) { | ||
// Add enum-based constrocutors | ||
ChildUnion[aSwitch.name] = function (value) { | ||
return new ChildUnion(aSwitch, value); | ||
}; | ||
// Add enum-based "set" helpers | ||
ChildUnion.prototype[aSwitch.name] = function (value) { | ||
return this.set(aSwitch, value); | ||
}; | ||
}); | ||
} | ||
// Add arm accessor helpers | ||
@@ -192,0 +210,0 @@ each(ChildUnion._arms, function (type, name) { |
{ | ||
"name": "js-xdr", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Read/write XDR encoded data structures (RFC 4506)", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
960008
22595