diff options
Diffstat (limited to 'src/utils/macro/Macro.hx')
| -rw-r--r-- | src/utils/macro/Macro.hx | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/utils/macro/Macro.hx b/src/utils/macro/Macro.hx new file mode 100644 index 0000000..118f0e8 --- /dev/null +++ b/src/utils/macro/Macro.hx @@ -0,0 +1,35 @@ +package utils.macro; + +import haxe.macro.Context; +import haxe.macro.Expr; + +using haxe.macro.Tools; + +class Macro { + macro public static function getTypedObject(obj:Expr, typePath:Expr):Expr { + final type = Context.getType(typePath.toString()); + switch (type.follow()) { + case TAnonymous(_.get() => td): + final name = obj.toString(); + if (obj.expr.match(EObjectDecl(_))) { + throw new Error('$name should be passed as reference (inside of variable)', obj.pos); + } + final e:Expr = { + expr: EObjectDecl([ + for (field in td.fields) { + field: field.name, + expr: macro $p{['$name', '${field.name}']}, + } + ]), + pos: Context.currentPos() + } + return e; + default: + throw new Error(type.toString() + " should be typedef structure", typePath.pos); + } + } + + macro public static function getBuildTime():Expr { + return macro $v{Date.now().toString()}; + } +} |
