Created
July 2, 2016 13:52
-
-
Save thesunlover/a70db42bd9bd1343e3a3d31a6c304ae7 to your computer and use it in GitHub Desktop.
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; | |
import js.Lib; | |
/** | |
* ... | |
* @author Iskren Stanislavov | |
*/ | |
@:build(MyMacro.build("sample")) | |
class Main | |
{ | |
var a:Int = 4; | |
var b:Int = 7; | |
static function main() | |
{ | |
//trace(this.json); | |
// | |
var me = new Main(); | |
} | |
public function new() | |
{ | |
trace(json); | |
this.json.objectInObject; | |
trace(json.arrayOfInts); | |
if (!Reflect.hasField(this, "winSymbolIds")) | |
{ | |
throw "Not yet"; | |
} | |
trace("Horray"); | |
} | |
} |
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; | |
import haxe.Http; | |
import haxe.Json; | |
import haxe.macro.ComplexTypeTools; | |
import haxe.macro.Context; | |
import haxe.macro.Expr; | |
#if sys | |
import sys.io.File; | |
#end | |
import haxe.macro.Type.TFunc; | |
/** | |
* ... | |
* author Isko | |
*/ | |
class MyMacro | |
{ | |
static public function build(jsonPath:String = ""):Array<Field> | |
{ | |
var fields:Array<Field> = Context.getBuildFields(); | |
#if sys | |
jsonPath = jsonPath + ".json"; | |
var json = File.getContent(Context.resolvePath(jsonPath)); | |
#end | |
var expr:Expr = Json.parse(json); | |
//var jsonFields:Array<Field> = generateSubFields1(expr); | |
//var jsonFields:Array<Field> = generateSubFields2(expr); | |
var jsonFields:Array<Field> = generateSubFields3(expr); | |
trace(jsonFields); | |
//trace(Context.makeExpr(json, Context.currentPos())); | |
fields.push({ | |
name : "json", //gids | |
pos : Context.currentPos(), | |
kind : FVar(TAnonymous( jsonFields )),//gtype | |
access: [APrivate], | |
}); | |
return fields; | |
} | |
#if macro | |
private static function generateSubFields1(json:Dynamic):Array<Field> | |
{ | |
var _pos: Position = Context.currentPos(); | |
return [ for (childName in Reflect.fields(json)) { | |
"name": childName, | |
"pos" : _pos, | |
"kind": FVar(macro:Dynamic) | |
}]; | |
} | |
private static function generateSubFields2(json:Dynamic):Array<Field> | |
{ | |
var _pos: Position = Context.currentPos(); | |
var fields:Array<Field> = []; | |
for (childName in Reflect.fields(json)) | |
{ | |
var child:Dynamic = Reflect.field(json, childName); | |
var childType = Type.typeof(child); | |
//TODO: | |
//var jsonRepr:String = Json.stringify(child); | |
var classOfChild = Type.getClass(child); | |
trace(childName,classOfChild); | |
switch(classOfChild){ | |
case Array: | |
var arrType = Type.typeof(child[0]); | |
fields.push({ | |
"name": childName, | |
"pos" : _pos, | |
//"kind": FVar(macro:Dynamic) | |
//"kind": FVar(TAnonymous(generateSubFields2(child))), | |
//"kind": TAnonymous(generateSubFields2(child)), | |
//"kind": childType.toString(), | |
//"kind": FVar(macro {childType.toString(); }), | |
//"kind": FVar(macro Array<$v{arrType}>) | |
"kind": FVar(macro: Array<Dynamic>) | |
}); | |
default: | |
fields.push({ | |
"name": childName, | |
"pos" : _pos, | |
//"kind": childType, | |
"kind": FVar(macro:Dynamic) | |
}); | |
} | |
} | |
return fields; | |
//var jsonType:ComplexType = TAnonymous(fields); | |
//return jsonType; | |
} | |
private static function generateSubFields3(json:Expr):Array<Field> | |
{ | |
var fields:Array<Field> = []; | |
var _pos: Position = Context.currentPos(); | |
var fields:Array<Field> = []; | |
for (childName in Reflect.fields(json)) | |
{ | |
var child:Dynamic = Reflect.field(json, childName); | |
var childType = Type.typeof(child); | |
//TODO: | |
var jsonRepr:String = Json.stringify(child); | |
var classOfChild = Type.getClass(child); | |
switch(classOfChild){ | |
case Array: | |
//var arrType = getTypeOfArray(child); | |
//trace(new String(""+arrType)); | |
fields.push({ | |
"name": childName, | |
"pos" : _pos, | |
//"kind": arrType | |
//"kind": FVar(TAnonymous(generateSubFields2(child))), | |
//"kind": TAnonymous(generateSubFields2(child)), | |
//"kind": childType.toString(), | |
//"kind": FVar(macro {childType.expr; }), | |
//"kind": FVar(null, macro $v(jsonRepr)), | |
"kind": FVar(EArrayDecl([for (e in child.expr) e ])) | |
}); | |
default: | |
fields.push({ | |
"name": childName, | |
"pos" : _pos, | |
//"kind": childType, | |
"kind": FVar(macro:Dynamic) | |
}); | |
} | |
} | |
trace(fields); | |
return fields; | |
//var jsonType:ComplexType = TAnonymous(fields); | |
//return jsonType; | |
} | |
#end | |
} |
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
{"floatVar1":0.1, "str":"some string", "nullValueObject":null, "arrayOfInts":[11,20,9], "matrixLikeArray":[[14, 12, 13, 11, 18]], "floatMatrix":[[14.4, 12.3, 13.7, 11.9, 18.0]], "symbolPayouts":[0.05], "objectInObject":{"prop1":"some str", "prop2": "some str2", "prop3":10.17, "prop4":[[1,2,3],[19.3,20.4]]}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment