从嵌套(子)寻址父元素

Addressing parent elemens from nested (children)

我有以下 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"
        }
}

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

请注意,super 用于对象继承(即当您扩展 base 对象以覆盖某些字段时,请参阅 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,
        }
}