From 1e5744d28dd683c0170f3d7bd3a75cef6e7fa7cb Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Fri, 22 Nov 2024 18:41:46 +0800 Subject: [PATCH] Use a faster deepcopy library This commit closes https://github.com/qax-os/excelize/issues/2029. Signed-off-by: Eng Zer Jun --- col.go | 8 +++++--- go.mod | 4 ++-- go.sum | 8 ++++---- rows.go | 10 ++++++---- sheet.go | 8 +++++--- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/col.go b/col.go index 68ffc70cc0..a61187781e 100644 --- a/col.go +++ b/col.go @@ -18,7 +18,7 @@ import ( "strconv" "strings" - "github.com/mohae/deepcopy" + "github.com/tiendc/go-deepcopy" ) // Define the default cell size and EMU unit of measurement. @@ -533,7 +533,8 @@ func (f *File) SetColWidth(sheet, startCol, endCol string, width float64) error func flatCols(col xlsxCol, cols []xlsxCol, replacer func(fc, c xlsxCol) xlsxCol) []xlsxCol { var fc []xlsxCol for i := col.Min; i <= col.Max; i++ { - c := deepcopy.Copy(col).(xlsxCol) + var c xlsxCol + deepcopy.Copy(&c, col) c.Min, c.Max = i, i fc = append(fc, c) } @@ -551,7 +552,8 @@ func flatCols(col xlsxCol, cols []xlsxCol, replacer func(fc, c xlsxCol) xlsxCol) fc[idx] = replacer(fc[idx], column) continue } - c := deepcopy.Copy(column).(xlsxCol) + var c xlsxCol + deepcopy.Copy(&c, column) c.Min, c.Max = i, i fc = append(fc, c) } diff --git a/go.mod b/go.mod index 48848c6dff..2a5d355f11 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module github.com/xuri/excelize/v2 go 1.18 require ( - github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 github.com/richardlehane/mscfb v1.0.4 - github.com/stretchr/testify v1.8.4 + github.com/stretchr/testify v1.9.0 + github.com/tiendc/go-deepcopy v1.1.0 github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 golang.org/x/crypto v0.29.0 diff --git a/go.sum b/go.sum index 2c4284e202..07876d5fdd 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw= -github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/richardlehane/mscfb v1.0.4 h1:WULscsljNPConisD5hR0+OyZjwK46Pfyr6mPu5ZawpM= @@ -9,8 +7,10 @@ github.com/richardlehane/mscfb v1.0.4/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7 github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg= github.com/richardlehane/msoleps v1.0.4 h1:WuESlvhX3gH2IHcd8UqyCuFY5yiq/GR/yqaSM/9/g00= github.com/richardlehane/msoleps v1.0.4/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/tiendc/go-deepcopy v1.1.0 h1:rBHhm5vg7WYnGLwktbQouodWjBXDoStOL4S7v/K8S4A= +github.com/tiendc/go-deepcopy v1.1.0/go.mod h1:toXoeQoUqXOOS/X4sKuiAoSk6elIdqc0pN7MTgOOo2I= github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7psK/lVsjIS2otl+1WyRyY= github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI= github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A= diff --git a/rows.go b/rows.go index d43a015040..1eb9106e79 100644 --- a/rows.go +++ b/rows.go @@ -20,7 +20,7 @@ import ( "strconv" "strings" - "github.com/mohae/deepcopy" + "github.com/tiendc/go-deepcopy" ) // duplicateHelperFunc defines functions to duplicate helper. @@ -653,7 +653,7 @@ func (f *File) DuplicateRowTo(sheet string, row, row2 int) error { for i, r := range ws.SheetData.Row { if r.R == row { - rowCopy = deepcopy.Copy(ws.SheetData.Row[i]).(xlsxRow) + deepcopy.Copy(&rowCopy, ws.SheetData.Row[i]) ok = true break } @@ -729,7 +729,8 @@ func (f *File) duplicateConditionalFormat(ws *xlsxWorksheet, sheet string, row, } } if len(SQRef) > 0 { - cfCopy := deepcopy.Copy(*cf).(xlsxConditionalFormatting) + var cfCopy xlsxConditionalFormatting + deepcopy.Copy(&cfCopy, *cf) cfCopy.SQRef = strings.Join(SQRef, " ") cfs = append(cfs, &cfCopy) } @@ -759,7 +760,8 @@ func (f *File) duplicateDataValidations(ws *xlsxWorksheet, sheet string, row, ro } } if len(SQRef) > 0 { - dvCopy := deepcopy.Copy(*dv).(xlsxDataValidation) + var dvCopy xlsxDataValidation + deepcopy.Copy(&dvCopy, *dv) dvCopy.Sqref = strings.Join(SQRef, " ") dvs = append(dvs, &dvCopy) } diff --git a/sheet.go b/sheet.go index f57797b4ee..4e454606f7 100644 --- a/sheet.go +++ b/sheet.go @@ -27,7 +27,7 @@ import ( "unicode/utf16" "unicode/utf8" - "github.com/mohae/deepcopy" + "github.com/tiendc/go-deepcopy" ) // NewSheet provides the function to create a new sheet by given a worksheet @@ -124,7 +124,8 @@ func (f *File) mergeExpandedCols(ws *xlsxWorksheet) { Width: ws.Cols.Col[i-1].Width, }, ws.Cols.Col[i]); i++ { } - column := deepcopy.Copy(ws.Cols.Col[left]).(xlsxCol) + var column xlsxCol + deepcopy.Copy(&column, ws.Cols.Col[left]) if left < i-1 { column.Max = ws.Cols.Col[i-1].Min } @@ -750,7 +751,8 @@ func (f *File) copySheet(from, to int) error { if err != nil { return err } - worksheet := deepcopy.Copy(sheet).(*xlsxWorksheet) + worksheet := &xlsxWorksheet{} + deepcopy.Copy(worksheet, sheet) toSheetID := strconv.Itoa(f.getSheetID(f.GetSheetName(to))) sheetXMLPath := "xl/worksheets/sheet" + toSheetID + ".xml" if len(worksheet.SheetViews.SheetView) > 0 {