[view] added yaml template support
This commit is contained in:
@@ -2,13 +2,15 @@
|
||||
"name": "haxework",
|
||||
"url" : "https://bitbucket.org/shmyga/haxework.git",
|
||||
"license": "BSD",
|
||||
"tags": ["flash"],
|
||||
"tags": ["flash", "openfl"],
|
||||
"description": "Framework.",
|
||||
"version": "0.6.1",
|
||||
"version": "0.7.0",
|
||||
"releasenote": "Update.",
|
||||
"contributors": ["shmyga"],
|
||||
"classPath": "src/main",
|
||||
"dependencies": {
|
||||
"promhx": ""
|
||||
"promhx": "1.1.0",
|
||||
"openfl": "7.0.0",
|
||||
"yaml": "1.3.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,12 @@ class Builder {
|
||||
templateFile = Context.resolvePath(templatePath[0]);
|
||||
templateKey = templatePath[1];
|
||||
|
||||
template = BuilderUtil.loadJsonFile(templateFile);
|
||||
var ext = templateFile.split('.').pop();
|
||||
template = switch(ext) {
|
||||
case 'json': BuilderUtil.loadJsonFile(templateFile);
|
||||
case 'yml' | 'yaml': BuilderUtil.loadYamlFile(templateFile);
|
||||
case x: throw 'Unsupported template format: "${x}"';
|
||||
}
|
||||
if (templateKey != null) template = Reflect.field(template, templateKey);
|
||||
|
||||
if (templateMeta[1] != null) {
|
||||
@@ -74,6 +79,17 @@ class Builder {
|
||||
}
|
||||
}
|
||||
|
||||
private function getType(value:Dynamic, position:Position):String {
|
||||
if (Reflect.hasField(value, "@type")) {
|
||||
return Reflect.field(value, "@type");
|
||||
} else if (Reflect.hasField(value, "$type")) {
|
||||
return Reflect.field(value, "$type");
|
||||
} else {
|
||||
Context.error("Need @type field", position);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function getValue(name:String, key:String, value:Dynamic, position:JsonKeyPosition):Dynamic {
|
||||
return if (Std.is(value, Array)) {
|
||||
value.map(function(v) {
|
||||
@@ -90,9 +106,9 @@ class Builder {
|
||||
} else if (Std.is(value, Float) || (Std.is(value, Bool))) {
|
||||
value;
|
||||
} else if (value != null) {
|
||||
if (Reflect.hasField(value, "@type")) {
|
||||
var type = getType(value, getPosition(position));
|
||||
if (type != null) {
|
||||
var n = "a" + i++;
|
||||
var type = Reflect.field(value, "@type");
|
||||
if (type == "Dynamic") {
|
||||
//ToDo:
|
||||
exprs.push(Context.parse("var " + n + " = cast {}", getPosition(position)));
|
||||
@@ -102,7 +118,6 @@ class Builder {
|
||||
createElement(value, n);
|
||||
n;
|
||||
} else {
|
||||
Context.error("Need @type field", getPosition(position));
|
||||
null;
|
||||
}
|
||||
} else {
|
||||
@@ -123,7 +138,7 @@ class Builder {
|
||||
|
||||
if (Reflect.hasField(template, "id")) {
|
||||
var id = Reflect.field(template, "id");
|
||||
var type = Reflect.field(template, "@type");
|
||||
var type = getType(template, getPosition());
|
||||
var expr = Context.parse("var a:" + type, getPosition());
|
||||
var complexType = switch (expr.expr) {
|
||||
case EVars(vars): vars[0].type;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package haxework.gui.build;
|
||||
#if macro
|
||||
|
||||
import yaml.Parser;
|
||||
import yaml.Yaml;
|
||||
import haxe.macro.Context;
|
||||
import haxe.macro.Expr.Constant;
|
||||
import haxe.macro.Expr.ExprDef;
|
||||
@@ -20,6 +22,20 @@ class BuilderUtil {
|
||||
return json;
|
||||
}
|
||||
|
||||
public static function loadYamlFile(path:String) {
|
||||
Context.registerModuleDependency(Context.getLocalModule(), path);
|
||||
var content = sys.io.File.getContent(path);
|
||||
var result = null;
|
||||
try {
|
||||
// ToDo: extract poisiton info
|
||||
result = Yaml.parse(content, Parser.options().useObjects());
|
||||
} catch(error:Dynamic) {
|
||||
Context.error(error, Context.makePosition({min:0, max:0, file:path}));
|
||||
}
|
||||
//Context.parse(content, Context.makePosition({min:0, max:0, file:path}));
|
||||
return result;
|
||||
}
|
||||
|
||||
public static function getMeta(key:String):Array<String> {
|
||||
var c = Context.getLocalClass().get();
|
||||
for (meta in c.meta.get()) {
|
||||
|
||||
Reference in New Issue
Block a user