Haxe自定义元数据到宏调用 [英] Haxe Custom Metadata to Macro Call

查看:93
本文介绍了Haxe自定义元数据到宏调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我已经创建了一个可以像这样使用的构建宏

Let's say that I've created a build macro that can be used like so

@:build(macros.SampleMacro.build("arg"))
class Main {}

是否可以将其转换为自定义的速记元数据?

Is it possible to convert it into a custom, shorthand metadata?

@:samplemacro("arg")
class Main {}

关于此的任何文档吗?

推荐答案

经过很多绊脚石,我发现可以这样做.

After much stumbling, I've figured out that it is possible to do.

解决方案的一部分是使用

Part of the solution is to use

--macro addGlobalMetadata('$path', '@:build(Build.build())')

这使您可以为$ path中的所有类分配一个@:build函数.可以用作编译器选项或haxeflag.

This lets you assign a @:build function to all of the classes in the $path. This can be used as either a compiler option or a haxeflag.

但是,仅凭它本身不足以采用带有动态参数的元数据标签.但是,因为我们现在有一个Build.build()可以对我们所有的类执行 ,所以那个 Build.build()函数都可以处理检查哪些类具有我们的自定义元数据标签,以及为我们可能需要的类构建任何内容.

But that on its own is not enough to adopt metadata tags that take dynamic arguments. But because we now have a Build.build() that executes for all of our classes, that Build.build() function can both handle checking which classes have our custom metadata tag(s), as well as build anything for those classes we may need.

要检查自定义元数据标签,请按以下步骤设置检查宏:

To check for our custom metadata tags, we set up our checking macro as follows:

class Build {

    static var META_STR:String = ":samplemacro";

    macro static public function build():Array<Field> {

        // We check only those Contexts for where a class type exists
        var localClass:Null<Ref<ClassType>> = Context.getLocalClass();
        if(localClass == null) return null; // no class type

        // We check if the metadata for the class contains our 
        // custom metadata string at all
        if(!localClass.get().meta.has(META_STR)) return null;

        // This class does have our custom metadata!
        // Because there may be more than one of the same type
        // of metadata, we extract a list of all the custom metadata tags

        var customTags = localClass.get().meta.extract(META_STR);

        // For each tag we can get at the arguments passed in 
        // by accessing the params field
        for(customTag in customTags){
            var params = customTag.params;

            // Here we can handle each of our @:samplemacro(params) tags,
            // save the params for use later, or 
            // pass the arguments over to another class to deal with
        }

        var fields = Context.getBuildFields();

        // Modify the class fields the way you want

        // Optionally destroy the metadata tags afterwards
        // with localClass.get().meta.remove(META_STR);

        return fields;
    }
}

有关addGlobalMetadata的更多详细信息,请参见: https://stackoverflow.com/a/38075492/1502818

More details on addGlobalMetadata can be found at: https://stackoverflow.com/a/38075492/1502818

这篇关于Haxe自定义元数据到宏调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆