Created
April 10, 2015 07:14
-
-
Save nitrobin/8c5f724ec2da8e88ff73 to your computer and use it in GitHub Desktop.
haxe.macro.Context.definedValue helper (place defined values into code, i.e. -DsetLanguage=ru)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ; | |
class MacroUtils { | |
public function new() { | |
} | |
/** Подставляет на этапе компиляции в код значение переданное компилятору через флаг -D (Например: -DsetLanguage=ru )*/ | |
macro public static function getDefinedValue(key:haxe.macro.Expr, defaultValue:haxe.macro.Expr) { | |
function getString(v:haxe.macro.Expr):String { | |
return switch(v.expr) { | |
case EConst(CString(str)): | |
str; | |
default: | |
null; | |
} | |
} | |
var _key:String = getString(key); | |
var _defaultValue:String = getString(defaultValue); | |
var value = haxe.macro.Context.definedValue(_key); | |
if (value == null) { | |
return haxe.macro.Context.makeExpr(_defaultValue, haxe.macro.Context.currentPos()); | |
} else { | |
return haxe.macro.Context.makeExpr(value, haxe.macro.Context.currentPos()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment