Skip to content

Commit 842edf6

Browse files
committed
Refactor constructors
1 parent 8a9a302 commit 842edf6

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

appr_handler.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type ApprHandler struct {
1212
Error func(context.Context, string)
1313
ApprService ApprService
1414
ModelType reflect.Type
15-
IdNames []string
15+
Keys []string
1616
Indexes map[string]int
1717
Offset int
1818
Log func(ctx context.Context, resource string, action string, success bool, desc string) error
@@ -26,24 +26,24 @@ func NewApprHandler(apprService ApprService, modelType reflect.Type, logError fu
2626
if len(option) > 0 && option[0] >= 0 {
2727
offset = option[0]
2828
}
29-
return NewApprHandlerWithKeysAndLog(apprService, modelType, offset, logError, nil, nil)
29+
return NewApprHandlerWithKeysAndLog(apprService, nil, modelType, offset, logError, nil)
3030
}
3131
func NewApprHandlerWithLogs(apprService ApprService, modelType reflect.Type, offset int, logError func(context.Context, string), writeLog func(context.Context, string, string, bool, string) error, options ...string) *ApprHandler {
32-
return NewApprHandlerWithKeysAndLog(apprService, modelType, offset, logError, nil, writeLog, options...)
32+
return NewApprHandlerWithKeysAndLog(apprService, nil, modelType, offset, logError, writeLog, options...)
3333
}
3434
func NewApprHandlerWithKeys(apprService ApprService, modelType reflect.Type, logError func(context.Context, string), idNames []string, option ...int) *ApprHandler {
3535
offset := 1
3636
if len(option) > 0 && option[0] >= 0 {
3737
offset = option[0]
3838
}
39-
return NewApprHandlerWithKeysAndLog(apprService, modelType, offset, logError, idNames, nil)
39+
return NewApprHandlerWithKeysAndLog(apprService, idNames, modelType, offset, logError, nil)
4040
}
41-
func NewApprHandlerWithKeysAndLog(apprService ApprService, modelType reflect.Type, offset int, logError func(context.Context, string), idNames []string, writeLog func(context.Context, string, string, bool, string) error, options ...string) *ApprHandler {
41+
func NewApprHandlerWithKeysAndLog(apprService ApprService, keys []string, modelType reflect.Type, offset int, logError func(context.Context, string), writeLog func(context.Context, string, string, bool, string) error, options ...string) *ApprHandler {
4242
if offset < 0 {
4343
offset = 1
4444
}
45-
if idNames == nil || len(idNames) == 0 {
46-
idNames = getJsonPrimaryKeys(modelType)
45+
if keys == nil || len(keys) == 0 {
46+
keys = getJsonPrimaryKeys(modelType)
4747
}
4848
indexes := getIndexes(modelType)
4949
var resource, action1, action2 string
@@ -62,7 +62,7 @@ func NewApprHandlerWithKeysAndLog(apprService ApprService, modelType reflect.Typ
6262
} else {
6363
resource = buildResourceName(modelType.Name())
6464
}
65-
return &ApprHandler{Log: writeLog, ApprService: apprService, ModelType: modelType, IdNames: idNames, Indexes: indexes, Offset: offset, Error: logError, Resource: resource, Action1: action1, Action2: action2}
65+
return &ApprHandler{Log: writeLog, ApprService: apprService, ModelType: modelType, Keys: keys, Indexes: indexes, Offset: offset, Error: logError, Resource: resource, Action1: action1, Action2: action2}
6666
}
6767

6868
func (c *ApprHandler) newModel(body interface{}) (out interface{}) {
@@ -81,7 +81,7 @@ func (c *ApprHandler) newModel(body interface{}) (out interface{}) {
8181
}
8282

8383
func (c *ApprHandler) Approve(w http.ResponseWriter, r *http.Request) {
84-
id, err := buildId(r, c.ModelType, c.IdNames, c.Indexes, c.Offset)
84+
id, err := buildId(r, c.ModelType, c.Keys, c.Indexes, c.Offset)
8585
if err != nil {
8686
http.Error(w, err.Error(), http.StatusBadRequest)
8787
} else {
@@ -95,7 +95,7 @@ func (c *ApprHandler) Approve(w http.ResponseWriter, r *http.Request) {
9595
}
9696

9797
func (c *ApprHandler) Reject(w http.ResponseWriter, r *http.Request) {
98-
id, err := buildId(r, c.ModelType, c.IdNames, c.Indexes, c.Offset)
98+
id, err := buildId(r, c.ModelType, c.Keys, c.Indexes, c.Offset)
9999
if err != nil {
100100
http.Error(w, err.Error(), http.StatusBadRequest)
101101
} else {

appr_list_handler.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ type ApprListHandler struct {
1010
Error func(context.Context, string)
1111
ApprListService ApprListService
1212
ModelType reflect.Type
13-
IdNames []string
13+
Keys []string
1414
Log func(ctx context.Context, resource string, action string, success bool, desc string) error
1515
Resource string
1616
Action1 string
1717
Action2 string
1818
}
1919

2020
func NewApprListHandler(apprListService ApprListService, modelType reflect.Type, logError func(context.Context, string), writeLog func(context.Context, string, string, bool, string) error, options ...string) *ApprListHandler {
21-
return NewApprListHandlerWithKeys(apprListService, modelType, logError, nil, writeLog, options...)
21+
return NewApprListHandlerWithKeys(apprListService, nil, modelType, logError, writeLog, options...)
2222
}
2323

24-
func NewApprListHandlerWithKeys(apprListService ApprListService, modelType reflect.Type, logError func(context.Context, string), idNames []string, writeLog func(context.Context, string, string, bool, string) error, options ...string) *ApprListHandler {
25-
if len(idNames) == 0 {
26-
idNames = getJsonPrimaryKeys(modelType)
24+
func NewApprListHandlerWithKeys(apprListService ApprListService, keys []string, modelType reflect.Type, logError func(context.Context, string), writeLog func(context.Context, string, string, bool, string) error, options ...string) *ApprListHandler {
25+
if keys == nil || len(keys) == 0 {
26+
keys = getJsonPrimaryKeys(modelType)
2727
}
2828
var resource, action1, action2 string
2929
if len(options) > 0 && len(options[0]) > 0 {
@@ -41,11 +41,11 @@ func NewApprListHandlerWithKeys(apprListService ApprListService, modelType refle
4141
} else {
4242
resource = buildResourceName(modelType.Name())
4343
}
44-
return &ApprListHandler{ApprListService: apprListService, ModelType: modelType, IdNames: idNames, Resource: resource, Error: logError, Log: writeLog, Action1: action1, Action2: action2}
44+
return &ApprListHandler{ApprListService: apprListService, ModelType: modelType, Keys: keys, Resource: resource, Error: logError, Log: writeLog, Action1: action1, Action2: action2}
4545
}
4646

4747
func (c *ApprListHandler) Approve(w http.ResponseWriter, r *http.Request) {
48-
ids, err := buildIds(r, c.ModelType, c.IdNames)
48+
ids, err := buildIds(r, c.ModelType, c.Keys)
4949
if err != nil {
5050
http.Error(w, err.Error(), http.StatusBadRequest)
5151
} else {
@@ -59,7 +59,7 @@ func (c *ApprListHandler) Approve(w http.ResponseWriter, r *http.Request) {
5959
}
6060

6161
func (c *ApprListHandler) Reject(w http.ResponseWriter, r *http.Request) {
62-
ids, err := buildIds(r, c.ModelType, c.IdNames)
62+
ids, err := buildIds(r, c.ModelType, c.Keys)
6363
if err != nil {
6464
http.Error(w, err.Error(), http.StatusBadRequest)
6565
} else {

diff_handler.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ 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-
Error func(context.Context, string)
1918
DiffService DiffService
19+
Keys []string
2020
ModelType reflect.Type
21-
IdNames []string
21+
Error func(context.Context, string)
2222
Indexes map[string]int
2323
Offset int
2424
Log func(ctx context.Context, resource string, action string, success bool, desc string) error
@@ -28,15 +28,15 @@ type DiffHandler struct {
2828
}
2929

3030
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, modelType, logError, nil, config, writeLog, options...)
31+
return NewDiffHandlerWithKeys(diffService, nil, modelType, logError, config, writeLog, options...)
3232
}
33-
func NewDiffHandlerWithKeys(diffService DiffService, modelType reflect.Type, logError func(context.Context, string), idNames []string, config *DiffModelConfig, writeLog func(context.Context, string, string, bool, string) error, options ...int) *DiffHandler {
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 {
3434
offset := 1
3535
if len(options) > 0 {
3636
offset = options[0]
3737
}
38-
if idNames == nil || len(idNames) == 0 {
39-
idNames = getJsonPrimaryKeys(modelType)
38+
if keys == nil || len(keys) == 0 {
39+
keys = getJsonPrimaryKeys(modelType)
4040
}
4141
indexes := getIndexes(modelType)
4242
var resource, action string
@@ -50,11 +50,11 @@ func NewDiffHandlerWithKeys(diffService DiffService, modelType reflect.Type, log
5050
if len(action) == 0 {
5151
action = "diff"
5252
}
53-
return &DiffHandler{Log: writeLog, DiffService: diffService, ModelType: modelType, IdNames: idNames, Indexes: indexes, Resource: resource, Offset: offset, Config: config, Error: logError}
53+
return &DiffHandler{Log: writeLog, DiffService: diffService, 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) {
57-
id, err := buildId(r, c.ModelType, c.IdNames, c.Indexes, c.Offset)
57+
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 {

diff_list_handler.go

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

1010
type DiffListHandler struct {
11-
Error func(context.Context, string)
1211
DiffListService DiffListService
12+
Keys []string
1313
ModelType reflect.Type
1414
modelTypeId reflect.Type
15-
IdNames []string
15+
Error func(context.Context, string)
1616
Log func(ctx context.Context, resource string, action string, success bool, desc string) error
1717
Resource string
1818
Action string
1919
Config *DiffModelConfig
2020
}
2121

2222
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, modelType, logError, nil, config, writeLog)
23+
return NewDiffListHandlerWithKeys(diffListService, nil, modelType, logError, config, writeLog)
2424
}
25-
func NewDiffListHandlerWithKeys(diffListService DiffListService, modelType reflect.Type, logError func(context.Context, string), idNames []string, config *DiffModelConfig, writeLog func(context.Context, string, string, bool, string) error) *DiffListHandler {
26-
if idNames == nil || len(idNames) == 0 {
27-
idNames = getJsonPrimaryKeys(modelType)
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 {
26+
if keys == nil || len(keys) == 0 {
27+
keys = getJsonPrimaryKeys(modelType)
2828
}
29-
modelTypeId := newModelTypeID(modelType, idNames)
29+
modelTypeId := newModelTypeID(modelType, keys)
3030
var resource, action string
3131
if config != nil {
3232
resource = config.Resource
@@ -38,11 +38,11 @@ func NewDiffListHandlerWithKeys(diffListService DiffListService, modelType refle
3838
if len(action) == 0 {
3939
action = "diff"
4040
}
41-
return &DiffListHandler{Log: writeLog, DiffListService: diffListService, ModelType: modelType, modelTypeId: modelTypeId, IdNames: idNames, Resource: resource, Action: action, Config: config, Error: logError}
41+
return &DiffListHandler{Log: writeLog, DiffListService: diffListService, 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) {
45-
ids, err := buildIds(r, c.modelTypeId, c.IdNames)
45+
ids, err := buildIds(r, c.modelTypeId, c.Keys)
4646
if err != nil {
4747
http.Error(w, err.Error(), http.StatusBadRequest)
4848
} else {

0 commit comments

Comments
 (0)