[macro] update ProvideMacro
This commit is contained in:
@@ -3,59 +3,65 @@ package haxework.macro;
|
|||||||
import haxe.macro.Context;
|
import haxe.macro.Context;
|
||||||
import haxe.macro.Expr;
|
import haxe.macro.Expr;
|
||||||
|
|
||||||
|
|
||||||
class ProvideMacro {
|
class ProvideMacro {
|
||||||
|
|
||||||
|
private static var metaName = ":provide";
|
||||||
|
|
||||||
public static function has(field:Field):Bool {
|
public static function has(field:Field):Bool {
|
||||||
return Util.hasFieldMeta(":provide", field);
|
return Util.hasFieldMeta(metaName, field);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private var field:Field;
|
private var field:Field;
|
||||||
|
private var meta:MetadataEntry;
|
||||||
|
|
||||||
public function new(field:Field) {
|
public function new(field:Field) {
|
||||||
this.field = field;
|
this.field = field;
|
||||||
|
this.meta = Util.getFieldMeta(metaName, field);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function apply():Array<Field> {
|
public function apply():Array<Field> {
|
||||||
var result:Array<Field> = [];
|
var result:Array<Field> = [];
|
||||||
var type:ComplexType = switch field.kind {
|
var type:ComplexType = switch field.kind {
|
||||||
case FieldType.FVar(t): t;
|
case FVar(t): t;
|
||||||
default: null;
|
default: null;
|
||||||
}
|
}
|
||||||
var name:String = switch type {
|
var name:String = switch type {
|
||||||
case ComplexType.TPath(p): p.name;
|
case TPath(p): p.name;
|
||||||
default: null;
|
default: null;
|
||||||
}
|
}
|
||||||
|
var provideType:String = meta.params.length == 0 ? null : Util.getExprString(meta.params[0].expr);
|
||||||
var isStatic = Lambda.exists(field.access, function(a: Access) return a == AStatic);
|
var isStatic = Lambda.exists(field.access, function(a: Access) return a == AStatic);
|
||||||
result.push({
|
result.push({
|
||||||
name: field.name,
|
name: field.name,
|
||||||
access: isStatic ? [Access.APublic, Access.AStatic] : [Access.APublic],
|
access: isStatic ? [APublic, AStatic] : [APublic],
|
||||||
pos: field.pos,
|
pos: field.pos,
|
||||||
kind: FieldType.FProp('get', 'set', type)
|
kind: FProp('get', 'set', type),
|
||||||
});
|
});
|
||||||
var access = [Access.APrivate, Access.AInline];
|
var access = [APrivate, AInline];
|
||||||
if (isStatic) access.push(Access.AStatic);
|
if (isStatic) access.push(AStatic);
|
||||||
|
var args = [name];
|
||||||
|
if (provideType != null) args.push('"${provideType}"');
|
||||||
result.push({
|
result.push({
|
||||||
name: 'get_${field.name}',
|
name: 'get_${field.name}',
|
||||||
access: access,
|
access: access,
|
||||||
pos: field.pos,
|
pos: field.pos,
|
||||||
kind: FieldType.FFun({
|
kind: FFun({
|
||||||
args: [],
|
args: [],
|
||||||
expr: Context.parse('return haxework.provider.Provider.get(${name})', field.pos),
|
expr: Context.parse('return haxework.provider.Provider.get(${args.join(',')})', field.pos),
|
||||||
params: [],
|
params: [],
|
||||||
ret: type
|
ret: type,
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
args.insert(1, "value");
|
||||||
result.push({
|
result.push({
|
||||||
name: 'set_${field.name}',
|
name: 'set_${field.name}',
|
||||||
access: access,
|
access: access,
|
||||||
pos: field.pos,
|
pos: field.pos,
|
||||||
kind: FieldType.FFun({
|
kind: FFun({
|
||||||
args: [{name: 'value', type: type}],
|
args: [{name: 'value', type: type}],
|
||||||
expr: Context.parse('{haxework.provider.Provider.set(${name}, value); return value;}', field.pos),
|
expr: Context.parse('{haxework.provider.Provider.set(${args.join(',')}); return value;}', field.pos),
|
||||||
params: [],
|
params: [],
|
||||||
ret: type
|
ret: type,
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -205,10 +205,7 @@ class TemplateMacro {
|
|||||||
for (field in fields) if (field.meta != null) {
|
for (field in fields) if (field.meta != null) {
|
||||||
for (meta in field.meta) {
|
for (meta in field.meta) {
|
||||||
if (meta.name == ':view') {
|
if (meta.name == ':view') {
|
||||||
var viewId:String = meta.params.length == 0 ? field.name : switch meta.params[0].expr {
|
var viewId:String = meta.params.length == 0 ? field.name : Util.getExprString(meta.params[0].expr);
|
||||||
case ExprDef.EConst(Constant.CString(value)): value;
|
|
||||||
default: null;
|
|
||||||
}
|
|
||||||
result.set(viewId, field.name);
|
result.set(viewId, field.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import haxe.macro.Expr;
|
|||||||
class Util {
|
class Util {
|
||||||
|
|
||||||
public static function getMetaParams(meta:MetadataEntry):Array<String> {
|
public static function getMetaParams(meta:MetadataEntry):Array<String> {
|
||||||
return meta.params.map(function(param:Expr) return switch(param.expr) {
|
return meta.params.map(function(param:Expr) return switch param.expr {
|
||||||
case ExprDef.EConst(Constant.CString(value)): value;
|
case EConst(CString(value)): value;
|
||||||
case _: null;
|
case _: null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -30,4 +30,18 @@ class Util {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getFieldMeta(metaName:String, field:Field):MetadataEntry {
|
||||||
|
for (md in field.meta) if (md.name == metaName) {
|
||||||
|
return md;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getExprString(expr:ExprDef) {
|
||||||
|
return switch expr {
|
||||||
|
case EConst(CString(value)): value;
|
||||||
|
case _: null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user