Created
November 24, 2014 23:25
-
-
Save nadako/440ee3c719019ccbd1eb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@:remove | |
@:autoBuild(SkipSerializeMacro.build()) | |
interface ISkipSerialize {} |
This file contains hidden or 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
class A implements ISkipSerialize { | |
var a:Int; | |
var b:String; | |
@:skip var c:Bool; | |
} | |
class Main { | |
static function main() { | |
} | |
} |
This file contains hidden or 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
import haxe.macro.Context; | |
import haxe.macro.Expr; | |
import haxe.macro.Type; | |
using haxe.macro.Tools; | |
class SkipSerializeMacro { | |
static function build():Array<Field> { | |
var fields = Context.getBuildFields(); | |
var pos = Context.currentPos(); | |
var serializeExprs = []; | |
var unserializeExprs = []; | |
for (field in fields) { | |
if (Lambda.exists(field.meta, function(m) return m.name == ":skip")) | |
continue; | |
var fieldName = field.name; | |
serializeExprs.push(macro s.serialize(this.$fieldName)); | |
unserializeExprs.push(macro this.$fieldName = u.unserialize()); | |
} | |
if (serializeExprs.length > 0) { | |
fields.push({ | |
pos: pos, | |
name: "hxSerialize", | |
access: [APrivate], | |
meta: [{name: ":keep", pos: pos}], | |
kind: FFun({ | |
ret: macro : Void, | |
args: [{name: "s", type: macro : haxe.Serializer}], | |
expr: macro $b{serializeExprs} | |
}) | |
}); | |
fields.push({ | |
pos: pos, | |
name: "hxUnserialize", | |
access: [APrivate], | |
meta: [{name: ":keep", pos: pos}], | |
kind: FFun({ | |
ret: macro : Void, | |
args: [{name: "u", type: macro : haxe.Unserializer}], | |
expr: macro $b{unserializeExprs} | |
}) | |
}); | |
} | |
return fields; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment