implmented basic ressource exporting

This commit is contained in:
2023-06-25 19:05:40 +02:00
parent 7996dd062d
commit e686afb298
11 changed files with 277 additions and 0 deletions

View 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);
}
}

View 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>,
}

View File

@@ -0,0 +1,5 @@
package lib.exporter;
interface IExportable {
public function export(): ExportConfig;
}

View 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);
}
});
}
}

View File

@@ -0,0 +1,6 @@
package lib.exporter;
enum Operation {
Get;
Set(value: Dynamic);
}

View 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;
}
}
}

View 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);
}