implmented basic ressource exporting
This commit is contained in:
35
src/lib/exporter/Export.hx
Normal file
35
src/lib/exporter/Export.hx
Normal file
@@ -0,0 +1,35 @@
|
||||
package lib.exporter;
|
||||
|
||||
import kernel.log.Log;
|
||||
|
||||
using tink.CoreApi;
|
||||
|
||||
class Export {
|
||||
private final exportConfig: ExportConfig;
|
||||
|
||||
public function new(exportConfig: IExportable) {
|
||||
this.exportConfig = exportConfig.export();
|
||||
}
|
||||
|
||||
public function handleRequest(req: Request): Response {
|
||||
switch (req.operation){
|
||||
case Get:
|
||||
return handleGet(req);
|
||||
case Set(value):
|
||||
//TODO: implement
|
||||
return NotFound;
|
||||
}
|
||||
}
|
||||
|
||||
private function handleGet(request: Request): Response {
|
||||
if (!this.exportConfig.getDelegates.exists(request.field)){
|
||||
Log.warn('Requested get field ${request.field} does not exist in ??');
|
||||
return NotFound;
|
||||
}
|
||||
|
||||
var delegate = this.exportConfig.getDelegates.get(request.field);
|
||||
var value = delegate(request.index);
|
||||
|
||||
return Get(value);
|
||||
}
|
||||
}
|
||||
9
src/lib/exporter/ExportConfig.hx
Normal file
9
src/lib/exporter/ExportConfig.hx
Normal file
@@ -0,0 +1,9 @@
|
||||
package lib.exporter;
|
||||
|
||||
import lib.exporter.Response;
|
||||
|
||||
typedef ExportConfig = {
|
||||
type: String,
|
||||
getDelegates: Map<String, Null<Int>->ValueType>,
|
||||
// setDelegates: Map<String, (ValueType, Null<Int>)->ValueType>,
|
||||
}
|
||||
5
src/lib/exporter/IExportable.hx
Normal file
5
src/lib/exporter/IExportable.hx
Normal file
@@ -0,0 +1,5 @@
|
||||
package lib.exporter;
|
||||
|
||||
interface IExportable {
|
||||
public function export(): ExportConfig;
|
||||
}
|
||||
28
src/lib/exporter/Import.hx
Normal file
28
src/lib/exporter/Import.hx
Normal file
@@ -0,0 +1,28 @@
|
||||
package lib.exporter;
|
||||
|
||||
import kernel.net.Net;
|
||||
import kernel.net.Package.NetworkID;
|
||||
|
||||
using tink.CoreApi;
|
||||
|
||||
class Import {
|
||||
public static function get(ressourceLocator: String): Promise<Response> {
|
||||
var request = Request.fromString(ressourceLocator);
|
||||
|
||||
return RessourceNames.get(request.id).next((response)->{
|
||||
return performRequest(response,request);
|
||||
});
|
||||
}
|
||||
|
||||
private static function performRequest(netID: NetworkID, request: Request): Promise<Response> {
|
||||
return Net.instance.sendAndAwait(netID,"res",request).map((response)->{
|
||||
switch (response){
|
||||
case Success(data):
|
||||
return Success(cast (data.data, Response));
|
||||
case Failure(error):
|
||||
return Failure(error);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
6
src/lib/exporter/Operation.hx
Normal file
6
src/lib/exporter/Operation.hx
Normal file
@@ -0,0 +1,6 @@
|
||||
package lib.exporter;
|
||||
|
||||
enum Operation {
|
||||
Get;
|
||||
Set(value: Dynamic);
|
||||
}
|
||||
51
src/lib/exporter/Request.hx
Normal file
51
src/lib/exporter/Request.hx
Normal file
@@ -0,0 +1,51 @@
|
||||
package lib.exporter;
|
||||
|
||||
import lua.TableTools;
|
||||
import lua.NativeStringTools;
|
||||
|
||||
class Request {
|
||||
public final id:String;
|
||||
public final field:String;
|
||||
public final index: Null<Int>;
|
||||
public final operation: Operation;
|
||||
|
||||
public function new(id:String, field:String, index:Null<Int>, operation:Operation) {
|
||||
this.id = id;
|
||||
this.field = field;
|
||||
this.index = index;
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Example:
|
||||
"myfield[2]@myid"
|
||||
"myfield@myid"
|
||||
**/
|
||||
public static function fromString(locator: String): Request {
|
||||
if (StringTools.contains(locator,"[")){
|
||||
var f = TableTools.pack(NativeStringTools.gmatch(locator, "(%a+)%[([%d]+)%]@(%a+)")());
|
||||
|
||||
var field = f[1];
|
||||
var index = Std.parseInt(f[2]);
|
||||
var id = f[3];
|
||||
|
||||
return new Request(id, field, index, Get);
|
||||
}else{
|
||||
var f = TableTools.pack(NativeStringTools.gmatch(locator, "(%a+)@(%a+)")());
|
||||
|
||||
var field = f[1];
|
||||
var id = f[2];
|
||||
|
||||
return new Request(id, field, null, Get);
|
||||
}
|
||||
}
|
||||
|
||||
public function toString() {
|
||||
if (index == null){
|
||||
return field + "@" + id;
|
||||
}else{
|
||||
return field + "[" + index + "]@" + id;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/lib/exporter/Response.hx
Normal file
14
src/lib/exporter/Response.hx
Normal file
@@ -0,0 +1,14 @@
|
||||
package lib.exporter;
|
||||
|
||||
enum Response {
|
||||
NotFound;
|
||||
Set;
|
||||
NotSet;
|
||||
Get(value: ValueType);
|
||||
}
|
||||
|
||||
enum ValueType {
|
||||
Number(value: Int);
|
||||
String(value: String);
|
||||
Bool(value: Bool);
|
||||
}
|
||||
Reference in New Issue
Block a user