Advertisement
Masadow

Trigger reader macro

May 14th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 1.36 KB | None | 0 0
  1. package hello;
  2. import haxe.macro.Context;
  3. import haxe.macro.Expr;
  4.  
  5. /**
  6.  * ...
  7.  * @author Masadow
  8.  */
  9. class Metadata
  10. {
  11.     public static var staticRoutes : Array<String> = new Array<String>();
  12.  
  13.     macro public static function getStaticRoutes() : Expr
  14.     {
  15.         return Context.makeExpr(staticRoutes, Context.currentPos());
  16.     }
  17.  
  18.     macro public static function test() : Array<Field>
  19.     {
  20.         var fields = Context.getBuildFields();
  21.  
  22.         var instanceRoutes = new Array<String>();
  23.         var instanceRoutesField : Field;
  24.        
  25.         for (field in fields)
  26.         {
  27.             for (meta in field.meta)
  28.             {
  29.                 if (meta.name == "route")
  30.                 {
  31.                     //Detect if field is static or not
  32.                     var isStatic = false;
  33.                     for (access in field.access) if (access == AStatic) isStatic = true;
  34.                     //Get the meta param
  35.                     switch (meta.params[0].expr)
  36.                     {
  37.                         case EConst(CString(metavalue)) :
  38.                             //Push the value to the corresponding array
  39.                             if (isStatic)
  40.                                 staticRoutes.push(metavalue);
  41.                             else
  42.                                 instanceRoutes.push(metavalue);
  43.                         default:
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.  
  49.         //Create a field that holds instance routes
  50.         fields.push( {
  51.             access: [APrivate],
  52.             doc: null,
  53.             kind: FVar(macro : Array<String>, Context.makeExpr(instanceRoutes, Context.currentPos())),
  54.             meta: [],
  55.             name: "instanceRoutes",
  56.             pos: Context.currentPos()
  57.         });
  58.  
  59.         return fields;
  60.     }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement