jsonnet 库的继承
Inheritance with jsonnet libraries
我对 JSONNet 的 import
功能有疑问。我 喜欢 的是能够导入一个主 libsonnet
文件,该文件本身由多个导入组成,并且能够从该导入中访问所有内容。
我有以下示例结构:
.
├── library_one
│ └── init.libsonnet
├── library_two
│ └── init.libsonnet
├── init.libsonnet
└── test.jsonnet
每个文件包含以下内容:
library_one/init.libsonnet
{
local LibraryOne = self,
some_function(some_argument='default'):: [
'echo "The argument was %s"' % some_argument,
],
}
library_two/init.libsonnet
{
local LibraryTwo = self,
some_other_function(some_other_argument='another_default'):: [
'echo "The other argument was %s"' % some_other_argument,
],
}
最后,根目录下的 "master" 文件 init.libsonnet:
local library_one = import 'library_one/init.libsonnet';
local library_two = import 'library_two/init.libsonnet';
{}
但是,当我 运行 文件 test.jsonnnet 具有以下内容时:
local master = import 'init.libsonnet';
{
some_key: master.library_one.some_function,
some_other_key: master.library_two.some_other_function,
}
我收到错误:
RUNTIME ERROR: field does not exist: library_one
test.jsonnet:4:13-31 object <anonymous>
During manifestation
这种继承方式不行吗?
local
符号不会导出,因为本身没有 "global" 作用域,而是 {...}
主对象的字段:
Modified source to use main object fields:
::::::::::::::
library_one/init.libsonnet
::::::::::::::
{
local LibraryOne = self,
some_function(some_argument='default'):: [
'echo "The argument was %s"' % some_argument,
],
}
::::::::::::::
library_two/init.libsonnet
::::::::::::::
{
local LibraryTwo = self,
some_other_function(some_other_argument='another_default'):: [
'echo "The other argument was %s"' % some_other_argument,
],
}
::::::::::::::
init.libsonnet
::::::::::::::
{
library_one:: import 'library_one/init.libsonnet',
library_two:: import 'library_two/init.libsonnet',
}
::::::::::::::
test.jsonnet
::::::::::::::
local master = import 'init.libsonnet';
{
some_key:: master.library_one.some_function,
some_other_key:: master.library_two.some_other_function,
foo: $.some_key("bar")
}
Sample output:
{
"foo": [
"echo \"The argument was bar\""
]
}
请先看看@jjo的回答
我只是想补充一点,构建您的库是可能且合理的,这样您就可以拥有本地 "declarations" 然后使用一个对象拥有它 "exported",我认为这类似于你在描述什么。
library.libsonnet:
local sqr(x) = x * x;
local cube(x): x * x * x;
{
sqr: sqr,
cube: cube,
}
然后你可以这样使用:
local lib = import 'library.libsonnet';
[
lib.sqr(2),
lib.cube(3) + lib.sqr(4),
]
这种样式对于性能也非常好。您可以在此处查看真实示例:https://github.com/sbarzowski/jsonnet-modifiers/blob/master/modifiers.libsonnet.
至于 "master library",您实际上可以在 init.libsonnet:
中将这些部分加在一起
local library_one = import 'library_one/init.libsonnet';
local library_two = import 'library_two/init.libsonnet';
library_one + library_two
如果library_one和library_two包含相同的字段,则library_two优先。您可以在官网阅读更多关于 Jsonnet 中的继承规则:https://jsonnet.org/learning/tutorial.html#oo。
我对 JSONNet 的 import
功能有疑问。我 喜欢 的是能够导入一个主 libsonnet
文件,该文件本身由多个导入组成,并且能够从该导入中访问所有内容。
我有以下示例结构:
.
├── library_one
│ └── init.libsonnet
├── library_two
│ └── init.libsonnet
├── init.libsonnet
└── test.jsonnet
每个文件包含以下内容:
library_one/init.libsonnet
{
local LibraryOne = self,
some_function(some_argument='default'):: [
'echo "The argument was %s"' % some_argument,
],
}
library_two/init.libsonnet
{
local LibraryTwo = self,
some_other_function(some_other_argument='another_default'):: [
'echo "The other argument was %s"' % some_other_argument,
],
}
最后,根目录下的 "master" 文件 init.libsonnet:
local library_one = import 'library_one/init.libsonnet';
local library_two = import 'library_two/init.libsonnet';
{}
但是,当我 运行 文件 test.jsonnnet 具有以下内容时:
local master = import 'init.libsonnet';
{
some_key: master.library_one.some_function,
some_other_key: master.library_two.some_other_function,
}
我收到错误:
RUNTIME ERROR: field does not exist: library_one
test.jsonnet:4:13-31 object <anonymous>
During manifestation
这种继承方式不行吗?
local
符号不会导出,因为本身没有 "global" 作用域,而是 {...}
主对象的字段:
Modified source to use main object fields:
::::::::::::::
library_one/init.libsonnet
::::::::::::::
{
local LibraryOne = self,
some_function(some_argument='default'):: [
'echo "The argument was %s"' % some_argument,
],
}
::::::::::::::
library_two/init.libsonnet
::::::::::::::
{
local LibraryTwo = self,
some_other_function(some_other_argument='another_default'):: [
'echo "The other argument was %s"' % some_other_argument,
],
}
::::::::::::::
init.libsonnet
::::::::::::::
{
library_one:: import 'library_one/init.libsonnet',
library_two:: import 'library_two/init.libsonnet',
}
::::::::::::::
test.jsonnet
::::::::::::::
local master = import 'init.libsonnet';
{
some_key:: master.library_one.some_function,
some_other_key:: master.library_two.some_other_function,
foo: $.some_key("bar")
}
Sample output:
{
"foo": [
"echo \"The argument was bar\""
]
}
请先看看@jjo的回答
我只是想补充一点,构建您的库是可能且合理的,这样您就可以拥有本地 "declarations" 然后使用一个对象拥有它 "exported",我认为这类似于你在描述什么。
library.libsonnet:
local sqr(x) = x * x;
local cube(x): x * x * x;
{
sqr: sqr,
cube: cube,
}
然后你可以这样使用:
local lib = import 'library.libsonnet';
[
lib.sqr(2),
lib.cube(3) + lib.sqr(4),
]
这种样式对于性能也非常好。您可以在此处查看真实示例:https://github.com/sbarzowski/jsonnet-modifiers/blob/master/modifiers.libsonnet.
至于 "master library",您实际上可以在 init.libsonnet:
中将这些部分加在一起local library_one = import 'library_one/init.libsonnet';
local library_two = import 'library_two/init.libsonnet';
library_one + library_two
如果library_one和library_two包含相同的字段,则library_two优先。您可以在官网阅读更多关于 Jsonnet 中的继承规则:https://jsonnet.org/learning/tutorial.html#oo。