make fromString return a conversion object. rather than the converted value
This commit is contained in:
parent
ab25eee135
commit
022f3b240d
2 changed files with 159 additions and 119 deletions
|
|
@ -11,7 +11,7 @@
|
|||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Mozilla Skywriter.
|
||||
* The Original Code is Skywriter.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla.
|
||||
|
|
@ -34,8 +34,81 @@
|
|||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
define(function(require, exports, module) {
|
||||
|
||||
/**
|
||||
* Some types can detect validity, that is to say they can distinguish between
|
||||
* valid and invalid values.
|
||||
*/
|
||||
var Status = {
|
||||
/**
|
||||
* The conversion process worked without any problem, and the value is
|
||||
* valid. There are a number of failure states, so the best way to check
|
||||
* for failure is (x !== Status.VALID)
|
||||
*/
|
||||
VALID: 1,
|
||||
|
||||
/**
|
||||
* The conversion process did not work, the value should be null and a
|
||||
* reason for failure should have been provided. In addition some completion
|
||||
* values may be available.
|
||||
* @see Status.INCOMPLETE
|
||||
*/
|
||||
INVALID: 2,
|
||||
|
||||
/**
|
||||
* The conversion process did not work like Status.INVALID, however it was
|
||||
* noted that the string provided to 'fromString()' could be VALID by
|
||||
* the addition of more characters, so the typing may not be actually
|
||||
* incorrect yet, just unfinished.
|
||||
* @see Status.INVALID
|
||||
*/
|
||||
INCOMPLETE: 3
|
||||
};
|
||||
exports.Status = Status;
|
||||
|
||||
/**
|
||||
* The type.fromString() method returns a Conversion to inform the user about
|
||||
* not only the result of a Conversion but also about what went wrong.
|
||||
* We could use an exception, and throw if the conversion failed, but that
|
||||
* seems to violate the idea that exceptions should be exceptional. Typos are
|
||||
* not. Also in order to store both a status and a message we'd still need
|
||||
* some sort of exception type...
|
||||
*/
|
||||
function Conversion(value, status, message, predictions) {
|
||||
/**
|
||||
* The result of the conversion process. Will be null if status != VALID
|
||||
*/
|
||||
this.value = value;
|
||||
|
||||
/**
|
||||
* The status of the conversion.
|
||||
* @see Status
|
||||
*/
|
||||
this.status = status || Status.VALID;
|
||||
|
||||
/**
|
||||
* A message to go with the conversion. This could be present for any status
|
||||
* including VALID in the case where we want to note a warning for example.
|
||||
* I18N: On the one hand this nasty and un-internationalized, however with
|
||||
* a command line it is hard to know where to start.
|
||||
*/
|
||||
this.message = message;
|
||||
|
||||
/**
|
||||
* A array of strings which are the systems best guess at better inputs than
|
||||
* the one presented.
|
||||
* We generally expect there to be about 7 predictions (to match human list
|
||||
* comprehension ability) however it is valid to provide up to about 20,
|
||||
* or less. It is the job of the predictor to decide a smart cut-off.
|
||||
* For example if there are 4 very good matches and 4 very poor ones,
|
||||
* probably only the 4 very good matches should be presented.
|
||||
*/
|
||||
this.predictions = predictions || [];
|
||||
}
|
||||
exports.Conversion = Conversion;
|
||||
|
||||
/**
|
||||
* Most of our types are 'static' e.g. there is only one type of 'text', however
|
||||
* some types like 'selection' and 'deferred' are customizable. The basic
|
||||
|
|
@ -44,14 +117,7 @@ define(function(require, exports, module) {
|
|||
*/
|
||||
function Type() {
|
||||
};
|
||||
|
||||
Type.prototype = {
|
||||
/**
|
||||
* Is the passed <tt>value</tt> an acceptable instance of this type?
|
||||
* @return true|false to indicate the validity of <tt>value</tt>
|
||||
*/
|
||||
isValid: function(value) { throw new Error("not implemented"); },
|
||||
|
||||
/**
|
||||
* Convert the given <tt>value</tt> to a string representation.
|
||||
* Where possible, there should be round-tripping between values and their
|
||||
|
|
@ -63,37 +129,20 @@ Type.prototype = {
|
|||
* Convert the given <tt>str</tt> to an instance of this type.
|
||||
* Where possible, there should be round-tripping between values and their
|
||||
* string representations.
|
||||
* @return Conversion
|
||||
*/
|
||||
fromString: function(str) { throw new Error("not implemented"); },
|
||||
|
||||
/**
|
||||
* The plug-in system, and other things need to know what this type is
|
||||
* called. This is called <tt>simpleName</tt> because this name alone is not
|
||||
* enough to specify a type. Types like 'selection' and 'deferred' need
|
||||
* extra data, however this function returns only the name, not the extra
|
||||
* data.
|
||||
* called. The name alone is not enough to fully specify a type. Types like
|
||||
* 'selection' and 'deferred' need extra data, however this function returns
|
||||
* only the name, not the extra data.
|
||||
* <p>In old bespin, equality was based on the name. This may turn out to be
|
||||
* important in Ace too.
|
||||
*/
|
||||
name: "unknown"
|
||||
|
||||
// Methods from the original type system that we might need, but not now.
|
||||
|
||||
/**
|
||||
* All types have a JSON representation used in command parameter
|
||||
* declarations and settings. This allows access to that representation as
|
||||
* an object rather than as a string
|
||||
*/
|
||||
/*
|
||||
getTypeSpec: function() { },
|
||||
*/
|
||||
|
||||
/**
|
||||
* 2 typeSpecs are considered equal if their simple names are the same.
|
||||
*/
|
||||
/*
|
||||
equals: function(that) { }
|
||||
*/
|
||||
name: undefined
|
||||
};
|
||||
|
||||
exports.Type = Type;
|
||||
|
||||
/**
|
||||
|
|
@ -142,4 +191,5 @@ exports.getType = function(typeSpec) {
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -39,6 +39,8 @@
|
|||
define(function(require, exports, module) {
|
||||
|
||||
var types = require("pilot/types");
|
||||
var Type = types.Type;
|
||||
var Conversion = types.Conversion;
|
||||
|
||||
/**
|
||||
* These are the basic types that we accept. They are vaguely based on the
|
||||
|
|
@ -55,18 +57,17 @@ var types = require("pilot/types");
|
|||
/**
|
||||
* 'text' is the default if no type is given.
|
||||
*/
|
||||
var text = new types.Type();
|
||||
|
||||
text.isValid = function(value) {
|
||||
return typeof value == 'string';
|
||||
};
|
||||
var text = new Type();
|
||||
|
||||
text.toString = function(value) {
|
||||
return value;
|
||||
};
|
||||
|
||||
text.fromString = function(value) {
|
||||
return value;
|
||||
if (typeof value != 'string') {
|
||||
throw new Error('non-string passed to text.fromString()');
|
||||
}
|
||||
return new Conversion(value);
|
||||
};
|
||||
|
||||
text.name = 'text';
|
||||
|
|
@ -74,23 +75,7 @@ text.name = 'text';
|
|||
/**
|
||||
* We don't currently plan to distinguish between integers and floats
|
||||
*/
|
||||
var number = new types.Type();
|
||||
|
||||
number.isValid = function(value) {
|
||||
if (isNaN(value)) {
|
||||
return false;
|
||||
}
|
||||
if (value === null) {
|
||||
return false;
|
||||
}
|
||||
if (value === undefined) {
|
||||
return false;
|
||||
}
|
||||
if (value === Infinity) {
|
||||
return false;
|
||||
}
|
||||
return typeof value == 'number';// && !isNaN(value);
|
||||
};
|
||||
var number = new Type();
|
||||
|
||||
number.toString = function(value) {
|
||||
if (!value) {
|
||||
|
|
@ -100,52 +85,21 @@ number.toString = function(value) {
|
|||
};
|
||||
|
||||
number.fromString = function(value) {
|
||||
if (!value) {
|
||||
return null;
|
||||
if (typeof value != 'string') {
|
||||
throw new Error('non-string passed to number.fromString()');
|
||||
}
|
||||
var reply = parseInt(value, 10);
|
||||
if (isNaN(reply)) {
|
||||
throw new Error('Can\'t convert "' + value + '" to a number.');
|
||||
|
||||
var reply = new Conversion(parseInt(value, 10));
|
||||
if (isNaN(reply.value)) {
|
||||
reply.status = Status.INVALID;
|
||||
reply.message = 'Can\'t convert "' + value + '" to a number.';
|
||||
}
|
||||
|
||||
return reply;
|
||||
};
|
||||
|
||||
number.name = 'number';
|
||||
|
||||
/**
|
||||
* true/false values
|
||||
*/
|
||||
var bool = new types.Type();
|
||||
|
||||
bool.isValid = function(value) {
|
||||
return typeof value == 'boolean';
|
||||
};
|
||||
|
||||
bool.toString = function(value) {
|
||||
return '' + value;
|
||||
};
|
||||
|
||||
bool.fromString = function(value) {
|
||||
if (value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!value.toLowerCase) {
|
||||
return !!value;
|
||||
}
|
||||
|
||||
var lower = value.toLowerCase();
|
||||
if (lower == 'true') {
|
||||
return true;
|
||||
} else if (lower == 'false') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !!value;
|
||||
};
|
||||
|
||||
bool.name = 'bool';
|
||||
|
||||
/**
|
||||
* One of a known set of options
|
||||
*/
|
||||
|
|
@ -153,42 +107,77 @@ function SelectionType(data) {
|
|||
this._data = data;
|
||||
};
|
||||
|
||||
SelectionType.prototype = new types.Type();
|
||||
|
||||
SelectionType.prototype.isValid = function(value) {
|
||||
if (typeof value != 'string') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this._data) {
|
||||
console.error('Missing data on selection type extension. Skipping');
|
||||
return true;
|
||||
}
|
||||
|
||||
var data = (typeof(this._data) === "function") ? this._data() : this._data;
|
||||
|
||||
var match = false;
|
||||
data.forEach(function(option) {
|
||||
if (value == option) {
|
||||
match = true;
|
||||
}
|
||||
});
|
||||
|
||||
return match;
|
||||
};
|
||||
SelectionType.prototype = new Type();
|
||||
|
||||
SelectionType.prototype.toString = function(value) {
|
||||
return value;
|
||||
};
|
||||
|
||||
SelectionType.prototype.fromString = function(value) {
|
||||
// TODO: should we validate and return null if invalid?
|
||||
return value;
|
||||
if (typeof value != 'string') {
|
||||
throw new Error('non-string passed to fromString()');
|
||||
}
|
||||
if (!this._data) {
|
||||
throw new Error('Missing data on selection type extension.');
|
||||
}
|
||||
var data = (typeof(this._data) === "function") ? this._data() : this._data;
|
||||
|
||||
var match = false;
|
||||
var completions = [];
|
||||
data.forEach(function(option) {
|
||||
if (value == option) {
|
||||
match = true;
|
||||
}
|
||||
else if (option.indexOf(value) === 0) {
|
||||
completions.push(option);
|
||||
}
|
||||
});
|
||||
|
||||
if (match) {
|
||||
return new Conversion(value);
|
||||
}
|
||||
else {
|
||||
var status = completions.length > 0 ? Status.INCOMPLETE : Status.INVALID;
|
||||
|
||||
// TODO: better error message - include options?
|
||||
// TODO: better completions - we're just using the extensions
|
||||
return new Conversion(null,
|
||||
status,
|
||||
'Can\'t convert "' + value + '" to a selection.',
|
||||
completions);
|
||||
}
|
||||
};
|
||||
|
||||
SelectionType.prototype.name = 'selection';
|
||||
|
||||
|
||||
/**
|
||||
* true/false values
|
||||
*/
|
||||
var bool = new SelectionType([ 'true', 'false' ]);
|
||||
|
||||
bool.toString = function(value) {
|
||||
return '' + value;
|
||||
};
|
||||
|
||||
bool.fromString = function(value) {
|
||||
var conversion = SelectionType.prototype.fromString(value);
|
||||
|
||||
if (conversion.value === 'true') {
|
||||
conversion.value = true;
|
||||
}
|
||||
if (conversion.value === 'false') {
|
||||
conversion.value = false;
|
||||
}
|
||||
|
||||
return conversion;
|
||||
};
|
||||
|
||||
bool.name = 'bool';
|
||||
|
||||
/**
|
||||
* Registration and de-registration.
|
||||
*/
|
||||
exports.startup = function() {
|
||||
types.registerType(text);
|
||||
types.registerType(number);
|
||||
|
|
@ -203,4 +192,5 @@ exports.shutdown = function() {
|
|||
types.unregisterType(SelectionType);
|
||||
};
|
||||
|
||||
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue