[view] added yaml template support

This commit is contained in:
2018-02-05 17:52:58 +03:00
parent cc6717f46b
commit 823f3ea596
3 changed files with 41 additions and 8 deletions

View File

@@ -2,13 +2,15 @@
"name": "haxework", "name": "haxework",
"url" : "https://bitbucket.org/shmyga/haxework.git", "url" : "https://bitbucket.org/shmyga/haxework.git",
"license": "BSD", "license": "BSD",
"tags": ["flash"], "tags": ["flash", "openfl"],
"description": "Framework.", "description": "Framework.",
"version": "0.6.1", "version": "0.7.0",
"releasenote": "Update.", "releasenote": "Update.",
"contributors": ["shmyga"], "contributors": ["shmyga"],
"classPath": "src/main", "classPath": "src/main",
"dependencies": { "dependencies": {
"promhx": "" "promhx": "1.1.0",
"openfl": "7.0.0",
"yaml": "1.3.0"
} }
} }

View File

@@ -25,7 +25,12 @@ class Builder {
templateFile = Context.resolvePath(templatePath[0]); templateFile = Context.resolvePath(templatePath[0]);
templateKey = templatePath[1]; 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 (templateKey != null) template = Reflect.field(template, templateKey);
if (templateMeta[1] != null) { 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 { private function getValue(name:String, key:String, value:Dynamic, position:JsonKeyPosition):Dynamic {
return if (Std.is(value, Array)) { return if (Std.is(value, Array)) {
value.map(function(v) { value.map(function(v) {
@@ -90,9 +106,9 @@ class Builder {
} else if (Std.is(value, Float) || (Std.is(value, Bool))) { } else if (Std.is(value, Float) || (Std.is(value, Bool))) {
value; value;
} else if (value != null) { } else if (value != null) {
if (Reflect.hasField(value, "@type")) { var type = getType(value, getPosition(position));
if (type != null) {
var n = "a" + i++; var n = "a" + i++;
var type = Reflect.field(value, "@type");
if (type == "Dynamic") { if (type == "Dynamic") {
//ToDo: //ToDo:
exprs.push(Context.parse("var " + n + " = cast {}", getPosition(position))); exprs.push(Context.parse("var " + n + " = cast {}", getPosition(position)));
@@ -102,7 +118,6 @@ class Builder {
createElement(value, n); createElement(value, n);
n; n;
} else { } else {
Context.error("Need @type field", getPosition(position));
null; null;
} }
} else { } else {
@@ -123,7 +138,7 @@ class Builder {
if (Reflect.hasField(template, "id")) { if (Reflect.hasField(template, "id")) {
var id = Reflect.field(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 expr = Context.parse("var a:" + type, getPosition());
var complexType = switch (expr.expr) { var complexType = switch (expr.expr) {
case EVars(vars): vars[0].type; case EVars(vars): vars[0].type;

View File

@@ -1,6 +1,8 @@
package haxework.gui.build; package haxework.gui.build;
#if macro #if macro
import yaml.Parser;
import yaml.Yaml;
import haxe.macro.Context; import haxe.macro.Context;
import haxe.macro.Expr.Constant; import haxe.macro.Expr.Constant;
import haxe.macro.Expr.ExprDef; import haxe.macro.Expr.ExprDef;
@@ -20,6 +22,20 @@ class BuilderUtil {
return json; 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> { public static function getMeta(key:String):Array<String> {
var c = Context.getLocalClass().get(); var c = Context.getLocalClass().get();
for (meta in c.meta.get()) { for (meta in c.meta.get()) {