1

I have following 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"
        }
}

My goal is to access parent from child. Questions are in comments for better context understanding. Thanks

Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122

1 Answers1

2

Note that super is used for object ~inheritance (i.e. when you extend a base object to e.g. override some fields, see https://jsonnet.org/learning/tutorial.html#oo ).

The trick is to plug a local variable pointing to the object's self you want to refer to:

{
        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,
        }
}
jjo
  • 2,595
  • 1
  • 8
  • 16
  • thank you for reply is it possible to also get the name of the key in which this block is wrapped? – Wakan Tanka May 04 '20 at 11:52
  • hm don't think so, haven't seen any _reflection_ / AST tree inspection support, then trying to use "dynamic" keys via `[keyname]` construct as per https://jsonnet.org/learning/tutorial.html#computed_field_names would also not be possible, as those "variable" keynames must be "finalized" reaching the object itself (as per the linked example) – jjo May 04 '20 at 14:35
  • 1
    Well, you could do something like `local fieldname = "foo"; { [fieldname]: { something: fieldname } }`. The local must be defined outside of the object, because the fields need to be known when the object is constructed (and locals inside of the object have access to its internals, so the object must be constructed before they are used). Not sure how useful that would be. – sbarzowski May 04 '20 at 14:50
  • 1
    There are some discussions about adding syntax sugar to make using a field name inside a nested object a little bit nicer: https://github.com/google/go-jsonnet/issues/355. – sbarzowski May 04 '20 at 14:51