refactor: 将 OpType 字段从枚举类型改为 string 类型

主要变更:
- Operation.OpType: Type → string
- NewFullOperation 参数: opType Type → opType string
- IsValidOpType 参数: opType Type → opType string
- operationMeta.OpType: *Type → *string
- queryclient.ListRequest.OpType: model.Type → string

优点:
- 更灵活,支持动态扩展操作类型
- 不再受限于预定义的枚举常量
- 简化类型转换逻辑

兼容性:
- Type 常量定义保持不变 (OpTypeCreate, OpTypeUpdate 等)
- 使用时需要 string() 转换: string(model.OpTypeCreate)
- 所有单元测试已更新并通过 (100%)

测试结果:
 api/adapter - PASS
 api/highclient - PASS
 api/logger - PASS
 api/model - PASS
 api/persistence - PASS
 api/queryclient - PASS
 internal/* - PASS
This commit is contained in:
ryan
2025-12-24 16:48:00 +08:00
parent 4b72a37120
commit a90d853a6e
16 changed files with 52 additions and 52 deletions

View File

@@ -34,7 +34,7 @@ func TestOperation_CheckAndInit(t *testing.T) {
op: &model.Operation{
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: model.OpTypeOCCreateHandle,
OpType: string(model.OpTypeOCCreateHandle),
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -48,7 +48,7 @@ func TestOperation_CheckAndInit(t *testing.T) {
OpID: "", // Will be auto-generated
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: model.OpTypeOCCreateHandle,
OpType: string(model.OpTypeOCCreateHandle),
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -62,7 +62,7 @@ func TestOperation_CheckAndInit(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: model.OpTypeOCCreateHandle,
OpType: string(model.OpTypeOCCreateHandle),
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -77,7 +77,7 @@ func TestOperation_CheckAndInit(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: model.OpTypeOCCreateHandle,
OpType: string(model.OpTypeOCCreateHandle),
DoPrefix: "test",
DoRepository: "repo",
Doid: "invalid/123", // Doesn't start with "test/repo"
@@ -222,7 +222,7 @@ func TestOperation_MarshalUnmarshalBinary(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: model.OpTypeOCCreateHandle,
OpType: string(model.OpTypeOCCreateHandle),
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -260,7 +260,7 @@ func TestOperation_MarshalBinary_Empty(t *testing.T) {
op := &model.Operation{
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: model.OpTypeOCCreateHandle,
OpType: string(model.OpTypeOCCreateHandle),
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -297,7 +297,7 @@ func TestOperation_DoHash(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: model.OpTypeOCCreateHandle,
OpType: string(model.OpTypeOCCreateHandle),
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -324,7 +324,7 @@ func TestOperationHashData(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: model.OpTypeOCCreateHandle,
OpType: string(model.OpTypeOCCreateHandle),
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -362,7 +362,7 @@ func TestOperation_MarshalTrustlog_EmptyProducerID(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: model.OpTypeOCCreateHandle,
OpType: string(model.OpTypeOCCreateHandle),
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -388,7 +388,7 @@ func TestOperation_MarshalTrustlog_NilSigner(t *testing.T) {
OpID: "op-123",
Timestamp: time.Now(),
OpSource: model.OpSourceIRP,
OpType: model.OpTypeOCCreateHandle,
OpType: string(model.OpTypeOCCreateHandle),
DoPrefix: "test",
DoRepository: "repo",
Doid: "test/repo/123",
@@ -452,37 +452,37 @@ func TestIsValidOpType(t *testing.T) {
tests := []struct {
name string
source model.Source
opType model.Type
opType string
expected bool
}{
{
name: "IRP有效操作类型",
source: model.OpSourceIRP,
opType: model.OpTypeOCCreateHandle,
opType: string(model.OpTypeOCCreateHandle),
expected: true,
},
{
name: "IRP无效操作类型",
source: model.OpSourceIRP,
opType: model.OpTypeHello,
opType: string(model.OpTypeHello),
expected: false,
},
{
name: "DOIP有效操作类型",
source: model.OpSourceDOIP,
opType: model.OpTypeHello,
opType: string(model.OpTypeHello),
expected: true,
},
{
name: "DOIP无效操作类型",
source: model.OpSourceDOIP,
opType: model.OpTypeOCCreateHandle,
opType: string(model.OpTypeOCCreateHandle),
expected: false,
},
{
name: "未知来源和类型",
source: model.Source("unknown"),
opType: model.Type("unknown"),
opType: "unknown",
expected: false,
},
}
@@ -502,7 +502,7 @@ func TestNewFullOperation(t *testing.T) {
tests := []struct {
name string
opSource model.Source
opType model.Type
opType string
doPrefix string
doRepository string
doid string
@@ -516,7 +516,7 @@ func TestNewFullOperation(t *testing.T) {
{
name: "成功创建完整操作",
opSource: model.OpSourceIRP,
opType: model.OpTypeOCCreateHandle,
opType: string(model.OpTypeOCCreateHandle),
doPrefix: "test",
doRepository: "repo",
doid: "test/repo/123",
@@ -530,7 +530,7 @@ func TestNewFullOperation(t *testing.T) {
{
name: "空请求体和响应体",
opSource: model.OpSourceIRP,
opType: model.OpTypeOCCreateHandle,
opType: string(model.OpTypeOCCreateHandle),
doPrefix: "test",
doRepository: "repo",
doid: "test/repo/123",
@@ -544,7 +544,7 @@ func TestNewFullOperation(t *testing.T) {
{
name: "字符串类型的请求体",
opSource: model.OpSourceIRP,
opType: model.OpTypeOCCreateHandle,
opType: string(model.OpTypeOCCreateHandle),
doPrefix: "test",
doRepository: "repo",
doid: "test/repo/123",