From a90d853a6e73e810814cd0a7023318159cbfe52e Mon Sep 17 00:00:00 2001 From: ryan <2650306917@qq.com> Date: Wed, 24 Dec 2025 16:48:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=B0=86=20OpType=20=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E4=BB=8E=E6=9E=9A=E4=B8=BE=E7=B1=BB=E5=9E=8B=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=20string=20=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 主要变更: - 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 --- api/highclient/client_test.go | 2 +- api/model/converter.go | 4 +-- api/model/converter_test.go | 10 +++--- api/model/envelope_debug_test.go | 4 +-- api/model/envelope_sign_verify_test.go | 2 +- api/model/envelope_test.go | 6 ++-- api/model/operation.go | 10 +++--- api/model/operation_test.go | 40 +++++++++++------------ api/model/operation_timestamp_test.go | 2 +- api/persistence/cursor_worker.go | 2 +- api/persistence/e2e_integration_test.go | 2 +- api/persistence/example_test.go | 10 +++--- api/persistence/repository_test.go | 2 +- api/queryclient/client.go | 4 +-- api/queryclient/client_additional_test.go | 2 +- api/queryclient/client_test.go | 2 +- 16 files changed, 52 insertions(+), 52 deletions(-) diff --git a/api/highclient/client_test.go b/api/highclient/client_test.go index 160d4a7..638a181 100644 --- a/api/highclient/client_test.go +++ b/api/highclient/client_test.go @@ -507,7 +507,7 @@ func createTestOperationWithID(t testing.TB, id string) *model.Operation { } operation, err := model.NewFullOperation( model.OpSourceDOIP, - model.OpTypeRetrieve, + string(model.OpTypeRetrieve), "test-prefix", "test-repo", "test-prefix/test-repo/test-object", diff --git a/api/model/converter.go b/api/model/converter.go index d4e2d17..b0fcff6 100644 --- a/api/model/converter.go +++ b/api/model/converter.go @@ -26,7 +26,7 @@ func FromProtobuf(pbOp *pb.OperationData) (*Operation, error) { OpID: pbOp.GetOpId(), Timestamp: timestamp, OpSource: Source(pbOp.GetOpSource()), - OpType: Type(pbOp.GetOpType()), + OpType: pbOp.GetOpType(), DoPrefix: pbOp.GetDoPrefix(), DoRepository: pbOp.GetDoRepository(), Doid: pbOp.GetDoid(), @@ -59,7 +59,7 @@ func ToProtobuf(op *Operation) (*pb.OperationData, error) { OpId: op.OpID, Timestamp: timestamp, OpSource: string(op.OpSource), - OpType: string(op.OpType), + OpType: op.OpType, DoPrefix: op.DoPrefix, DoRepository: op.DoRepository, Doid: op.Doid, diff --git a/api/model/converter_test.go b/api/model/converter_test.go index 76cf372..4a5360f 100644 --- a/api/model/converter_test.go +++ b/api/model/converter_test.go @@ -54,7 +54,7 @@ func TestFromProtobuf_Basic(t *testing.T) { assert.Equal(t, "op-123", result.OpID) assert.Equal(t, now.Unix(), result.Timestamp.Unix()) assert.Equal(t, model.Source("IRP"), result.OpSource) - assert.Equal(t, model.Type("OC_CREATE_HANDLE"), result.OpType) + assert.Equal(t, "OC_CREATE_HANDLE", result.OpType) assert.Equal(t, "test", result.DoPrefix) assert.Equal(t, "repo", result.DoRepository) assert.Equal(t, "test/repo/123", result.Doid) @@ -133,7 +133,7 @@ func TestToProtobuf_Basic(t *testing.T) { OpID: "op-123", Timestamp: now, OpSource: model.OpSourceIRP, - OpType: model.OpTypeOCCreateHandle, + OpType: string(model.OpTypeOCCreateHandle), DoPrefix: "test", DoRepository: "repo", Doid: "test/repo/123", @@ -166,7 +166,7 @@ func TestToProtobuf_WithHashes(t *testing.T) { OpID: "op-123", Timestamp: now, OpSource: model.OpSourceDOIP, - OpType: model.OpTypeCreate, + OpType: string(model.OpTypeCreate), DoPrefix: "test", DoRepository: "repo", Doid: "test/repo/123", @@ -192,7 +192,7 @@ func TestToProtobuf_WithoutHashes(t *testing.T) { OpID: "op-123", Timestamp: now, OpSource: model.OpSourceDOIP, - OpType: model.OpTypeCreate, + OpType: string(model.OpTypeCreate), DoPrefix: "test", DoRepository: "repo", Doid: "test/repo/123", @@ -511,7 +511,7 @@ func TestRoundTrip_Operation(t *testing.T) { OpID: "op-123", Timestamp: now, OpSource: model.OpSourceIRP, - OpType: model.OpTypeOCCreateHandle, + OpType: string(model.OpTypeOCCreateHandle), DoPrefix: "test", DoRepository: "repo", Doid: "test/repo/123", diff --git a/api/model/envelope_debug_test.go b/api/model/envelope_debug_test.go index cf89f11..714ce65 100644 --- a/api/model/envelope_debug_test.go +++ b/api/model/envelope_debug_test.go @@ -93,7 +93,7 @@ func TestEnvelopeBodyTampering(t *testing.T) { OpID: "op-test-002", Timestamp: time.Now(), OpSource: model.OpSourceIRP, - OpType: model.OpTypeOCCreateHandle, + OpType: string(model.OpTypeOCCreateHandle), DoPrefix: "test", DoRepository: "repo", Doid: "test/repo/456", @@ -168,7 +168,7 @@ func TestEnvelopeSignatureTampering(t *testing.T) { OpID: "op-test-003", Timestamp: time.Now(), OpSource: model.OpSourceIRP, - OpType: model.OpTypeOCCreateHandle, + OpType: string(model.OpTypeOCCreateHandle), DoPrefix: "test", DoRepository: "repo", Doid: "test/repo/789", diff --git a/api/model/envelope_sign_verify_test.go b/api/model/envelope_sign_verify_test.go index 99c5fbd..e9d57d4 100644 --- a/api/model/envelope_sign_verify_test.go +++ b/api/model/envelope_sign_verify_test.go @@ -35,7 +35,7 @@ func TestSignVerifyConsistency(t *testing.T) { OpID: "op-test-001", Timestamp: time.Now(), OpSource: model.OpSourceIRP, - OpType: model.OpTypeOCCreateHandle, + OpType: string(model.OpTypeOCCreateHandle), DoPrefix: "test", DoRepository: "repo", Doid: "test/repo/123", diff --git a/api/model/envelope_test.go b/api/model/envelope_test.go index 0b28bb7..fba08e6 100644 --- a/api/model/envelope_test.go +++ b/api/model/envelope_test.go @@ -196,7 +196,7 @@ func TestMarshalTrustlog_Basic(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", @@ -229,7 +229,7 @@ func TestMarshalOperation(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", @@ -253,7 +253,7 @@ func TestUnmarshalOperation(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", diff --git a/api/model/operation.go b/api/model/operation.go index 8e022fe..1b6734e 100644 --- a/api/model/operation.go +++ b/api/model/operation.go @@ -113,9 +113,9 @@ func GetOpTypesBySource(source Source) []Type { } // IsValidOpType 判断指定操作类型在给定来源下是否合法。 -func IsValidOpType(source Source, opType Type) bool { +func IsValidOpType(source Source, opType string) bool { for _, t := range GetOpTypesBySource(source) { - if t == opType { + if string(t) == opType { return true } } @@ -132,7 +132,7 @@ type Operation struct { OpID string `json:"opId" validate:"max=32"` Timestamp time.Time `json:"timestamp" validate:"required"` OpSource Source `json:"opSource" validate:"required,oneof=IRP DOIP"` - OpType Type `json:"opType" validate:"required"` + OpType string `json:"opType" validate:"required"` DoPrefix string `json:"doPrefix" validate:"required,max=512"` DoRepository string `json:"doRepository" validate:"required,max=512"` Doid string `json:"doid" validate:"required,max=512"` @@ -157,7 +157,7 @@ type Operation struct { // 自动完成哈希计算和字段校验,确保创建的 Operation 是完整且有效的。 func NewFullOperation( opSource Source, - opType Type, + opType string, doPrefix, doRepository, doid string, producerID string, opActor string, @@ -289,7 +289,7 @@ type operationData struct { OpID *string `cbor:"opId"` Timestamp *time.Time `cbor:"timestamp"` OpSource *Source `cbor:"opSource"` - OpType *Type `cbor:"opType"` + OpType *string `cbor:"opType"` DoPrefix *string `cbor:"doPrefix"` DoRepository *string `cbor:"doRepository"` Doid *string `cbor:"doid"` diff --git a/api/model/operation_test.go b/api/model/operation_test.go index 94a6288..a2f8d3f 100644 --- a/api/model/operation_test.go +++ b/api/model/operation_test.go @@ -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", diff --git a/api/model/operation_timestamp_test.go b/api/model/operation_timestamp_test.go index 4997068..4c6e149 100644 --- a/api/model/operation_timestamp_test.go +++ b/api/model/operation_timestamp_test.go @@ -21,7 +21,7 @@ func TestOperation_TimestampNanosecondPrecision(t *testing.T) { OpID: "op-nanosecond-test", Timestamp: timestamp, OpSource: model.OpSourceIRP, - OpType: model.OpTypeOCCreateHandle, + OpType: string(model.OpTypeOCCreateHandle), DoPrefix: "test", DoRepository: "repo", Doid: "test/repo/123", diff --git a/api/persistence/cursor_worker.go b/api/persistence/cursor_worker.go index 88f31f0..1532f49 100644 --- a/api/persistence/cursor_worker.go +++ b/api/persistence/cursor_worker.go @@ -40,7 +40,7 @@ func (r *OperationRecord) ToModel() *model.Operation { RequestBodyHash: &r.RequestBodyHash, ResponseBodyHash: &r.ResponseBodyHash, OpSource: model.Source(r.OpSource), - OpType: model.Type(r.OpType), + OpType: r.OpType, DoPrefix: r.DOPrefix, DoRepository: r.DORepository, ClientIP: r.ClientIP, diff --git a/api/persistence/e2e_integration_test.go b/api/persistence/e2e_integration_test.go index 3fc5d75..3b56d1f 100644 --- a/api/persistence/e2e_integration_test.go +++ b/api/persistence/e2e_integration_test.go @@ -726,7 +726,7 @@ func createE2ETestOperations(count int) []*model.Operation { OpID: fmt.Sprintf("e2e-op-%d-%d", timestamp, i), Timestamp: time.Now(), OpSource: model.OpSourceDOIP, - OpType: model.OpTypeCreate, + OpType: string(model.OpTypeCreate), DoPrefix: "e2e-test", DoRepository: "e2e-repo", Doid: fmt.Sprintf("e2e/test/%d", i), diff --git a/api/persistence/example_test.go b/api/persistence/example_test.go index eacd12b..33e4d1d 100644 --- a/api/persistence/example_test.go +++ b/api/persistence/example_test.go @@ -57,7 +57,7 @@ func Example_dbOnly() { // 5. 构造 Operation(包含 IP 信息) op, err := model.NewFullOperation( model.OpSourceDOIP, - model.OpTypeCreate, + string(model.OpTypeCreate), "10.1000", "my-repo", "10.1000/my-repo/doc001", @@ -139,7 +139,7 @@ func Example_dbAndTrustlog() { // 5. 构造 Operation op, err := model.NewFullOperation( model.OpSourceDOIP, - model.OpTypeCreate, + string(model.OpTypeCreate), "10.1000", "my-repo", "10.1000/my-repo/doc002", @@ -212,7 +212,7 @@ func Example_trustlogOnly() { // 5. 构造 Operation op, err := model.NewFullOperation( model.OpSourceDOIP, - model.OpTypeCreate, + string(model.OpTypeCreate), "10.1000", "my-repo", "10.1000/my-repo/doc003", @@ -278,7 +278,7 @@ func Example_mysqlDatabase() { // 5. 构造并发布 Operation op, err := model.NewFullOperation( model.OpSourceDOIP, - model.OpTypeCreate, + string(model.OpTypeCreate), "10.1000", "my-repo", "10.1000/my-repo/doc004", @@ -349,7 +349,7 @@ func Example_sqliteDatabase() { // 5. 构造并发布 Operation op, err := model.NewFullOperation( model.OpSourceDOIP, - model.OpTypeCreate, + string(model.OpTypeCreate), "10.1000", "my-repo", "10.1000/my-repo/doc005", diff --git a/api/persistence/repository_test.go b/api/persistence/repository_test.go index 9c7d30f..435820d 100644 --- a/api/persistence/repository_test.go +++ b/api/persistence/repository_test.go @@ -44,7 +44,7 @@ func setupTestDB(t *testing.T) *sql.DB { func createTestOperation(t *testing.T, opID string) *model.Operation { op, err := model.NewFullOperation( model.OpSourceDOIP, - model.OpTypeCreate, + string(model.OpTypeCreate), "10.1000", "test-repo", "10.1000/test-repo/"+opID, diff --git a/api/queryclient/client.go b/api/queryclient/client.go index 95bc26b..41b7a1f 100644 --- a/api/queryclient/client.go +++ b/api/queryclient/client.go @@ -76,7 +76,7 @@ type ListOperationsRequest struct { // 可选过滤条件 Timestamp *time.Time // 操作时间戳 OpSource model.Source // 操作来源 - OpType model.Type // 操作类型 + OpType string // 操作类型 DoPrefix string // 数据前缀 DoRepository string // 数据仓库 } @@ -99,7 +99,7 @@ func (c *Client) ListOperations(ctx context.Context, req ListOperationsRequest) pbReq := &pb.ListOperationReq{ PageSize: req.PageSize, OpSource: string(req.OpSource), - OpType: string(req.OpType), + OpType: req.OpType, DoPrefix: req.DoPrefix, DoRepository: req.DoRepository, } diff --git a/api/queryclient/client_additional_test.go b/api/queryclient/client_additional_test.go index 41cda14..d5b800d 100644 --- a/api/queryclient/client_additional_test.go +++ b/api/queryclient/client_additional_test.go @@ -66,7 +66,7 @@ func TestListOperations_ErrorHandling(t *testing.T) { assert.Equal(t, uint64(10), req.PageSize) assert.Equal(t, model.Source("api"), req.OpSource) - assert.Equal(t, model.Type("create"), req.OpType) + assert.Equal(t, "create", req.OpType) }) } diff --git a/api/queryclient/client_test.go b/api/queryclient/client_test.go index 339c2ac..823ce77 100644 --- a/api/queryclient/client_test.go +++ b/api/queryclient/client_test.go @@ -312,7 +312,7 @@ func TestListOperationsRequest(t *testing.T) { PreTime: now, Timestamp: &now, OpSource: model.Source("test"), - OpType: model.Type("create"), + OpType: "create", } assert.Equal(t, uint64(10), req.PageSize)