Reputation: 1218
$ cat foo.libsonnet
{
local foo = 99,
bar: [ foo, 101 ]
}
$ cat baz.jsonnet
{
local foo = import 'foo.libsonnet',
baz: [foo.foo, foo.bar]
}
$ jsonnet baz.jsonnet
RUNTIME ERROR: field does not exist: foo
baz.jsonnet:3:11-18 thunk <array_element>
baz.jsonnet:3:10-28 object <anonymous>
During manifestation
In this example, it is easy to access bar
field of foo
. Is there any way for baz.jsonnet
to access the locals of foo.libsonnet
?
If the answer is no, how should I implement foo
and baz
so that I can access the foo
field of foo.libsonnet
in both foo.libsonnet
and also in baz.jsonnet
?
Upvotes: 0
Views: 1311
Reputation: 2991
Is there any way for baz.jsonnet to access the locals of foo.libsonnet?
No. Locals are, well, local. They are just names for values available in some lexical scope. In particular object locals can only be accessed directly in the object definition.
If you want to access a value from outside the object, it shouldn't be a local - use a field instead. If you don't want it to be shown when you manifest the object, you can use a hidden field like so:
$ cat foo.libsonnet
{
foo:: 99, // double colon indicates that the field is hidden
bar: [ self.foo, 101 ]
}
$ cat baz.jsonnet
{
local foo = import 'foo.libsonnet',
baz: [foo.foo, foo.bar]
}
$ jsonnet baz.jsonnet
{
"baz": [
99,
[
99,
101
]
]
}
$ jsonnet foo.libsonnet
{
"bar": [
99,
101
]
}
Upvotes: 5
Reputation: 1218
I came up with this:
$ cat foo.libsonnet
{
foo_parameters: {
foo: 98
},
foo_objects: {
local foo = $.foo_parameters,
bar: [ foo.foo, 101 ]
}
}
$ cat baz.jsonnet
{
local foo = import 'foo.libsonnet',
baz: [foo.foo_parameters.foo, foo.foo_objects.bar]
}
$ jsonnet baz.jsonnet
{
"baz": [
98,
[
98,
101
]
]
}
This is essentially the same question and answer.
Upvotes: 0