从 hiera_hash 创建新数组,但避免嵌套数据。

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

Create new array from hiera_hash, but avoid nested data

问题

我有一个看起来像这样的 hiera_hash:

signalfx_custom_receivers:
  smartagent/http1:
    type: http
    host: "es.mydomain.tv"
    useHTTPS: true
  smartagent/http2:
    type: http
    host: "es.other.tv"
    useHTTPS: true
  smartagent/http3:
    type: http
    host: "pmd-akamai.cdn.mydomain.tv"
    useHTTPS: true

我试图从中生成一个数组以在其他地方使用。这个数组应该只包含 smartagent 的值,所以应该是这样的:

[smartagent/http1, smartagent/http2, smartagent/http3]

我尝试过以下方法:

$signalfx_custom_pipeline   = Array($signalfx_custom_receivers)

然而,这包括了所有嵌套的键/值,最终看起来像这样:

[["smartagent/http1", {"type"="http", "host"="es.mydomain.tv", "useHTTPS"=true}], ["smartagent/http2", {"type"="http", "host"="es.other.tv", "useHTTPS"=true}], ["smartagent/http3", {"type"="http", "host"="pmd-akamai.cdn.mydomain.tv", "useHTTPS"=true}]]

如何生成一个数组,但避免添加这些嵌套值?

英文:

I have a hiera_hash that looks like this:

signalfx_custom_receivers:
  smartagent/http1:
    type: http
    host: "es.mydomain.tv"
    useHTTPS: true
  smartagent/http2:
    type: http
    host: "es.other.tv"
    useHTTPS: true
  smartagent/http3:
    type: http
    host: "pmd-akamai.cdn.mydomain.tv"
    useHTTPS: true

I'm trying to generate an array from this to use elsewhere. The array should only contain smartagent values, so should looks like this:

[smartagent/http1, smartagent/http2, smartagent/http3]

I've tried the following:

$signalfx_custom_pipeline   = Array($signalfx_custom_receivers)

However, this includes all of the nested key/values, and ends up looking like this:

[["smartagent/http1", {"type"=>"http", "host"=>"es.mydomain.tv", "useHTTPS"=>true}], ["smartagent/http2", {"type"=>"http", "host"=>"es.other.tv", "useHTTPS"=>true}], ["smartagent/http3", {"type"=>"http", "host"=>"pmd-akamai.cdn.mydomain.tv", "useHTTPS"=>true}]]

How can I generate an array, but avoid adding these nested values?

答案1

得分: 2

我试图从这里生成一个数组以在其他地方使用。数组应仅包含smartagent值。

也就是说,哈希表的。提取这些键正是内置的keys()函数所做的事情:

$signalfx_custom_pipeline = keys($signalfx_custom_receivers)

# 或者

$signalfx_custom_pipeline = $signalfx_custom_receivers.keys()

这两种形式的行为完全相同,但个人而言,在这种情况下,我觉得第二种更清晰。

英文:

> I'm trying to generate an array from this to use elsewhere. The array should only contain smartagent values

That is, the keys of the hash. Extracting these is exactly what the built-in keys() function does:

$signalfx_custom_pipeline = keys($signalfx_custom_receivers)

# or

$signalfx_custom_pipeline = $signalfx_custom_receivers.keys()

The two forms behave identically, but personally, I find the second clearer in this case.

huangapple
  • 本文由 发表于 2023年6月15日 20:24:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482440.html
匿名

发表评论

匿名网友

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

确定