Jsonnet: 如何进行字符串格式化和字符串拼接?

huangapple go评论60阅读模式
英文:

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) 来自 < 匿名对象 >
:769:20-24 来自 < 函数 < 匿名 > >
:32:25-26 来自 < 函数 < 匿名 > >
:32:16-27 函数 < 匿名 >
:769:8-25 函数 < 匿名 >


基本上,我想要创建一个等效的字符串,但更易读:

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&#39;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&#39;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 = &#39;someId&#39;;"

"本地变量 dev_account_id = 'someId';"

"local business_service = &#39;someService&#39;;"

"本地变量 business_service = 'someService';"

"local kms_module_dir = &#39;someDir&#39;;"

"本地变量 kms_module_dir = 'someDir';"

"["

"["

"  // Manual string formatting"

"  // 手动字符串格式化"

"  std.format("

"  std.format("

"    &#39;{%s}&#39;,"

"    '{%s}',"

"    std.join(&#39;, &#39;, ["

"    std.join(', ', ["

"      std.format(&#39;&quot;account&quot;: &quot;%s&quot;&#39;, dev_account_id),"

"      std.format('\"account\": \"%s\"', dev_account_id),"

"      std.format(&#39;&quot;service&quot;: &quot;%s&quot;&#39;, business_service),"

"      std.format('\"service\": \"%s\"', business_service),"

"      std.format(&#39;&quot;dir&quot;: &quot;%s&quot;&#39;, kms_module_dir),"

"      std.format('\"dir\": \"%s\"', kms_module_dir),"

"    ])"

"    ])"

"  ),"

"  ),"

"  // Looks like JSON, let&#39;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"

"["

"["

"   &quot;{\&quot;account\&quot;: \&quot;someId\&quot;, \&quot;service\&quot;: \&quot;someService\&quot;, \&quot;dir\&quot;: \&quot;someDir\&quot;}&quot;,"

"   '{\"account\": \"someId\", \"service\": \"someService\", \"dir\": \"someDir\"}',"

"   &quot;{\&quot;account\&quot;:\&quot;someId\&quot;,\&quot;dir\&quot;:\&quot;someDir\&quot;,\&quot;service\&quot;:\&quot;someService\&quot;}&quot;"

"   '{\"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 = &#39;someId&#39;;
local business_service = &#39;someService&#39;;
local kms_module_dir = &#39;someDir&#39;;
[
  // Manual string formatting
  std.format(
    &#39;{%s}&#39;,
    std.join(&#39;, &#39;, [
      std.format(&#39;&quot;account&quot;: &quot;%s&quot;&#39;, dev_account_id),
      std.format(&#39;&quot;service&quot;: &quot;%s&quot;&#39;, business_service),
      std.format(&#39;&quot;dir&quot;: &quot;%s&quot;&#39;, kms_module_dir),
    ])
  ),
  // Looks like JSON, let&#39;s use native function
  std.manifestJsonMinified({
    account: dev_account_id,
    service: business_service,
    dir: kms_module_dir,
  }),
]

output

$ jsonnet foo.jsonnet
[
   &quot;{\&quot;account\&quot;: \&quot;someId\&quot;, \&quot;service\&quot;: \&quot;someService\&quot;, \&quot;dir\&quot;: \&quot;someDir\&quot;}&quot;,
   &quot;{\&quot;account\&quot;:\&quot;someId\&quot;,\&quot;dir\&quot;:\&quot;someDir\&quot;,\&quot;service\&quot;:\&quot;someService\&quot;}&quot;
]

huangapple
  • 本文由 发表于 2023年3月9日 15:56:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定