bufbuild/protoyaml-go
GitHub: bufbuild/protoyaml-go
protoyaml-go 是一个将 Protocol Buffers 消息序列化和反序列化为 YAML 格式的 Go 库,并提供包含精确位置和代码片段的丰富错误信息。
Stars: 66 | Forks: 8
# ProtoYAML
[](https://github.com/bufbuild/protoyaml-go/actions/workflows/ci.yaml)
[](https://goreportcard.com/report/buf.build/go/protoyaml)
[](https://pkg.go.dev/buf.build/go/protoyaml)
将 Protocol Buffers 以 YAML 格式进行序列化和反序列化。提供包含文件、行号、列号和代码片段信息的细粒度错误详情。
与 [protojson](https://github.com/protocolbuffers/protobuf-go/tree/master/encoding/protojson) 完全兼容。
## 用法
```
package main
import (
"log"
"buf.build/go/protoyaml"
)
func main() {
// Marshal a proto message to YAML.
yamlBytes, err := protoyaml.Marshal(
&pb.MyMessage{
MyField: "hello world",
},
)
if err != nil {
log.Fatal(err)
}
// Unmarshal a proto message from YAML.
options := protoyaml.UnmarshalOptions{
Path: "testdata/basic.proto3test.yaml",
}
var myMessage pb.MyMessage
if err := options.Unmarshal(yamlBytes, &myMessage); err != nil {
log.Fatal(err)
}
}
```
ProtoYAML 返回 `nil` 或带有详细信息的错误。对于在文件中发现的每个错误,错误
信息都包含文件名(如果在 `UnmarshalOptions` 中设置了 `Path`)、行号、列号,以及
导致错误的 YAML 代码片段。例如,在反序列化以下 YAML 文件时:
```
values:
- single_bool: true
- single_bool: false
- single_bool: 1
- single_bool: 0
- single_bool: "true"
- single_bool: "false"
- single_bool: True
- single_bool: False
- single_bool: TRUE
- single_bool: FALSE
- single_bool: yes
- single_bool: no
```
将返回以下错误:
```
testdata/basic.proto3test.yaml:5:18: expected bool, got "1"
5 | - single_bool: 1
| .................^
testdata/basic.proto3test.yaml:6:18: expected bool, got "0"
6 | - single_bool: 0
| .................^
testdata/basic.proto3test.yaml:7:18: expected tag !!bool, got !!str
7 | - single_bool: "true"
| .................^
testdata/basic.proto3test.yaml:8:18: expected tag !!bool, got !!str
8 | - single_bool: "false"
| .................^
testdata/basic.proto3test.yaml:9:18: expected bool, got "True"
9 | - single_bool: True
| .................^
testdata/basic.proto3test.yaml:10:18: expected bool, got "False"
10 | - single_bool: False
| .................^
testdata/basic.proto3test.yaml:11:18: expected bool, got "TRUE"
11 | - single_bool: TRUE
| .................^
testdata/basic.proto3test.yaml:12:18: expected bool, got "FALSE"
12 | - single_bool: FALSE
| .................^
testdata/basic.proto3test.yaml:13:18: expected bool, got "yes"
13 | - single_bool: yes
| .................^
testdata/basic.proto3test.yaml:14:18: expected bool, got "no"
14 | - single_bool: no
| .................^
```
只有 `true` 和 `false` 是 `single_bool` 字段的有效值。
更多示例,请参见 [internal/testdata](internal/testdata) 目录。
## 验证
ProtoYAML 可以与外部验证库集成,例如
[Protovalidate](https://github.com/bufbuild/protovalidate-go),以提供更丰富的错误
信息。只需在 `UnmarshalOptions` 中提供一个 `Validator`:
```
package main
import (
"log"
"buf.build/go/protoyaml"
"buf.build/go/protovalidate"
)
func main() {
validator, err := protovalidate.NewValidator()
if err != nil {
log.Fatal(err)
}
var myMessage pb.MyMessage
options := protoyaml.UnmarshalOptions{
Path: "testdata/basic.proto3test.yaml",
Validator: validator,
}
if err := options.Unmarshal(yamlBytes, &myMessage); err != nil {
log.Fatal(err)
}
}
```
由 `Validator` 生成的错误将与 ProtoYAML 的错误一起显示。例如:
```
testdata/validate.validate.yaml:4:18 cases[2].float_gt_lt: value must be greater than 0 and less than 10 (float.gt_lt)
4 | - float_gt_lt: 10.5
| .................^
```
## 状态:Beta
ProtoYAML 尚未稳定。不过,其最终形态不太可能发生剧烈变化——未来的修改会比较微小。
## 法律
基于 [Apache 2 许可证](https://github.com/bufbuild/protoyaml-go/blob/main/LICENSE) 提供
标签:EVTX分析, Go, Protobuf, Ruby工具, YAML, 安全库, 序列化与反序列化, 开发库, 日志审计