如何将Terraform字符串集合的字符串表示转换为字符串切片。

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

How to convert the string representation of a Terraform set of strings to a slice of strings

问题

我有一个 terratest,从 terraform 中获取一个输出,形式如下:s := " [a b] "。terraform 输出的 value = toset([resource.name]) 是一个字符串集合。

显然 fmt.Printf("%T", s) 返回的是 string。我需要迭代进行进一步验证。

我尝试了下面的方法,但出现了错误!

var v interface{}
if err := json.Unmarshal([]byte(s), &v); err != nil {
    fmt.Println(err)
}

我当前的实现方法将其转换为切片:

s := "[a b]"
s1 := strings.Fields(strings.Trim(s, "[]"))
for _, v := range s1 {
    fmt.Println("v -> " + v)
}

寻求对当前方法的建议或者转换为数组/切片的其他方法。感谢任何建议。谢谢。

英文:

I've a terratest where I get an output from terraform like so s := "[a b]". The terraform output's value = toset([resource.name]), it's a set of strings.

Apparently fmt.Printf("%T", s) returns string. I need to iterate to perform further validation.

I tried the below approach but errors!

var v interface{}
if err := json.Unmarshal([]byte(s), &v); err != nil {
    fmt.Println(err)
}

My current implementation to convert to a slice is:

s := "[a b]"
s1 := strings.Fields(strings.Trim(s, "[]"))
for _, v:= range s1 {
	fmt.Println("v -> " + v)
}

Looking for suggestions to current approach or alternative ways to convert to arr/slice that I should be considering. Appreciate any inputs. Thanks.

答案1

得分: 3

实际上,你当前的实现看起来很好。

你不能使用JSON解组,因为JSON字符串必须用双引号"括起来。

相反,strings.Fields可以实现这一点,它会根据unicode.IsSpace匹配的一个或多个字符来拆分字符串,这些字符包括\t\n\v\f\r和空格。

此外,根据文档的说明,如果terraform将一个空集合表示为[],这种方法也适用:

> 如果s只包含空白字符,则返回一个空切片。

...这也包括s完全为空的情况""

如果你需要对此进行更多的控制,可以使用strings.FieldsFunc,它接受一个类型为func(rune) bool的函数,因此你可以自己确定什么是“空格”。但由于你的输入字符串来自terraform,我猜它应该足够规范。

可能有第三方包已经实现了这个功能,但除非你的程序已经导入了它们,否则我认为基于标准库的本地解决方案始终是首选。

<hr>

实际上,unicode.IsSpace还包括更高的符文0x850xA0,在这种情况下,strings.Fields会调用FieldsFunc(s, unicode.IsSpace)

英文:

Actually your current implementation seems just fine.

You can't use JSON unmarshaling because JSON strings must be enclosed in double quotes &quot;.

Instead strings.Fields does just that, it splits a string on one or more characters that match unicode.IsSpace, which is \t, \n, \v. \f, \r and .

Moeover this works also if terraform sends an empty set as [], as stated in the documentation:

> returning [...] an empty slice if s contains only white space.

...which includes the case of s being empty &quot;&quot; altogether.

In case you need additional control over this, you can use strings.FieldsFunc, which accepts a function of type func(rune) bool so you can determine yourself what constitutes a "space". But since your input string comes from terraform, I guess it's going to be well-behaved enough.

There may be third-party packages that already implement this functionality, but unless your program already imports them, I think the native solution based on the standard lib is always preferrable.

<hr>

unicode.IsSpace actually includes also the higher runes 0x85 and 0xA0, in which case strings.Fields calls FieldsFunc(s, unicode.IsSpace)

答案2

得分: 0

package main

import (
	"fmt"
	"strings"
)

func main() {
	src := "[a b]"
	dst := strings.Split(src[1:len(src)-1], " ")
	fmt.Println(dst)
}

https://play.golang.org/p/KVY4r_8RWv6

英文:
package main

import (
	&quot;fmt&quot;
	&quot;strings&quot;
)

func main() {
	src := &quot;[a b]&quot;
	dst := strings.Split(src[1:len(src)-1], &quot; &quot;)
	fmt.Println(dst)
}

https://play.golang.org/p/KVY4r_8RWv6

huangapple
  • 本文由 发表于 2021年9月24日 22:18:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/69316552.html
匿名

发表评论

匿名网友

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

确定