英文:
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还包括更高的符文0x85和0xA0,在这种情况下,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 ".
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 "" 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 (
"fmt"
"strings"
)
func main() {
src := "[a b]"
dst := strings.Split(src[1:len(src)-1], " ")
fmt.Println(dst)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论