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

@@ -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",

View File

@@ -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,

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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",

View File

@@ -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"`

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",

View File

@@ -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",

View File

@@ -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,

View File

@@ -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),

View File

@@ -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",

View File

@@ -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,

View File

@@ -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,
}

View File

@@ -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)
})
}

View File

@@ -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)