英文:
Jsonnet: How to do String Formatting + String Joining together?
问题
我想要使这个表达更加可读,以便在需要时更容易添加新变量,所以我尝试写了类似这样的内容:
std.format('{%s}',
std.join(', ', [
std.format('"account": "%s"', dev_account_id),
std.format('"service": "%s"', business_service),
std.format('"dir": "%s"', kms_module_dir)
])
)
不幸的是,我遇到了这个错误:
app_metaflow/shore/main.pipeline.jsonnet:(58:19)-(72:8) 匿名对象
在展现期间
运行时错误: 缺少参数: vals
/mnt/data/jenkins/workspace/d-platform_fusmltrn-app_metaflow/shore/main.pipeline.jsonnet:63:9-35 来自 < 来自 < 来自 < 匿名对象 > > >
/mnt/data/jenkins/workspace/d-platform_fusmltrn-app_metaflow/shore/main.pipeline.jsonnet:(59:9)-(71:11) 来自 < 匿名对象 >
基本上,我想要创建一个等效的字符串,但更易读:
std.format('{ "account": "%s", "service": "%s", "dir": "%s" }', [dev_account_id, business_service, kms_module_dir])
<details>
<summary>英文:</summary>
I want to make this expression more readable so that it's easier to add new variables if needed, so I tried writing something like this:
std.format('{%s}',
std.join(', ', [
std.format('"account": "%s"', dev_account_id),
std.format('"service": "%s"', business_service),
std.format('"dir": "%s"', kms_module_dir)
])
)
Unfortunately, I'm getting this error:
app_metaflow/shore/main.pipeline.jsonnet:(58:19)-(72:8)\tobject <anonymous>\n\tDuring manifestation\t\n"
RUNTIME ERROR: Missing argument: vals
/mnt/data/jenkins/workspace/d-platform_fusmltrn-app_metaflow/shore/main.pipeline.jsonnet:63:9-35 thunk from <thunk from <thunk from <object <anonymous>>>>
/mnt/data/jenkins/workspace/d-platform_fusmltrn-app_metaflow/shore/main.pipeline.jsonnet:(59:9)-(71:11) thunk from <object <anonymous>>
<std>:769:20-24 thunk from <function <anonymous>>
<std>:32:25-26 thunk from <function <anonymous>>
<std>:32:16-27 function <anonymous>
<std>:769:8-25 function <anonymous>
I basically want to create a string equivalent of this, but more readable
std.format('{"account": "%s", "service": "%s", "dir": "%s"}', [dev_account_id, business_service, kms_module_dir])
</details>
# 答案1
**得分**: 1
以下是翻译的部分:
"Interesting, as your example did work for me (using `jsonnet` `v0.19.1`), nevertheless it _looks_ like your building a JSON string, thus it may be worth using native function(s) to do so."
"你的示例对我有效(使用 `jsonnet` `v0.19.1`),但看起来你正在构建一个JSON字符串,因此值得考虑使用本机函数来完成。"
"Below example shows your implementation, together with a JSON- native approach:"
"下面的示例展示了你的实现,以及一个本机的JSON方法:"
"src: foo.jsonnet"
"源码:foo.jsonnet"
"local dev_account_id = 'someId';"
"本地变量 dev_account_id = 'someId';"
"local business_service = 'someService';"
"本地变量 business_service = 'someService';"
"local kms_module_dir = 'someDir';"
"本地变量 kms_module_dir = 'someDir';"
"["
"["
" // Manual string formatting"
" // 手动字符串格式化"
" std.format("
" std.format("
" '{%s}',"
" '{%s}',"
" std.join(', ', ["
" std.join(', ', ["
" std.format('"account": "%s"', dev_account_id),"
" std.format('\"account\": \"%s\"', dev_account_id),"
" std.format('"service": "%s"', business_service),"
" std.format('\"service\": \"%s\"', business_service),"
" std.format('"dir": "%s"', kms_module_dir),"
" std.format('\"dir\": \"%s\"', kms_module_dir),"
" ])"
" ])"
" ),"
" ),"
" // Looks like JSON, let's use native function"
" // 看起来像JSON,让我们使用本机函数"
" std.manifestJsonMinified({"
" std.manifestJsonMinified({"
" account: dev_account_id,"
" account: dev_account_id,"
" service: business_service,"
" service: business_service,"
" dir: kms_module_dir,"
" dir: kms_module_dir,"
" }),"
" }),"
"]"
"]"
"output"
"输出"
"lang-shell"
"lang-shell"
"$ jsonnet foo.jsonnet"
"$ jsonnet foo.jsonnet"
"["
"["
" "{\"account\": \"someId\", \"service\": \"someService\", \"dir\": \"someDir\"}","
" '{\"account\": \"someId\", \"service\": \"someService\", \"dir\": \"someDir\"}',"
" "{\"account\":\"someId\",\"dir\":\"someDir\",\"service\":\"someService\"}""
" '{\"account\":\"someId\",\"dir\":\"someDir\",\"service\":\"someService\"}',"
"]"
"]"
<details>
<summary>英文:</summary>
Interesting, as your example did work for me (using `jsonnet` `v0.19.1`), nevertheless it _looks_ like your building a JSON string, thus it may be worth using native function(s) to do so.
Below example shows your implementation, together with a JSON- native approach:
## src: foo.jsonnet
``` lang-js
local dev_account_id = 'someId';
local business_service = 'someService';
local kms_module_dir = 'someDir';
[
// Manual string formatting
std.format(
'{%s}',
std.join(', ', [
std.format('"account": "%s"', dev_account_id),
std.format('"service": "%s"', business_service),
std.format('"dir": "%s"', kms_module_dir),
])
),
// Looks like JSON, let's use native function
std.manifestJsonMinified({
account: dev_account_id,
service: business_service,
dir: kms_module_dir,
}),
]
output
$ jsonnet foo.jsonnet
[
"{\"account\": \"someId\", \"service\": \"someService\", \"dir\": \"someDir\"}",
"{\"account\":\"someId\",\"dir\":\"someDir\",\"service\":\"someService\"}"
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论