Skip to content

Commit 4ae50a6

Browse files
committed
Add config
1 parent 85dc586 commit 4ae50a6

File tree

3 files changed

+46
-42
lines changed

3 files changed

+46
-42
lines changed

code.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ const (
1919
DriverNotSupport = "no support"
2020
)
2121

22-
type CodeModel struct {
22+
type Model struct {
2323
Id string `mapstructure:"id" json:"id,omitempty" gorm:"column:id" bson:"id,omitempty" dynamodbav:"id,omitempty" firestore:"id,omitempty"`
2424
Code string `mapstructure:"code" json:"code,omitempty" gorm:"column:code" bson:"code,omitempty" dynamodbav:"code,omitempty" firestore:"code,omitempty"`
2525
Value string `mapstructure:"value" json:"value,omitempty" gorm:"column:value" bson:"value,omitempty" dynamodbav:"value,omitempty" firestore:"value,omitempty"`
2626
Name string `mapstructure:"name" json:"name,omitempty" gorm:"column:name" bson:"name,omitempty" dynamodbav:"name,omitempty" firestore:"name,omitempty"`
2727
Text string `mapstructure:"text" json:"text,omitempty" gorm:"column:text" bson:"text,omitempty" dynamodbav:"text,omitempty" firestore:"text,omitempty"`
2828
Sequence int32 `mapstructure:"sequence" json:"sequence,omitempty" gorm:"column:sequence" bson:"sequence,omitempty" dynamodbav:"sequence,omitempty" firestore:"sequence,omitempty"`
2929
}
30-
type CodeConfig struct {
30+
type StructureConfig struct {
3131
Master string `mapstructure:"master" json:"master,omitempty" gorm:"column:master" bson:"master,omitempty" dynamodbav:"master,omitempty" firestore:"master,omitempty"`
3232
Id string `mapstructure:"id" json:"id,omitempty" gorm:"column:id" bson:"id,omitempty" dynamodbav:"id,omitempty" firestore:"id,omitempty"`
3333
Code string `mapstructure:"code" json:"code,omitempty" gorm:"column:code" bson:"code,omitempty" dynamodbav:"code,omitempty" firestore:"code,omitempty"`
@@ -38,25 +38,25 @@ type CodeConfig struct {
3838
Status string `mapstructure:"status" json:"status,omitempty" gorm:"column:status" bson:"status,omitempty" dynamodbav:"status,omitempty" firestore:"status,omitempty"`
3939
Active interface{} `mapstructure:"active" json:"active,omitempty" gorm:"column:active" bson:"active,omitempty" dynamodbav:"active,omitempty" firestore:"active,omitempty"`
4040
}
41-
type CodeLoader interface {
42-
Load(ctx context.Context, master string) ([]CodeModel, error)
41+
type Loader interface {
42+
Load(ctx context.Context, master string) ([]Model, error)
4343
}
44-
type SqlCodeLoader struct {
44+
type SqlLoader struct {
4545
DB *sql.DB
4646
Table string
47-
Config CodeConfig
47+
Config StructureConfig
4848
Build func(i int) string
4949
Map func(col string) string
5050
}
51-
type DynamicSqlCodeLoader struct {
51+
type DynamicSqlLoader struct {
5252
DB *sql.DB
5353
Query string
5454
ParameterCount int
5555
Map func(col string) string
5656
driver string
5757
}
5858

59-
func NewDefaultDynamicSqlCodeLoader(db *sql.DB, query string, options ...int) *DynamicSqlCodeLoader {
59+
func NewDefaultDynamicSqlCodeLoader(db *sql.DB, query string, options ...int) *DynamicSqlLoader {
6060
var parameterCount int
6161
if len(options) > 0 {
6262
parameterCount = options[0]
@@ -65,11 +65,13 @@ func NewDefaultDynamicSqlCodeLoader(db *sql.DB, query string, options ...int) *D
6565
}
6666
return NewDynamicSqlCodeLoader(db, query, parameterCount, true)
6767
}
68-
func NewDynamicSqlCodeLoader(db *sql.DB, query string, parameterCount int, options ...bool) *DynamicSqlCodeLoader {
68+
func NewDynamicSqlCodeLoader(db *sql.DB, query string, parameterCount int, options ...bool) *DynamicSqlLoader {
6969
driver := getDriver(db)
7070
var mp func(string) string
7171
if driver == DriverOracle {
7272
mp = strings.ToUpper
73+
} else {
74+
mp = strings.ToLower
7375
}
7476
if parameterCount < 0 {
7577
parameterCount = 1
@@ -96,10 +98,10 @@ func NewDynamicSqlCodeLoader(db *sql.DB, query string, parameterCount int, optio
9698
}
9799
}
98100
}
99-
return &DynamicSqlCodeLoader{DB: db, Query: query, ParameterCount: parameterCount, Map: mp}
101+
return &DynamicSqlLoader{DB: db, Query: query, ParameterCount: parameterCount, Map: mp}
100102
}
101-
func (l DynamicSqlCodeLoader) Load(ctx context.Context, master string) ([]CodeModel, error) {
102-
models := make([]CodeModel, 0)
103+
func (l DynamicSqlLoader) Load(ctx context.Context, master string) ([]Model, error) {
104+
models := make([]Model, 0)
103105

104106
var rows *sql.Rows
105107
var er1 error
@@ -122,7 +124,7 @@ func (l DynamicSqlCodeLoader) Load(ctx context.Context, master string) ([]CodeMo
122124
return models, er2
123125
}
124126
// get list indexes column
125-
modelType := reflect.TypeOf(CodeModel{})
127+
modelType := reflect.TypeOf(Model{})
126128

127129
fieldsIndexSelected := make([]int, 0)
128130
fieldsIndex, er3 := getColumnIndexes(modelType, l.Map)
@@ -139,13 +141,13 @@ func (l DynamicSqlCodeLoader) Load(ctx context.Context, master string) ([]CodeMo
139141
return models, er4
140142
}
141143
for _, v := range tb {
142-
if c, ok := v.(*CodeModel); ok {
144+
if c, ok := v.(*Model); ok {
143145
models = append(models, *c)
144146
}
145147
}
146148
return models, nil
147149
}
148-
func NewSqlCodeLoader(db *sql.DB, table string, config CodeConfig, options ...func(i int) string) *SqlCodeLoader {
150+
func NewSqlCodeLoader(db *sql.DB, table string, config StructureConfig, options ...func(i int) string) *SqlLoader {
149151
var build func(i int) string
150152
if len(options) > 0 && options[0] != nil {
151153
build = options[0]
@@ -157,10 +159,10 @@ func NewSqlCodeLoader(db *sql.DB, table string, config CodeConfig, options ...fu
157159
if driver == DriverOracle {
158160
mp = strings.ToUpper
159161
}
160-
return &SqlCodeLoader{DB: db, Table: table, Config: config, Build: build, Map: mp}
162+
return &SqlLoader{DB: db, Table: table, Config: config, Build: build, Map: mp}
161163
}
162-
func (l SqlCodeLoader) Load(ctx context.Context, master string) ([]CodeModel, error) {
163-
models := make([]CodeModel, 0)
164+
func (l SqlLoader) Load(ctx context.Context, master string) ([]Model, error) {
165+
models := make([]Model, 0)
164166
s := make([]string, 0)
165167
values := make([]interface{}, 0)
166168
sql2 := ""
@@ -230,7 +232,7 @@ func (l SqlCodeLoader) Load(ctx context.Context, master string) ([]CodeModel, er
230232
return nil, er1
231233
}
232234
fieldsIndexSelected := make([]int, 0)
233-
modelType := reflect.TypeOf(CodeModel{})
235+
modelType := reflect.TypeOf(Model{})
234236
// get list indexes column
235237
fieldsIndex, er3 := getColumnIndexes(modelType, l.Map)
236238
if er3 != nil {
@@ -246,7 +248,7 @@ func (l SqlCodeLoader) Load(ctx context.Context, master string) ([]CodeModel, er
246248
return nil, er3
247249
}
248250
for _, v := range tb {
249-
if c, ok := v.(*CodeModel); ok {
251+
if c, ok := v.(*Model); ok {
250252
models = append(models, *c)
251253
}
252254
}

config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package code
2+
3+
type Config struct {
4+
Handler HandlerConfig `mapstructure:"handler"`
5+
Loader StructureConfig `mapstructure:"loader"`
6+
}

handler.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import (
1010

1111
const internalServerError = "Internal Server Error"
1212

13-
type CodeHandlerConfig struct {
13+
type HandlerConfig struct {
1414
Master *bool `mapstructure:"master" json:"master,omitempty" gorm:"column:master" bson:"master,omitempty" dynamodbav:"master,omitempty" firestore:"master,omitempty"`
1515
Id string `mapstructure:"id" json:"id,omitempty" gorm:"column:id" bson:"id,omitempty" dynamodbav:"id,omitempty" firestore:"id,omitempty"`
1616
Name string `mapstructure:"name" json:"name,omitempty" gorm:"column:name" bson:"name,omitempty" dynamodbav:"name,omitempty" firestore:"name,omitempty"`
1717
Resource string `mapstructure:"resource" json:"resource,omitempty" gorm:"column:resource" bson:"resource,omitempty" dynamodbav:"resource,omitempty" firestore:"resource,omitempty"`
1818
Action string `mapstructure:"action" json:"action,omitempty" gorm:"column:action" bson:"action,omitempty" dynamodbav:"action,omitempty" firestore:"action,omitempty"`
1919
}
20-
type CodeHandler struct {
21-
Codes func(ctx context.Context, master string) ([]CodeModel, error)
20+
type Handler struct {
21+
Codes func(ctx context.Context, master string) ([]Model, error)
2222
RequiredMaster bool
2323
Error func(context.Context, string)
2424
Log func(ctx context.Context, resource string, action string, success bool, desc string) error
@@ -28,14 +28,14 @@ type CodeHandler struct {
2828
Name string
2929
}
3030

31-
func NewDefaultCodeHandler(load func(ctx context.Context, master string) ([]CodeModel, error), logError func(context.Context, string), options ...func(context.Context, string, string, bool, string) error) *CodeHandler {
31+
func NewDefaultCodeHandler(load func(ctx context.Context, master string) ([]Model, error), logError func(context.Context, string), options ...func(context.Context, string, string, bool, string) error) *Handler {
3232
var writeLog func(context.Context, string, string, bool, string) error
3333
if len(options) >= 1 {
3434
writeLog = options[0]
3535
}
3636
return NewCodeHandlerWithLog(load, logError, true, writeLog, "", "")
3737
}
38-
func NewCodeHandlerByConfig(load func(ctx context.Context, master string) ([]CodeModel, error), c CodeHandlerConfig, logError func(context.Context, string), options ...func(context.Context, string, string, bool, string) error) *CodeHandler {
38+
func NewCodeHandlerByConfig(load func(ctx context.Context, master string) ([]Model, error), c HandlerConfig, logError func(context.Context, string), options ...func(context.Context, string, string, bool, string) error) *Handler {
3939
var requireMaster bool
4040
if c.Master != nil {
4141
requireMaster = *c.Master
@@ -51,14 +51,14 @@ func NewCodeHandlerByConfig(load func(ctx context.Context, master string) ([]Cod
5151
h.Name = c.Name
5252
return h
5353
}
54-
func NewCodeHandler(load func(ctx context.Context, master string) ([]CodeModel, error), logError func(context.Context, string), requiredMaster bool, options ...func(context.Context, string, string, bool, string) error) *CodeHandler {
54+
func NewCodeHandler(load func(ctx context.Context, master string) ([]Model, error), logError func(context.Context, string), requiredMaster bool, options ...func(context.Context, string, string, bool, string) error) *Handler {
5555
var writeLog func(context.Context, string, string, bool, string) error
5656
if len(options) >= 1 {
5757
writeLog = options[0]
5858
}
5959
return NewCodeHandlerWithLog(load, logError, requiredMaster, writeLog, "", "")
6060
}
61-
func NewCodeHandlerWithLog(load func(ctx context.Context, master string) ([]CodeModel, error), logError func(context.Context, string), requiredMaster bool, writeLog func(context.Context, string, string, bool, string) error, options ...string) *CodeHandler {
61+
func NewCodeHandlerWithLog(load func(ctx context.Context, master string) ([]Model, error), logError func(context.Context, string), requiredMaster bool, writeLog func(context.Context, string, string, bool, string) error, options ...string) *Handler {
6262
var resource, action string
6363
if len(options) >= 1 && len(options[0]) > 0 {
6464
resource = options[0]
@@ -70,12 +70,12 @@ func NewCodeHandlerWithLog(load func(ctx context.Context, master string) ([]Code
7070
} else {
7171
action = "load"
7272
}
73-
h := CodeHandler{Codes: load, Resource: resource, Action: action, RequiredMaster: requiredMaster, Log: writeLog, Error: logError}
73+
h := Handler{Codes: load, Resource: resource, Action: action, RequiredMaster: requiredMaster, Log: writeLog, Error: logError}
7474
return &h
7575
}
76-
func (c *CodeHandler) Load(w http.ResponseWriter, r *http.Request) {
76+
func (h *Handler) Load(w http.ResponseWriter, r *http.Request) {
7777
code := ""
78-
if c.RequiredMaster {
78+
if h.RequiredMaster {
7979
if r.Method == "GET" {
8080
i := strings.LastIndex(r.RequestURI, "/")
8181
if i >= 0 {
@@ -84,34 +84,30 @@ func (c *CodeHandler) Load(w http.ResponseWriter, r *http.Request) {
8484
} else {
8585
b, er1 := ioutil.ReadAll(r.Body)
8686
if er1 != nil {
87-
respondString(w, r, http.StatusBadRequest, "Body cannot is empty")
87+
http.Error(w, "Body cannot is empty", http.StatusBadRequest)
8888
return
8989
}
9090
code = strings.Trim(string(b), " ")
9191
}
9292
}
93-
result, er4 := c.Codes(r.Context(), code)
93+
result, er4 := h.Codes(r.Context(), code)
9494
if er4 != nil {
95-
respondError(w, r, http.StatusInternalServerError, internalServerError, c.Error, c.Resource, c.Action, er4, c.Log)
95+
respondError(w, r, http.StatusInternalServerError, internalServerError, h.Error, h.Resource, h.Action, er4, h.Log)
9696
} else {
97-
if len(c.Id) == 0 && len(c.Name) == 0 {
98-
succeed(w, r, http.StatusOK, result, c.Log, c.Resource, c.Action)
97+
if len(h.Id) == 0 && len(h.Name) == 0 {
98+
succeed(w, r, http.StatusOK, result, h.Log, h.Resource, h.Action)
9999
} else {
100100
rs := make([]map[string]string, 0)
101101
for _, r := range result {
102102
m := make(map[string]string)
103-
m[c.Id] = r.Id
104-
m[c.Name] = r.Name
103+
m[h.Id] = r.Id
104+
m[h.Name] = r.Name
105105
rs = append(rs, m)
106106
}
107-
succeed(w, r, http.StatusOK, rs, c.Log, c.Resource, c.Action)
107+
succeed(w, r, http.StatusOK, rs, h.Log, h.Resource, h.Action)
108108
}
109109
}
110110
}
111-
func respondString(w http.ResponseWriter, r *http.Request, code int, result string) {
112-
w.WriteHeader(code)
113-
w.Write([]byte(result))
114-
}
115111
func respond(w http.ResponseWriter, r *http.Request, code int, result interface{}, writeLog func(context.Context, string, string, bool, string) error, resource string, action string, success bool, desc string) {
116112
response, _ := json.Marshal(result)
117113
w.Header().Set("Content-Type", "application/json")

0 commit comments

Comments
 (0)