From 738b052aba7f87ec5f856830cc322171d5b9101f Mon Sep 17 00:00:00 2001 From: root Date: Wed, 24 Feb 2021 18:40:50 +0000 Subject: [PATCH] code optimization --- src/agreements_api.go | 36 ++++++----------------------- src/lbasis_api.go | 22 ++++++++---------- src/requests_api.go | 54 +++++++++++++------------------------------ src/utils.go | 18 +++++++-------- 4 files changed, 41 insertions(+), 89 deletions(-) diff --git a/src/agreements_api.go b/src/agreements_api.go index 314913f..51a522e 100644 --- a/src/agreements_api.go +++ b/src/agreements_api.go @@ -78,31 +78,14 @@ func (e mainEnv) agreementAccept(w http.ResponseWriter, r *http.Request, ps http w.Write([]byte(`{"status":"ok"}`)) }() - status := "yes" - agreementmethod := "" - referencecode := "" - lastmodifiedby := "" starttime := int32(0) expiration := int32(0) - if value, ok := records["agreementmethod"]; ok { - if reflect.TypeOf(value).Kind() == reflect.String { - agreementmethod = value.(string) - } - } - if value, ok := records["referencecode"]; ok { - if reflect.TypeOf(value).Kind() == reflect.String { - referencecode = value.(string) - } - } - if value, ok := records["lastmodifiedby"]; ok { - if reflect.TypeOf(value).Kind() == reflect.String { - lastmodifiedby = value.(string) - } - } - if value, ok := records["status"]; ok { - if reflect.TypeOf(value).Kind() == reflect.String { - status = normalizeConsentStatus(value.(string)) - } + referencecode := getStringValue(records["referencecode"]) + lastmodifiedby := getStringValue(records["lastmodifiedby"]) + agreementmethod := getStringValue(records["agreementmethod"]) + status := normalizeConsentStatus(getStringValue(records["status"])) + if len(status) == 0 { + status = "yes" } if value, ok := records["expiration"]; ok { switch records["expiration"].(type) { @@ -202,12 +185,7 @@ func (e mainEnv) agreementWithdraw(w http.ResponseWriter, r *http.Request, ps ht returnError(w, r, "failed to decode request body", 405, err, event) return } - lastmodifiedby := "" - if value, ok := records["lastmodifiedby"]; ok { - if reflect.TypeOf(value).Kind() == reflect.String { - lastmodifiedby = value.(string) - } - } + lastmodifiedby := getStringValue(records["lastmodifiedby"]) selfService := false if value, ok := lbasis["usercontrol"]; ok { if reflect.TypeOf(value).Kind() == reflect.Bool { diff --git a/src/lbasis_api.go b/src/lbasis_api.go index 5d01dba..8792773 100644 --- a/src/lbasis_api.go +++ b/src/lbasis_api.go @@ -25,27 +25,23 @@ func (e mainEnv) createLegalBasis(w http.ResponseWriter, r *http.Request, ps htt returnError(w, r, "failed to decode request body", 405, err, nil) return } - newbrief := getStringValue(records, "brief") + newbrief := getStringValue(records["brief"]) if len(newbrief) > 0 && newbrief != brief { if isValidBrief(newbrief) == false { returnError(w, r, "bad brief format", 405, nil, nil) return } } - status := "active"; - module := getStringValue(records, "module") - fulldesc := getStringValue(records, "fulldesc") - shortdesc := getStringValue(records, "shortdesc") - basistype := getStringValue(records, "basistype") - requiredmsg := getStringValue(records, "requiredmsg") + status := getStringValue(records["status"]) + module := getStringValue(records["module"]) + fulldesc := getStringValue(records["fulldesc"]) + shortdesc := getStringValue(records["shortdesc"]) + basistype := getStringValue(records["basistype"]) + requiredmsg := getStringValue(records["requiredmsg"]) usercontrol := false requiredflag := false - if value, ok := records["status"]; ok { - if reflect.TypeOf(value) == reflect.TypeOf("string") { - if value.(string) == "disabled" { - status = value.(string) - } - } + if status != "disabled" { + status = "active" } if value, ok := records["usercontrol"]; ok { if reflect.TypeOf(value).Kind() == reflect.Bool { diff --git a/src/requests_api.go b/src/requests_api.go index fe2c817..4e14946 100644 --- a/src/requests_api.go +++ b/src/requests_api.go @@ -109,36 +109,18 @@ func (e mainEnv) getUserRequest(w http.ResponseWriter, r *http.Request, ps httpr return } var resultJSON []byte - userTOKEN := "" - appName := "" - brief := "" - change := "" - action := "" - if value, ok := requestInfo["action"]; ok { - action = value.(string) - } - if value, ok := requestInfo["token"]; ok { - userTOKEN = value.(string) + action := getStringValue(requestInfo["action"]) + userTOKEN := getStringValue(requestInfo["token"]) + if len(userTOKEN) != 0 { event.Record = userTOKEN } authResult := e.enforceAuth(w, r, event) if authResult == "" { return } - if value, ok := requestInfo["change"]; ok { - switch value.(type) { - case string: - change = value.(string) - case []uint8: - change = string(value.([]uint8)) - } - } - if value, ok := requestInfo["app"]; ok { - appName = value.(string) - } - if value, ok := requestInfo["brief"]; ok { - brief = value.(string) - } + change := getStringValue(requestInfo["change"]) + appName := getStringValue(requestInfo["app"]) + brief := getStringValue(requestInfo["brief"]) if strings.HasPrefix(action, "plugin") { brief = "" } @@ -195,7 +177,7 @@ func (e mainEnv) approveUserRequest(w http.ResponseWriter, r *http.Request, ps h returnError(w, r, "failed to decode request body", 405, err, event) return } - reason := getStringValue(records, "reason"); + reason := getStringValue(records["reason"]); requestInfo, err := e.db.getRequest(request) if err != nil { returnError(w, r, "internal error", 405, err, event) @@ -205,17 +187,14 @@ func (e mainEnv) approveUserRequest(w http.ResponseWriter, r *http.Request, ps h returnError(w, r, "not found", 405, err, event) return } - userTOKEN := "" - action := "" - if value, ok := requestInfo["action"]; ok { - action = value.(string) - } - if value, ok := requestInfo["token"]; ok { - userTOKEN = value.(string) + userTOKEN := getStringValue(requestInfo["token"]) + if len(userTOKEN) != 0 { event.Record = userTOKEN } - if requestInfo["status"].(string) != "open" { - returnError(w, r, "wrong status: " + requestInfo["status"].(string), 405, err, event) + action := getStringValue(requestInfo["action"]) + status := getStringValue(requestInfo["status"]) + if status != "open" { + returnError(w, r, "wrong status: " + status, 405, err, event) return } resultJSON, err := e.db.getUser(userTOKEN) @@ -289,7 +268,7 @@ func (e mainEnv) cancelUserRequest(w http.ResponseWriter, r *http.Request, ps ht returnError(w, r, "failed to decode request body", 405, err, event) return } - reason := getStringValue(records, "reason"); + reason := getStringValue(records["reason"]); requestInfo, err := e.db.getRequest(request) if err != nil { returnError(w, r, "internal error", 405, err, event) @@ -299,9 +278,8 @@ func (e mainEnv) cancelUserRequest(w http.ResponseWriter, r *http.Request, ps ht returnError(w, r, "not found", 405, err, event) return } - userTOKEN := "" - if value, ok := requestInfo["token"]; ok { - userTOKEN = value.(string) + userTOKEN := getStringValue(requestInfo["token"]) + if len(userTOKEN) != 0 { event.Record = userTOKEN } authResult := e.enforceAuth(w, r, event) diff --git a/src/utils.go b/src/utils.go index 26d9daf..8a1cdb3 100644 --- a/src/utils.go +++ b/src/utils.go @@ -57,16 +57,16 @@ func getMeta(r *http.Request) string { } */ -func getStringValue(records map[string]interface{}, key string) string { - if value, ok := records[key]; ok { - if reflect.TypeOf(value) == reflect.TypeOf("string") { - return strings.TrimSpace(value.(string)) - } - log.Printf("getStringValue unknown type %s", key) - } else { - log.Printf("getStringValue not found %s", key) +func getStringValue(r interface{}) string { + if r == nil { + return "" + } + switch r.(type) { + case string: + return strings.TrimSpace(r.(string)) + case []uint8: + return strings.TrimSpace(string(r.([]uint8))) } - return "" }