Skip to content

Instantly share code, notes, and snippets.

@postite
Forked from nadako/ISkipSerialize.hx
Last active August 29, 2015 14:10
Show Gist options
  • Save postite/c6ef3ec6da0c9d41fd6d to your computer and use it in GitHub Desktop.
Save postite/c6ef3ec6da0c9d41fd6d to your computer and use it in GitHub Desktop.
@:remove
@:autoBuild(SkipSerializeMacro.build())
interface ISkipSerialize {}
class A implements ISkipSerialize {
var a:Int;
var b:String;
@:skip var c:Bool;
}
class Main {
static function main() {
}
}
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