Skip to content

Instantly share code, notes, and snippets.

@back2dos
Created January 22, 2013 15:25
Show Gist options
  • Save back2dos/4595458 to your computer and use it in GitHub Desktop.
Save back2dos/4595458 to your computer and use it in GitHub Desktop.
Metadata checking
package ;
@:build(MetaCheck.build())
class Main {
static function main() {
trace(Meta.getStatics(Main).main.AngularSupport);
}
@AngularSupport({ inject:["$scope", '$http'], scope:'$scope' })
function foo() {}
@AngularSupport({ inject:["$scope", '$http'], scfope:'$scope' })//will cause an error
function bar() {}
}
package ;
import haxe.macro.Context;
import haxe.macro.Expr;
using tink.macro.tools.MacroTools;
using StringTools;
class MetaCheck {
static public function build():Array<Field> {
var fields = Context.getBuildFields();
var tags = new Hash<Array<ComplexType>>();
tags.set(
"AngularSupport",
[macro : { inject:Array<String>, scope:String }]
);
for (f in fields)
for (m in f.meta)
if (tags.exists(m.name)) {
var sig = tags.get(m.name).copy();
if (sig.length != m.params.length)
m.pos.error('Expected ${sig.length} arguments but found ${m.params.length} instead');
for (e in m.params) {
//need to escape '$' in string to avoid interpolation
var escaped = e.transform(function (e:Expr)
return
switch (e.expr) {
case EConst(CString(s)):
s.replace('$', '_').toExpr(e.pos);
default: e;
}
);
Context.typeof(ECheckType(escaped, sig.shift()).at(e.pos));
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment