Skip to content

Conversation

@cyenxchen
Copy link

概述

本 PR 为 Zedis 添加了 Protobuf 数据格式的检测和解码支持,使用户能够直观地查看存储在 Redis 中的 Protobuf 编码数据。

新增功能

1. 原始 Protobuf 格式自动检测

  • 实现了基于启发式算法的 Protobuf wire format 检测
  • 无需 schema 即可解析和显示原始 Protobuf 数据
  • 自动将解析结果转换为易读的 JSON 格式

2. Schema-based Protobuf 解码

  • 支持加载 .proto 文件(通过 protoc 编译)
  • 使用 prost-reflect 进行运行时反射解码
  • 支持选择具体的 message 类型进行精确解码

3. 状态栏集成

  • 当检测到 Protobuf 格式时,状态栏显示 protobuf 标签
  • 新增 Protobuf 类型选择器(默认 "Rawproto" 为无 schema 模式)
  • 新增文件选择按钮,支持加载 .proto 文件

技术实现

核心模块

文件 功能
src/helpers/protobuf.rs Protobuf wire format 解析器,包含 varint 解码、字段解析、JSON 转换
src/states/server/protobuf.rs ProtobufSchema 状态管理,处理 proto 文件加载、编解码
src/states/server/value.rs 新增 DataFormat::ProtobufRawDataFormat::Protobuf 类型
src/views/status_bar.rs 状态栏 UI 集成,类型选择器和文件加载器

安全边界

  • 递归深度限制(64 层)防止栈溢出
  • 字段大小限制(16MB)防止内存耗尽
  • 有效字段编号验证(1-536870911)
  • 无效 wire type 拒绝(包括已废弃的 Group 类型)

启发式检测策略

为减少误报,实现了以下检测规则:

  • 排除 JSON、XML、纯数字字符串等常见格式
  • 字段编号 > 10000 的消息被视为可疑
  • 单个短字节字段的消息被排除

依赖变更

prost-reflect = { version = "0.14", features = ["serde"] }  # Protobuf 运行时反射
rfd = "0.15"  # 跨平台文件对话框

测试

新增 50 个单元测试,覆盖:

  • decode_varint_safe() 边界测试(6 个)
  • try_parse_raw_protobuf() 解析测试(12 个)
  • is_likely_protobuf() 启发式检测测试(7 个)
  • decode_raw_to_json() JSON 输出测试(11 个)
  • ProtobufSchema 状态管理测试(14 个)
cargo test protobuf
# test result: ok. 50 passed; 0 failed; 4 ignored

使用示例

无 Schema 模式(自动)

当 Redis 值被检测为 Protobuf 格式时,自动以 JSON 形式展示:

{
  "1": 42,
  "2": "hello",
  "3": {
    "1": 100
  }
}

有 Schema 模式

  1. 点击状态栏的文件夹图标加载 .proto 文件
  2. 从下拉菜单选择目标 message 类型
  3. 数据将按照 schema 定义的字段名称显示

截图

(如需要可补充 UI 截图)

Checklist

  • 代码通过 make lint 检查
  • 新增功能包含单元测试
  • 无破坏性变更

…coding

Add protobuf support for Redis value inspection:
- Implement raw protobuf wire format parser with heuristic detection
- Add ProtobufSchema state for loading .proto files via protoc
- Integrate protobuf type selector in status bar with file picker
- Support both raw (schema-less) and schema-based protobuf decoding
- Add comprehensive unit tests (50 tests) for protobuf parsing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant