Skip to content

Commit

Permalink
refactor: 代码改进
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyi committed Nov 15, 2024
1 parent d9cdc2d commit 65d4c26
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"time"
)

Expand Down Expand Up @@ -60,7 +60,7 @@ func loadCache(ctx context.Context, ent Cacheable) (bool, error) {
}
defer zr.Close()

v, err := ioutil.ReadAll(zr)
v, err := io.ReadAll(zr)
if err != nil {
return false, fmt.Errorf("uncompress data, %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ func dbDriver(db DB) string {

func isConflictError(err error, driver string) bool {
s := err.Error()
if driver == driverPostgres {
switch driver {
case driverPostgres:
return strings.Contains(s, "duplicate key value violates unique constraint")
} else if driver == driverMysql {
case driverMysql:
return strings.Contains(s, "Duplicate entry")
} else if driver == driverSqlite3 {
case driverSqlite3:
return strings.Contains(s, "UNIQUE constraint failed")
}
return false
Expand Down
13 changes: 7 additions & 6 deletions entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,22 @@ func getColumns(ent Entity) []Column { // revive:disable-line
}

for key := range fi.Options {
if key == "primaryKey" || key == "primary_key" {
switch key {
case "primaryKey", "primary_key":
col.PrimaryKey = true
col.RefuseUpdate = true
} else if key == "refuseUpdate" || key == "refuse_update" {
case "refuseUpdate", "refuse_update":
col.RefuseUpdate = true
} else if key == "returning" {
case "returning":
col.ReturningInsert = true
col.ReturningUpdate = true
col.RefuseUpdate = true
} else if key == "returningInsert" || key == "returning_insert" {
case "returningInsert", "returning_insert":
col.ReturningInsert = true
} else if key == "returningUpdate" || key == "returning_update" {
case "returningUpdate", "returning_update":
col.ReturningUpdate = true
col.RefuseUpdate = true
} else if key == "autoIncrement" || key == "auto_increment" {
case "autoIncrement", "auto_increment":
col.AutoIncrement = true
col.RefuseUpdate = true
}
Expand Down

0 comments on commit 65d4c26

Please sign in to comment.