Skip to content

Commit 6c28d4f

Browse files
committed
Replace interface by function
1 parent d12ede8 commit 6c28d4f

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

diff_handler.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ type DiffModelConfig struct {
1515
Action string `mapstructure:"action" json:"action,omitempty" gorm:"column:action" bson:"action,omitempty" dynamodbav:"action,omitempty" firestore:"action,omitempty"`
1616
}
1717
type DiffHandler struct {
18-
DiffService DiffService
19-
Keys []string
20-
ModelType reflect.Type
21-
Error func(context.Context, string)
22-
Indexes map[string]int
23-
Offset int
24-
Log func(ctx context.Context, resource string, action string, success bool, desc string) error
25-
Resource string
26-
Action string
27-
Config *DiffModelConfig
18+
GetDiff func(ctx context.Context, id interface{}) (*DiffModel, error)
19+
Keys []string
20+
ModelType reflect.Type
21+
Error func(context.Context, string)
22+
Indexes map[string]int
23+
Offset int
24+
Log func(ctx context.Context, resource string, action string, success bool, desc string) error
25+
Resource string
26+
Action string
27+
Config *DiffModelConfig
2828
}
2929

30-
func NewDiffHandler(diffService DiffService, modelType reflect.Type, logError func(context.Context, string), config *DiffModelConfig, writeLog func(context.Context, string, string, bool, string) error, options ...int) *DiffHandler {
31-
return NewDiffHandlerWithKeys(diffService, nil, modelType, logError, config, writeLog, options...)
30+
func NewDiffHandler(diff func(context.Context, interface{}) (*DiffModel, error), modelType reflect.Type, logError func(context.Context, string), config *DiffModelConfig, writeLog func(context.Context, string, string, bool, string) error, options ...int) *DiffHandler {
31+
return NewDiffHandlerWithKeys(diff, nil, modelType, logError, config, writeLog, options...)
3232
}
33-
func NewDiffHandlerWithKeys(diffService DiffService, keys []string, modelType reflect.Type, logError func(context.Context, string), config *DiffModelConfig, writeLog func(context.Context, string, string, bool, string) error, options ...int) *DiffHandler {
33+
func NewDiffHandlerWithKeys(diff func(context.Context, interface{}) (*DiffModel, error), keys []string, modelType reflect.Type, logError func(context.Context, string), config *DiffModelConfig, writeLog func(context.Context, string, string, bool, string) error, options ...int) *DiffHandler {
3434
offset := 1
3535
if len(options) > 0 {
3636
offset = options[0]
@@ -50,15 +50,15 @@ func NewDiffHandlerWithKeys(diffService DiffService, keys []string, modelType re
5050
if len(action) == 0 {
5151
action = "diff"
5252
}
53-
return &DiffHandler{Log: writeLog, DiffService: diffService, ModelType: modelType, Keys: keys, Indexes: indexes, Resource: resource, Offset: offset, Config: config, Error: logError}
53+
return &DiffHandler{Log: writeLog, GetDiff: diff, ModelType: modelType, Keys: keys, Indexes: indexes, Resource: resource, Offset: offset, Config: config, Error: logError}
5454
}
5555

5656
func (c *DiffHandler) Diff(w http.ResponseWriter, r *http.Request) {
5757
id, err := buildId(r, c.ModelType, c.Keys, c.Indexes, c.Offset)
5858
if err != nil {
5959
http.Error(w, err.Error(), http.StatusBadRequest)
6060
} else {
61-
result, err := c.DiffService.Diff(r.Context(), id)
61+
result, err := c.GetDiff(r.Context(), id)
6262
if err != nil {
6363
handleError(w, r, http.StatusInternalServerError, internalServerError, c.Error, c.Resource, c.Action, err, c.Log)
6464
} else {

diff_list_handler.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ import (
88
)
99

1010
type DiffListHandler struct {
11-
DiffListService DiffListService
12-
Keys []string
13-
ModelType reflect.Type
14-
modelTypeId reflect.Type
15-
Error func(context.Context, string)
16-
Log func(ctx context.Context, resource string, action string, success bool, desc string) error
17-
Resource string
18-
Action string
19-
Config *DiffModelConfig
11+
GetDiff func(ctx context.Context, ids interface{}) (*[]DiffModel, error)
12+
Keys []string
13+
ModelType reflect.Type
14+
modelTypeId reflect.Type
15+
Error func(context.Context, string)
16+
Log func(ctx context.Context, resource string, action string, success bool, desc string) error
17+
Resource string
18+
Action string
19+
Config *DiffModelConfig
2020
}
2121

22-
func NewDiffListHandler(diffListService DiffListService, modelType reflect.Type, logError func(context.Context, string), config *DiffModelConfig, writeLog func(context.Context, string, string, bool, string) error) *DiffListHandler {
23-
return NewDiffListHandlerWithKeys(diffListService, nil, modelType, logError, config, writeLog)
22+
func NewDiffListHandler(diff func(context.Context, interface{}) (*[]DiffModel, error), modelType reflect.Type, logError func(context.Context, string), config *DiffModelConfig, writeLog func(context.Context, string, string, bool, string) error) *DiffListHandler {
23+
return NewDiffListHandlerWithKeys(diff, nil, modelType, logError, config, writeLog)
2424
}
25-
func NewDiffListHandlerWithKeys(diffListService DiffListService, keys []string, modelType reflect.Type, logError func(context.Context, string), config *DiffModelConfig, writeLog func(context.Context, string, string, bool, string) error) *DiffListHandler {
25+
func NewDiffListHandlerWithKeys(diff func(context.Context, interface{}) (*[]DiffModel, error), keys []string, modelType reflect.Type, logError func(context.Context, string), config *DiffModelConfig, writeLog func(context.Context, string, string, bool, string) error) *DiffListHandler {
2626
if keys == nil || len(keys) == 0 {
2727
keys = getJsonPrimaryKeys(modelType)
2828
}
@@ -38,15 +38,15 @@ func NewDiffListHandlerWithKeys(diffListService DiffListService, keys []string,
3838
if len(action) == 0 {
3939
action = "diff"
4040
}
41-
return &DiffListHandler{Log: writeLog, DiffListService: diffListService, ModelType: modelType, modelTypeId: modelTypeId, Keys: keys, Resource: resource, Action: action, Config: config, Error: logError}
41+
return &DiffListHandler{Log: writeLog, GetDiff: diff, ModelType: modelType, modelTypeId: modelTypeId, Keys: keys, Resource: resource, Action: action, Config: config, Error: logError}
4242
}
4343

4444
func (c *DiffListHandler) DiffList(w http.ResponseWriter, r *http.Request) {
4545
ids, err := buildIds(r, c.modelTypeId, c.Keys)
4646
if err != nil {
4747
http.Error(w, err.Error(), http.StatusBadRequest)
4848
} else {
49-
list, err := c.DiffListService.Diff(r.Context(), ids)
49+
list, err := c.GetDiff(r.Context(), ids)
5050
if err != nil {
5151
handleError(w, r, http.StatusInternalServerError, internalServerError, c.Error, c.Resource, c.Action, err, c.Log)
5252
} else {

0 commit comments

Comments
 (0)