【Golang】数据设计模式

在Go语言中,根据不同业务类型返回不同表数据,可以使用以下几种方式来设计方法的出参:

1. 使用接口(interface)作为返回类型

go 复制代码
// 定义一个通用的业务数据接口
type BusinessData interface {
    GetId() int64
    GetName() string
}

// 不同业务类型实现该接口
type SampleData struct {
    Id   int64  `json:"id"`
    Name string `json:"name"`
    // 其他样本特有字段
}

func (s SampleData) GetId() int64 { return s.Id }
func (s SampleData) GetName() string { return s.Name }

type ProjectData struct {
    Id   int64  `json:"id"`
    Name string `json:"name"`
    // 其他项目特有字段
}

func (p ProjectData) GetId() int64 { return p.Id }
func (p ProjectData) GetName() string { return p.Name }

// 业务处理方法
func GetBusinessData(businessType string, id int64) (BusinessData, error) {
    switch businessType {
    case "sample":
        // 查询样本数据
        var data SampleData
        // ... 查询逻辑
        return data, nil
    case "project":
        // 查询项目数据
        var data ProjectData
        // ... 查询逻辑
        return data, nil
    default:
        return nil, errors.New("unsupported business type")
    }
}

2. 使用泛型(Go 1.18+)

go 复制代码
// 定义泛型方法
func GetBusinessData[T SampleData|ProjectData](businessType string, id int64) (T, error) {
    var result T
    switch businessType {
    case "sample":
        if t, ok := any(result).(SampleData); ok {
            // 查询样本数据逻辑
            // ...
            result = any(t).(T)
        }
    case "project":
        if t, ok := any(result).(ProjectData); ok {
            // 查询项目数据逻辑
            // ...
            result = any(t).(T)
        }
    }
    return result, nil
}

3. 使用any/interface{} + 类型断言

go 复制代码
func GetBusinessData(businessType string, id int64) (interface{}, error) {
    switch businessType {
    case "sample":
        var data SampleData
        // 查询样本数据逻辑
        // ...
        return data, nil
    case "project":
        var data ProjectData
        // 查询项目数据逻辑
        // ...
        return data, nil
    default:
        return nil, errors.New("unsupported business type")
    }
}

// 使用时进行类型断言
func Usage() {
    data, err := GetBusinessData("sample", 1)
    if err != nil {
        // 处理错误
        return
    }
    
    switch v := data.(type) {
    case SampleData:
        // 处理样本数据
        fmt.Println("Sample:", v.Name)
    case ProjectData:
        // 处理项目数据
        fmt.Println("Project:", v.Name)
    }
}

4. 使用统一的响应结构体

go 复制代码
type BusinessResponse struct {
    BusinessType string      `json:"businessType"`
    Data         interface{} `json:"data"`
}

func GetBusinessData(businessType string, id int64) (*BusinessResponse, error) {
    resp := &BusinessResponse{
        BusinessType: businessType,
    }
    
    switch businessType {
    case "sample":
        var data SampleData
        // 查询样本数据逻辑
        // ...
        resp.Data = data
    case "project":
        var data ProjectData
        // 查询项目数据逻辑
        // ...
        resp.Data = data
    default:
        return nil, errors.New("unsupported business type")
    }
    
    return resp, nil
}

5. 在你的具体场景中应用

根据你提供的代码,可以这样设计:

go 复制代码
// 定义业务审批数据接口
type BusinessApprovalData interface {
    GetId() int64
    GetBusinessId() int64
    GetStatus() int
}

// 样本审批数据
type SampleApprovalData struct {
    Id         int64  `json:"id"`
    BusinessId int64  `json:"businessId"`
    SampleName string `json:"sampleName"`
    Status     int    `json:"status"`
}

func (s SampleApprovalData) GetId() int64 { return s.Id }
func (s SampleApprovalData) GetBusinessId() int64 { return s.BusinessId }
func (s SampleApprovalData) GetStatus() int { return s.Status }

// 项目审批数据
type ProjectApprovalData struct {
    Id         int64  `json:"id"`
    BusinessId int64  `json:"businessId"`
    ProjectName string `json:"projectName"`
    Status     int    `json:"status"`
}

func (p ProjectApprovalData) GetId() int64 { return p.Id }
func (p ProjectApprovalData) GetBusinessId() int64 { return p.BusinessId }
func (p ProjectApprovalData) GetStatus() int { return p.Status }

// 修改你的方法
func (c *cUserTask) ToDoTaskList(ctx context.Context, req *usertask.UserTaskListInp) (interface{}, error) {
    instanceIdList, err := c.ToDoTaskInstanceIdList(ctx)
    if err != nil {
        return nil, err
    }
    
    if req.BusinessType == "" {
        return nil, nil
    }
    
    inp := bizin.WorkflowModelInstanceByConditionInp{
        FlwInstantIdList: instanceIdList, 
        BusinessType: req.BusinessType,
    }
    
    instanceList, err := service.BizWorkflowModelInstance().ListByCondition(ctx, &inp)
    if err != nil {
        return nil, err
    }
    
    businessIdList := make([]int64, 0, len(instanceList))
    for _, each := range instanceList {
        businessIdList = append(businessIdList, each.BusinessId)
    }
    
    // 根据业务类型返回不同数据
    switch req.BusinessType {
    case "sample":
        // 获取样本审批数据
        var approvalDataList []SampleApprovalData
        // ... 查询逻辑
        return approvalDataList, nil
    case "project":
        // 获取项目审批数据
        var approvalDataList []ProjectApprovalData
        // ... 查询逻辑
        return approvalDataList, nil
    default:
        return nil, errors.New("unsupported business type")
    }
}

推荐使用第1种或第4种方式,它们既保持了类型安全,又具有良好的可读性和可维护性。

相关推荐
步行cgn3 小时前
责任链设计模式详解
设计模式
城管不管3 小时前
SpringBoot与反射
java·开发语言·前端
十启树3 小时前
常见开发语言在 Windows 上的默认编码格式
开发语言
eqwaak03 小时前
科技信息差(9.22)
开发语言·python·科技·语言模型
胡耀超3 小时前
37、RAG系统架构与实现:知识增强型AI的完整构建
开发语言·人工智能·python·深度学习·神经网络·系统架构
小闫BI设源码4 小时前
Python Flask快速入门
开发语言·python·flask
青草地溪水旁4 小时前
设计模式(C++)详解——职责链模式 (Chain of Responsibility)(2)
c++·设计模式·责任链模式
青草地溪水旁4 小时前
设计模式(C++)详解——职责链模式 (Chain of Responsibility)(1)
c++·设计模式·责任链模式
phdsky4 小时前
【设计模式】责任链模式
设计模式·责任链模式