从嵌套(子级)寻址父级元素

问题描述 投票:1回答: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"
        }
}

我的目标是从孩子那里访问父母。为了更好地理解上下文,在注释中添加了问题。谢谢

json jsonpath jsonnet
1个回答
0
投票

请注意,super用于对象的继承关系(即,当您扩展base对象以覆盖例如某些字段时,请参见https://jsonnet.org/learning/tutorial.html#oo。]

技巧是plug指向您要引用的对象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,
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.