Reader
Direct Subclass:
A reader is used by the handler during the execution (Handler.runAction) to query the Input and Session information that is going be used during the execution of the action.
In case of new implements it's expected to implement the Reader._perform.
When a value is found for an input it's decoded using Input.parseValue where each input implementation has its own way of parsing the serialized data, to find out about how a value is serialized for an specific input type you could simply set an arbitrary value to an input then query it back through Input.serializeValue. The reference bellow shows the basic serialization for the inputs blundled with Mebo:
Input Type | Scalar Serialization | Vector Serialization (compatible with JSON) |
---|---|---|
Text | 'value' |
'["valueA","valueB"]' |
FilePath | '/tmp/a.txt' |
'["/tmp/a.txt","/tmp/b.txt"]' |
Bool | 'true ' or '1' |
'[true,false]' or '[1,0]' |
Numeric | '20' |
'[20,30]' |
'test@email.com' |
'["test@email.com","test2@email.com"]' |
|
Ip | '192.168.0.1' |
'["192.168.0.1","192.168.0.2"]' |
Timestamp | '2017-02-02T22:26:30.431Z' |
'["2017-02-02T22:26:30.431Z","2017-02-02T22:27:19.066Z"]' |
Url | 'http://www.google.com' |
'["http://www.google.com","http://www.wikipedia.com"]' |
Version | '10.1.1' |
'["10.1.1","10.2"]' |
Buf | 'aGVsbG8=' |
'["aGVsbG8=","d29ybGQ="]' |
Hex | 'ffff00' |
'["ffff00","ff"]' |
Hash | 'd65709ab' |
'["d65709ab","b94d6fe4"]' |
UUID | '075054e0-810a-11e6-8c1d-e5fb28c699ca' |
'["075054e0-810a-11e6-8c1d-e5fb28c699ca","98e631d3-6255-402a-88bd-66056e1ca9df"]' |
Options:
Custom options can be assigned to readers (Reader.setOption). They are
passed from the handler to the reader during the handler's execution
(Handler.runAction).
const myHandler = Mebo.Handler.create('someHandler');
// setting reading options
myHandler.runAction('myAction', {
someOption: 10,
});
When an action is executed through a handler it can define options via the Metadata support. Detailed information about that can be found at Metadata:
@Mebo.register('myAction')
class MyAction extends Mebo.Action{
constructor(){
super();
// defining a custom reading option
this.setMeta('$myOption', {
someOption: 10,
});
// ...
}
}
Hiding inputs from readers:
A reader only sees inputs that are capable of serialization
(Input.isSerializable) or visible inputs. Therefore, any input assigned
with the property hidden
is not visible by readers, for instance:
class Example extends Mebo.Action{
constructor(){
super();
this.createInput('readerCantSeeMe: numeric', {hidden: true});
this.createInput('readerSeeMe: numeric');
}
}
Constructor Summary
Public Constructor | ||
public |
constructor(action: Action) Creates a reader. |
Method Summary
Public Methods | ||
public |
Returns the action associated with the reader. |
|
public |
async autofillValues(): Promise<Object> Reads the autofill information and returns it through a plain object. |
|
public |
async inputValues(): Promise<object> Reads the input values and returns it through a plain object. |
|
public |
Returns an option |
|
public |
Sets a value under the options |
|
public |
Returns a list of valid input names that should be used for the parsing. |
Protected Methods | ||
protected |
This method should be re-implemented by derived classes to perform the handler parsing. |
Public Constructors
Public Methods
public async autofillValues(): Promise<Object> source
Reads the autofill information and returns it through a plain object.
If the autofill information is already assigned under autofill (Action.session) then that information is skipped otherwise it adds the parsed information the result.
public async inputValues(): Promise<object> source
Reads the input values and returns it through a plain object.
public option(path: string, defaultValue: *): * source
Returns an option
Params:
Name | Type | Attribute | Description |
path | string | path about where the option is localized (the levels must be separated by '.'). In case of an empty string it returns the entire options |
|
defaultValue | * |
|
default value returned in case a value was not found for the path |
Return:
* |
public setOption(path: string, value: *, merge: boolean) source
Sets a value under the options
Params:
Name | Type | Attribute | Description |
path | string | path about where the option should be stored under the options (the levels must be separated by '.') |
|
value | * | value that is going to be stored under the collection |
|
merge | boolean |
|
this option is used to decide in case of the last level is already existing under the collection, if the value should be either merged (default) or overridden. |
public validInputNames(): Array<string> source
Returns a list of valid input names that should be used for the parsing. This avoids hidden inputs to get exposed in the parsing.
Protected Methods
protected _perform(inputList: Array<Input>): Promise<Object> source
This method should be re-implemented by derived classes to perform the handler parsing.
It should return a plain object containing the input name and the value for that. Where any input value from either String or Array types are considered valid values that are later (Reader.inputValues, Reader.autofillValues) used to parse the value of the input (Input.parseValue), otherwise the value is ignored.
Only return the ones that were found by the parsing. Also, in case of any error during the parsing then an exception should be raised.