Skip to content

Commit

Permalink
fix: sync.Map use LoadOrStore
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtwinkle committed Aug 24, 2022
1 parent d48a293 commit 05a105d
Showing 1 changed file with 40 additions and 65 deletions.
105 changes: 40 additions & 65 deletions excelizeam.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,14 @@ func (e *excelizeam) setCellValue(colIndex, rowIndex int, value interface{}, sty
e.checkMaxIndex(colIndex, rowIndex)
key := e.getCacheKey(colIndex, rowIndex)

if cached, ok := e.cellStore.Load(key); ok {
styleID, err := e.getStyleID(style)
if err != nil {
return err
}
if cached, ok := e.cellStore.LoadOrStore(key, &Cell{
StyleID: styleID,
Value: value,
}); ok {
cell := cached.(*Cell)
if cell.Value != nil && value != nil && !overrideValue {
return ErrOverrideCellValue
Expand All @@ -200,31 +207,14 @@ func (e *excelizeam) setCellValue(colIndex, rowIndex int, value interface{}, sty
if !overrideStyle {
return ErrOverrideCellStyle
}
styleID, err := e.overrideStyle(cell.StyleID, *style)
styleID, err = e.overrideStyle(cell.StyleID, *style)
if err != nil {
return err
}
cell.StyleID = styleID
return nil
}
styleID, err := e.getStyleID(style)
if err != nil {
return err
}
cell.StyleID = styleID
return nil
}
return nil
}

styleID, err := e.getStyleID(style)
if err != nil {
return err
}
e.cellStore.Store(key, &Cell{
StyleID: styleID,
Value: value,
})
return nil
}

Expand All @@ -245,29 +235,28 @@ func (e *excelizeam) SetStyleCell(colIndex, rowIndex int, style excelize.Style,
func (e *excelizeam) setStyleCell(colIndex, rowIndex int, style excelize.Style, override bool) error {
e.checkMaxIndex(colIndex, rowIndex)
key := e.getCacheKey(colIndex, rowIndex)
if cached, ok := e.cellStore.Load(key); ok {

styleID, err := e.getStyleID(&style)
if err != nil {
return err
}
if cached, ok := e.cellStore.LoadOrStore(key, &Cell{
StyleID: styleID,
Value: nil,
}); ok {
c := cached.(*Cell)
if c.StyleID > 0 {
if !override {
return ErrOverrideCellStyle
}
styleID, err := e.overrideStyle(c.StyleID, style)
styleID, err = e.overrideStyle(c.StyleID, style)
if err != nil {
return err
}
c.StyleID = styleID
}
return nil
}

styleID, err := e.getStyleID(&style)
if err != nil {
return err
}
e.cellStore.Store(key, &Cell{
StyleID: styleID,
Value: nil,
})
return nil
}

Expand All @@ -290,34 +279,27 @@ func (e *excelizeam) setStyleCellRange(startColIndex, startRowIndex, endColIndex
for rowIdx := startRowIndex; rowIdx <= endRowIndex; rowIdx++ {
for colIdx := startColIndex; colIdx <= endColIndex; colIdx++ {
key := e.getCacheKey(colIdx, rowIdx)
if cached, ok := e.cellStore.Load(key); ok {

styleID, err := e.getStyleID(&style)
if err != nil {
return err
}
if cached, ok := e.cellStore.LoadOrStore(key, &Cell{
StyleID: styleID,
Value: nil,
}); ok {
c := cached.(*Cell)
if c.StyleID > 0 {
if !override {
return ErrOverrideCellStyle
}
styleID, err := e.overrideStyle(c.StyleID, style)
if err != nil {
return err
}
c.StyleID = styleID
} else {
styleID, err := e.getStyleID(&style)
styleID, err = e.overrideStyle(c.StyleID, style)
if err != nil {
return err
}
c.StyleID = styleID
}
continue
}
styleID, err := e.getStyleID(&style)
if err != nil {
return err
c.StyleID = styleID
}
e.cellStore.Store(key, &Cell{
StyleID: styleID,
Value: nil,
})
}
}
return nil
Expand Down Expand Up @@ -501,34 +483,27 @@ func (e *excelizeam) setBorderRange(startColIndex, startRowIndex, endColIndex, e
}
style := excelize.Style{Border: borderStyles}

if cached, ok := e.cellStore.Load(key); ok {
styleID, err := e.getStyleID(&style)
if err != nil {
return err
}
if cached, ok := e.cellStore.LoadOrStore(key, &Cell{
StyleID: styleID,
Value: nil,
}); ok {
c := cached.(*Cell)
if c.StyleID > 0 {
if !override {
return ErrOverrideCellStyle
}
styleID, err := e.overrideStyle(c.StyleID, style)
if err != nil {
return err
}
c.StyleID = styleID
} else {
styleID, err := e.getStyleID(&style)
styleID, err = e.overrideStyle(c.StyleID, style)
if err != nil {
return err
}
c.StyleID = styleID
}
c.StyleID = styleID
continue
}
styleID, err := e.getStyleID(&style)
if err != nil {
return err
}
e.cellStore.Store(key, &Cell{
StyleID: styleID,
Value: nil,
})
}
}
return nil
Expand Down

0 comments on commit 05a105d

Please sign in to comment.