code optimization

This commit is contained in:
root
2021-02-24 18:40:50 +00:00
parent c4edc869ce
commit 738b052aba
4 changed files with 41 additions and 89 deletions

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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 ""
}