Skip to content

Commit 25da9f2

Browse files
committed
Refactor code
1 parent d1f9c7e commit 25da9f2

File tree

9 files changed

+33
-29
lines changed

9 files changed

+33
-29
lines changed

echo/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ type Handler struct {
1717
Transform func(s string) string
1818
Cache q.TxCache
1919
Generate func(ctx context.Context) (string, error)
20-
Error func(context.Context, string)
20+
Error func(context.Context, string, ...map[string]interface{})
2121
}
2222

2323
const d = 120 * time.Second
24-
func NewHandler(db *sql.DB, transform func(s string) string, cache q.TxCache, generate func(context.Context) (string, error), options... func(context.Context, string)) *Handler {
25-
var logError func(context.Context, string)
24+
func NewHandler(db *sql.DB, transform func(s string) string, cache q.TxCache, generate func(context.Context) (string, error), options... func(context.Context, string, ...map[string]interface{})) *Handler {
25+
var logError func(context.Context, string, ...map[string]interface{})
2626
if len(options) >= 1 {
2727
logError = options[0]
2828
}
@@ -300,7 +300,7 @@ func (h *Handler) ExecBatch(ctx echo.Context) error {
300300
return ctx.JSON(http.StatusOK, res)
301301
}
302302

303-
func handleError(ctx echo.Context, code int, result interface{}, logError func(context.Context, string), err error) {
303+
func handleError(ctx echo.Context, code int, result interface{}, logError func(context.Context, string, ...map[string]interface{}), err error) {
304304
if logError != nil {
305305
logError(ctx.Request().Context(), err.Error())
306306
}

echo_v3/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ type Handler struct {
1717
Transform func(s string) string
1818
Cache q.TxCache
1919
Generate func(ctx context.Context) (string, error)
20-
Error func(context.Context, string)
20+
Error func(context.Context, string, ...map[string]interface{})
2121
}
2222

2323
const d = 120 * time.Second
24-
func NewHandler(db *sql.DB, transform func(s string) string, cache q.TxCache, generate func(context.Context) (string, error), options... func(context.Context, string)) *Handler {
25-
var logError func(context.Context, string)
24+
func NewHandler(db *sql.DB, transform func(s string) string, cache q.TxCache, generate func(context.Context) (string, error), options... func(context.Context, string, ...map[string]interface{})) *Handler {
25+
var logError func(context.Context, string, ...map[string]interface{})
2626
if len(options) >= 1 {
2727
logError = options[0]
2828
}
@@ -300,7 +300,7 @@ func (h *Handler) ExecBatch(ctx echo.Context) error {
300300
return ctx.JSON(http.StatusOK, res)
301301
}
302302

303-
func handleError(ctx echo.Context, code int, result interface{}, logError func(context.Context, string), err error) {
303+
func handleError(ctx echo.Context, code int, result interface{}, logError func(context.Context, string, ...map[string]interface{}), err error) {
304304
if logError != nil {
305305
logError(ctx.Request().Context(), err.Error())
306306
}

gin/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ type Handler struct {
1616
Transform func(s string) string
1717
Cache q.TxCache
1818
Generate func(ctx context.Context) (string, error)
19-
Error func(context.Context, string)
19+
Error func(context.Context, string, ...map[string]interface{})
2020
}
2121

2222
const d = 120 * time.Second
23-
func NewHandler(db *sql.DB, transform func(s string) string, cache q.TxCache, generate func(context.Context) (string, error), options... func(context.Context, string)) *Handler {
24-
var logError func(context.Context, string)
23+
func NewHandler(db *sql.DB, transform func(s string) string, cache q.TxCache, generate func(context.Context) (string, error), options... func(context.Context, string, ...map[string]interface{})) *Handler {
24+
var logError func(context.Context, string, ...map[string]interface{})
2525
if len(options) >= 1 {
2626
logError = options[0]
2727
}
@@ -297,7 +297,7 @@ func (h *Handler) ExecBatch(ctx *gin.Context) {
297297
ctx.JSON(http.StatusOK, res)
298298
}
299299

300-
func handleError(ctx *gin.Context, code int, result interface{}, logError func(context.Context, string), err error) {
300+
func handleError(ctx *gin.Context, code int, result interface{}, logError func(context.Context, string, ...map[string]interface{}), err error) {
301301
if logError != nil {
302302
logError(ctx.Request.Context(), err.Error())
303303
}

grpc-server/grpc.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@ type GRPCHandler struct {
2020
Transform func(s string) string
2121
Cache q.TxCache
2222
Generate func(ctx context.Context) (string, error)
23-
Error func(context.Context, string)
23+
Error func(context.Context, string, ...map[string]interface{})
2424
}
2525

26-
func NewHandler(db *sql.DB, transform func(s string) string, cache q.TxCache, generate func(ctx context.Context) (string, error), err func(context.Context, string)) *GRPCHandler {
26+
func NewHandler(db *sql.DB, transform func(s string) string, cache q.TxCache, generate func(ctx context.Context) (string, error), options... func(context.Context, string, ...map[string]interface{})) *GRPCHandler {
27+
var logError func(context.Context, string, ...map[string]interface{})
28+
if len(options) >= 1 {
29+
logError = options[0]
30+
}
2731
g := GRPCHandler{
2832
Transform: transform,
2933
Cache: cache,
3034
DB: db,
3135
Generate: generate,
32-
Error: err,
36+
Error: logError,
3337
}
3438
return &g
3539
}

handler/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ type Handler struct {
1616
Transform func(s string) string
1717
Cache q.TxCache
1818
Generate func(ctx context.Context) (string, error)
19-
Error func(context.Context, string)
19+
Error func(context.Context, string, ...map[string]interface{})
2020
}
2121

2222
const d = 120 * time.Second
23-
func NewHandler(db *sql.DB, transform func(s string) string, cache q.TxCache, generate func(context.Context) (string, error), options... func(context.Context, string)) *Handler {
24-
var logError func(context.Context, string)
23+
func NewHandler(db *sql.DB, transform func(s string) string, cache q.TxCache, generate func(context.Context) (string, error), options... func(context.Context, string, ...map[string]interface{})) *Handler {
24+
var logError func(context.Context, string, ...map[string]interface{})
2525
if len(options) >= 1 {
2626
logError = options[0]
2727
}
@@ -291,7 +291,7 @@ func (h *Handler) ExecBatch(w http.ResponseWriter, r *http.Request) {
291291
respond(w, http.StatusOK, res)
292292
}
293293

294-
func handleError(w http.ResponseWriter, r *http.Request, code int, result interface{}, logError func(context.Context, string), err error) {
294+
func handleError(w http.ResponseWriter, r *http.Request, code int, result interface{}, logError func(context.Context, string, ...map[string]interface{}), err error) {
295295
if logError != nil {
296296
logError(r.Context(), err.Error())
297297
}

query/echo/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ const internalServerError = "Internal Server Error"
1111

1212
type QueryHandler struct {
1313
Load func(ctx context.Context, key string, max int64) ([]string, error)
14-
LogError func(context.Context, string)
14+
LogError func(context.Context, string, ...map[string]interface{})
1515
Keyword string
1616
Max string
1717
}
1818

19-
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]string, error), logError func(context.Context, string), opts ...string) *QueryHandler {
19+
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]string, error), logError func(context.Context, string, ...map[string]interface{}), opts ...string) *QueryHandler {
2020
keyword := "keyword"
2121
if len(opts) > 0 && len(opts[0]) > 0 {
2222
keyword = opts[0]

query/echo_v3/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ const internalServerError = "Internal Server Error"
1111

1212
type QueryHandler struct {
1313
Load func(ctx context.Context, key string, max int64) ([]string, error)
14-
LogError func(context.Context, string)
14+
LogError func(context.Context, string, ...map[string]interface{})
1515
Keyword string
1616
Max string
1717
}
1818

19-
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]string, error), logError func(context.Context, string), opts ...string) *QueryHandler {
19+
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]string, error), logError func(context.Context, string, ...map[string]interface{}), opts ...string) *QueryHandler {
2020
keyword := "keyword"
2121
if len(opts) > 0 && len(opts[0]) > 0 {
2222
keyword = opts[0]

query/gin/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ const internalServerError = "Internal Server Error"
1111

1212
type QueryHandler struct {
1313
Load func(ctx context.Context, key string, max int64) ([]string, error)
14-
LogError func(context.Context, string)
14+
LogError func(context.Context, string, ...map[string]interface{})
1515
Keyword string
1616
Max string
1717
}
1818

19-
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]string, error), logError func(context.Context, string), opts ...string) *QueryHandler {
19+
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]string, error), logError func(context.Context, string, ...map[string]interface{}), opts ...string) *QueryHandler {
2020
keyword := "keyword"
2121
if len(opts) > 0 && len(opts[0]) > 0 {
2222
keyword = opts[0]

query/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ const internalServerError = "Internal Server Error"
1111

1212
type QueryHandler struct {
1313
Load func(ctx context.Context, key string, max int64) ([]string, error)
14-
LogError func(context.Context, string)
14+
LogError func(context.Context, string, ...map[string]interface{})
1515
Keyword string
1616
Max string
1717
}
1818

19-
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]string, error), logError func(context.Context, string), opts ...string) *QueryHandler {
19+
func NewQueryHandler(load func(ctx context.Context, key string, max int64) ([]string, error), logError func(context.Context, string, ...map[string]interface{}), opts ...string) *QueryHandler {
2020
keyword := "keyword"
2121
if len(opts) > 0 && len(opts[0]) > 0 {
2222
keyword = opts[0]
@@ -47,7 +47,7 @@ func (h *QueryHandler) Query(w http.ResponseWriter, r *http.Request) {
4747
}
4848
}
4949

50-
func respondModel(w http.ResponseWriter, r *http.Request, model interface{}, err error, logError func(context.Context, string), writeLog func(context.Context, string, string, bool, string) error, options... string) {
50+
func respondModel(w http.ResponseWriter, r *http.Request, model interface{}, err error, logError func(context.Context, string, ...map[string]interface{}), writeLog func(context.Context, string, string, bool, string) error, options... string) {
5151
var resource, action string
5252
if len(options) > 0 && len(options[0]) > 0 {
5353
resource = options[0]
@@ -65,7 +65,7 @@ func respondModel(w http.ResponseWriter, r *http.Request, model interface{}, err
6565
}
6666
}
6767
}
68-
func respondAndLog(w http.ResponseWriter, r *http.Request, code int, result interface{}, err error, logError func(context.Context, string), writeLog func(context.Context, string, string, bool, string) error, options... string) error {
68+
func respondAndLog(w http.ResponseWriter, r *http.Request, code int, result interface{}, err error, logError func(context.Context, string, ...map[string]interface{}), writeLog func(context.Context, string, string, bool, string) error, options... string) error {
6969
var resource, action string
7070
if len(options) > 0 && len(options[0]) > 0 {
7171
resource = options[0]

0 commit comments

Comments
 (0)