func (c *cAsset) GetComponentList(r *ghttp.Request) { var req *v1.GetComponentListReq if err := r.Parse(&req); err != nil { r.Response.WriteJson(g.Map{ "code": 1, "msg": err.Error(), }) } filtering := &creativecomponent.GetFiltering{ ComponentID: req.ComponentId, ComponentName: req.ComponentName, ComponentTypes: []enum.ComponentType{}, Status: []enum.ComponentStatus{}, } getRequest := &creativecomponent.GetRequest{ AdvertiserID: req.AdvertiserId, Page: req.Page, PageSize: req.PageSize, Filtering: filtering, } res, err := service.Asset().Get(getRequest) if err != nil { r.Response.WriteJson(g.Map{ "code": 2, "msg": err.Error(), }) } r.Response.WriteJson(res) }这段代码中GetComponentList的过滤条件为type GetComponentListReq struct { g.Meta `path:"/get_component_list" tags:"查询组件列表" method:"post" sm:"组件列表"` AdvertiserId uint64 `json:"advertiser_id" v:"required" dc:"广告主id"` Page int `json:"page" dc:"页码"` PageSize int `json:"page_size" dc:"每页数量。默认值20,最新值10,最大值40"` ComponentId uint64 `json:"component_id" dc:"组件id"` ComponentName string `json:"component_name" dc:"组件名称"` ComponentTypes string `json:"component_types" dc:"组件类型"` Status string `json:"status" dc:"组件审核状态"` }在不修改componentTypes类型的的情况下怎么把componentTypes放到[]enum.ComponentType{}中,使代码能正常运行,请详细一点
时间: 2023-07-24 15:14:01 浏览: 82
如果你不想修改GetComponentListReq结构体中ComponentTypes字段的类型为[]enum.ComponentType,但仍然想将componentTypes参数放入[]enum.ComponentType{}中,你可以在代码中进行一些转换。
首先,你可以将获取的componentTypes字符串通过逗号分割成多个组件类型,然后使用循环将每个组件类型转换为enum.ComponentType,并添加到一个新的切片中。接下来,将新切片赋值给filtering结构体的ComponentTypes字段。
以下是对代码的修改:
```go
import "strings"
func (c *cAsset) GetComponentList(r *ghttp.Request) {
var req *v1.GetComponentListReq
if err := r.Parse(&req); err != nil {
r.Response.WriteJson(g.Map{
"code": 1,
"msg": err.Error(),
})
}
// 转换componentTypes字符串为[]enum.ComponentType
componentTypes := []enum.ComponentType{}
if req.ComponentTypes != "" {
types := strings.Split(req.ComponentTypes, ",")
for _, t := range types {
componentTypes = append(componentTypes, enum.ComponentType(t))
}
}
filtering := &creativecomponent.GetFiltering{
ComponentID: req.ComponentId,
ComponentName: req.ComponentName,
ComponentTypes: componentTypes,
Status: []enum.ComponentStatus{},
}
getRequest := &creativecomponent.GetRequest{
AdvertiserID: req.AdvertiserId,
Page: req.Page,
PageSize: req.PageSize,
Filtering: filtering,
}
res, err := service.Asset().Get(getRequest)
if err != nil {
r.Response.WriteJson(g.Map{
"code": 2,
"msg": err.Error(),
})
}
r.Response.WriteJson(res)
}
```
在上面的代码中,我们使用`strings.Split`函数将componentTypes字符串分割为多个组件类型,并使用enum.ComponentType将每个类型转换为对应的枚举类型。然后,将转换后的类型切片赋值给filtering结构体的ComponentTypes字段,这样代码就能正常运行了。请确保在进行类型转换时,每个组件类型的值与enum.ComponentType中的枚举值相匹配。
阅读全文