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:
@@ -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,
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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"`
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user