我正在尝试重构我的代码但是我被卡住了。
我想清理这个HandleExportDevicesAsCsv
函数。所以,决定从我开始评论到评论结束的地方编码。然后我重构并将该代码块移动到另一个函数。
任何人都可以指导我从ExportDevicesAsCsv
返回值,以便我可以将其传递给c.Data(http.StatusOK, "text/csv", b.Bytes())
此功能正常工作
func HandleExportDevicesAsCsv(c *gin.Context) {
deviceIDs := c.Request.FormValue("deviceIDs")
var data []byte
token := c.Request.Header.Get("Authorization")
data = services.ExportDevicesData(token, deviceIDs)
//===================================================================
//I want to refactor this code from here
//====================================================================
type DR []models.DeviceResponse
var deviceResponse DR
json.Unmarshal([]byte(data), &deviceResponse)
b := &bytes.Buffer{}
w := csv.NewWriter(b)
if err := w.Write([]string{"Name", "Id", "Createdon","Lastupdated", "qrcode", "mac", "state", "kind"});
err != nil {
log.Fatalln("error writing record to csv:", err)
}
for _, device := range deviceResponse {
var record []string
record = append(record, device.Name)
record = append(record, device.ID)
record = append(record, device.Createdon)
record = append(record, device.Lastupdated)
record = append(record, device.Qrcode)
record = append(record, device.State)
record = append(record, device.Kind)
if err := w.Write(record); err != nil {
log.Fatalln("error writing record to csv:", err)
}
}
w.Flush()
if err := w.Error();
err != nil {
log.Fatal(err)
}
//===================================================================
// to here end of refactor code
//====================================================================
c.Header("Content-type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename=devices.csv")
c.Data(http.StatusOK, "text/csv", b.Bytes())
}
当我重构功能时,这不起作用。
func HandleExportDevicesAsCsv(c *gin.Context) {
METHOD_NAME := "HandleExportDevicesAsCsv() - "
mlog.Debug("%s request = %v", METHOD_NAME, c.Request)
deviceIDs := c.Request.FormValue("deviceIDs")
mlog.Debug("%s deviceIDs = %s", METHOD_NAME, deviceIDs)
var data []byte
token := c.Request.Header.Get("Authorization")
data = services.ExportDevicesData(token, deviceIDs)
cdata := services.ExportDevicesAsCsv(data)
ccdata := cdata.Bytes()
mlog.Debug("%s response = %s", METHOD_NAME, string(data))
c.Header("Content-type", "application/octet-stream")
c.Header("Content-Disposition", "attachment; filename=devices.csv")
//c.Data(http.StatusOK, "application/octet-stream", data) // text/csv
// c.Header("Content-Description", "File Transfer")
// c.Header("Content-Disposition", "attachment; filename=devices.csv")
c.Data(http.StatusOK, "text/csv", ccdata)
// c.Header("Content-Type", "text/csv")
// c.Writer.Write(b.Bytes())
// c.Writer.Flush()
}
在这里,我重构并将该代码放在另一个函数中并调用HandleExportDevicesAsCsv
func ExportDevicesAsCsv(data []byte) *bytes.Buffer {
//METHOD_NAME := "ExportDevicesAsCsv() - "
//var Handlersdata handlers.data
//var csvVal []byte
//var devices []interface{}
//var nextPageToken string
//for len(nextPageToken) > 0 {
type DR []models.DeviceResponse
var deviceResponse DR
json.Unmarshal([]byte(data), &deviceResponse)
b := &bytes.Buffer{}
w := csv.NewWriter(b)
if err := w.Write([]string{"Name", "Id", "Createdon",
"Lastupdated", "qrcode", "mac", "state", "kind"}); err != nil {
log.Fatalln("error writing record to csv:", err)
}
for _, device := range deviceResponse {
var record []string
record = append(record, device.Name)
record = append(record, device.ID)
record = append(record, device.Createdon)
record = append(record, device.Lastupdated)
record = append(record, device.Qrcode)
record = append(record, device.State)
record = append(record, device.Kind)
if err := w.Write(record); err != nil {
log.Fatalln("error writing record to csv:", err)
}
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatal(err)
}
// c.Header("Content-Description", "File Transfer")
// c.Header("Content-Disposition", "attachment; filename=devices.csv")
// c.Data(http.StatusOK, "text/csv", b.Bytes())
return b
}