1

我有以下 jsonnet。

{
        local property = "global variable",

        property: "global property",
        bar: self.property,   // global property
        baz: property,        // global variable

        nested: {
            local property = "local variable",

            property: "local property",
            bar: self.property,       // local property
            baz: property,            // local variable

            // Q1:
            // Can I get the property of parent from here? In my case:
            // property: "global property"
            // I want to use some kind of relative addressing, from child to parent not other way like:
            // $.property
            // I've tried:
            // super.property
            // but got errors

            // Q2:
            // Can I get the name of the key in which this block is wrapped? In my case:
            // "nested"
        }
}

我的目标是从孩子访问父母。问题在评论中,以便更好地理解上下文。谢谢

4

1 回答 1

2

请注意,super它用于对象〜继承(即,当您扩展基础对象以例如覆盖某些字段时,请参阅https://jsonnet.org/learning/tutorial.html#oo)。

诀窍是插入一个指向self您要引用的对象的局部变量:

{
        local property = "global variable",

        property: "global property",
        bar: self.property,   // global property
        baz: property,        // global variable

        // "Plug" a local variable pointing here
        local this = self,

        nested: {
            local property = "local variable",

            property: "local property",
            bar: self.property,       // local property
            baz: property,            // local variable

            // Use variable set at container obj
            glo1: this.property,
            // In this particular case, can also use '$' to refer to the root obj
            glo2: $.property,
        }
}
于 2020-05-04T11:35:41.430 回答