encoding
encoding
命名空间包含编码和解码函数。
encoding.from_base64
encoding.from_base64
函数将符合 RFC4648 规范的 Base64 编码字符串解码为原始字符串。
如果提供的字符串参数包含无效的 Base64 数据,encoding.from_base64
会失败。
示例
> encoding.from_base64("dGFuZ2VyaW5l")
tangerine
encoding.from_URLbase64
encoding.from_URLbase64
函数将符合 RFC4648 规范的 Base64 URL 安全编码字符串解码为原始字符串。
如果提供的字符串参数包含无效的 Base64 数据,encoding.from_URLbase64
会失败。
示例
> encoding.from_URLbase64("c3RyaW5nMTIzIT8kKiYoKSctPUB-")
string123!?$*&()'-=@~
encoding.to_base64
encoding.to_base64
函数将原始字符串编码为符合 RFC4648 规范的 Base64 编码字符串。
示例
> encoding.to_base64("string123!?$*&()'-=@~")
c3RyaW5nMTIzIT8kKiYoKSctPUB+
encoding.to_URLbase64
encoding.to_URLbase64
函数将原始字符串编码为符合 RFC4648 规范的 URL 安全 Base64 编码字符串。
示例
> encoding.to_URLbase64("string123!?$*&()'-=@~")
c3RyaW5nMTIzIT8kKiYoKSctPUB-
encoding.to_json
encoding.to_json
函数将映射编码为 JSON 字符串。如果提供的输入参数无法解析为 JSON 字符串,encoding.to_json
将失败。
encoding.to_json
的一个常见用例是编码预期为 JSON 字符串的组件配置。例如,prometheus.exporter.blackbox
的 config
参数。
示例
> encoding.to_json({"modules"={"http_2xx"={"prober"="http","timeout"="5s","http"={"headers"={"Authorization"=sys.env("TEST_VAR")}}}}})
"{\"modules\":{\"http_2xx\":{\"http\":{\"headers\":{\"Authorization\":\"Hello!\"}},\"prober\":\"http\",\"timeout\":\"5s\"}}}"
encoding.from_json
encoding.from_json
函数将表示 JSON 的字符串解码为 Alloy 值。如果提供的字符串参数无法解析为 JSON,encoding.from_json
将失败。
encoding.from_json
的一个常见用例是将 local.file
组件的输出解码为 Alloy 值。
注意
请记住,在将 JSON 字符串字面量传递给
encoding.from_json
时,要转义双引号。例如,JSON 值
{"key": "value"}
可以正确地表示为字符串"{\"key\": \"value\"}"
。
示例
> encoding.from_json("15")
15
> encoding.from_json("[1, 2, 3]")
[1, 2, 3]
> encoding.from_json("null")
null
> encoding.from_json("{\"key\": \"value\"}")
{
key = "value",
}
> encoding.from_json(local.file.some_file.content)
"Hello, world!"
encoding.from_yaml
encoding.from_yaml
函数将表示 YAML 的字符串解码为 Alloy 值。如果提供的字符串参数无法解析为 YAML,encoding.from_yaml
将失败。
encoding.from_yaml
的一个常见用例是将 local.file
组件的输出解码为 Alloy 值。
注意
请记住,在将 YAML 字符串字面量传递给
encoding.from_yaml
时,要转义双引号。例如,YAML 值
key: "value"
可以正确地表示为字符串"key: \"value\""
。
示例
> encoding.from_yaml("15")
15
> encoding.from_yaml("[1, 2, 3]")
[1, 2, 3]
> encoding.from_yaml("null")
null
> encoding.from_yaml("key: value")
{
key = "value",
}
> encoding.from_yaml(local.file.some_file.content)
"Hello, world!"