簡體   English   中英

模擬字符串以標記dict

[英]Emulate string to label dict

由於Bazel沒有提供將標簽映射到字符串的方法,所以我想知道如何通過Skylark解決此問題。

遵循我的部分可怕的 “解決方法”。

首先是靜態:

_INDEX_COUNT = 50

def _build_label_mapping():
    lmap = {}

    for i in range(_INDEX_COUNT):
        lmap ["map_name%s" % i] = attr.string()
        lmap ["map_label%s" % i] = attr.label(allow_files = True)

    return lmap

_LABEL_MAPPING = _build_label_mapping()

並在執行中:

    item_pairs = {}
    for i in range(_INDEX_COUNT):
        id = getattr(ctx.attr, "map_name%s" % i)

        if not id:
            continue

        mapl = getattr(ctx.attr, "map_label%s" % i)


        if len(mapl.files):
            item_pairs[id] = list(mapl.files)[0].path
        else:
            item_pairs[id] = ""

    if item_pairs:
        arguments += [
            "--map", str(item_pairs), # Pass json data
        ]

然后是規則:

_foo = rule(
    implementation = _impl,
    attrs = dict({
        "srcs": attr.label_list(allow_files = True, mandatory = True),
    }.items() + _LABEL_MAPPING.items()),

需要包裝如下:

def foo(map={}, **kwargs):

    map_args = {}

    # TODO: Check whether order of items is defined
    for i, item in enumerate(textures.items()):
        key, value = item
        map_args["map_name%s" % i] = key
        map_args["map_label%s" % i] = value

    return _foo(
        **dict(map_args.items() + kwargs.items())
    )

在雲雀中有更好的方法嗎?

要改寫您的問題,您想創建一個從字符串到標簽的規則屬性映射嗎?

當前不支持此功能(請參閱屬性列表 ),但是您可以為此提出功能請求。

您是否認為使用“ label_keyed_string_dict”是合理的解決方法? (如果您有重復的鍵,它將不起作用)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM