From 62456dcdaf330c462affded4ca65ed981f4ce242 Mon Sep 17 00:00:00 2001 From: stremovsky Date: Tue, 7 Jan 2020 21:25:22 +0200 Subject: [PATCH] fix golint warnings --- src/audit_db.go | 5 ++++- src/bunker.go | 4 ++++ src/consent_db.go | 11 ++++++++++- src/qldb.go | 22 +++++++++++----------- src/sessions_db.go | 2 +- src/userapps_db.go | 9 +++++++++ src/users_db.go | 12 +++++++----- src/utils.go | 38 +++++++++++++++++++------------------- 8 files changed, 65 insertions(+), 38 deletions(-) diff --git a/src/audit_db.go b/src/audit_db.go index 624e9d7..10d86b9 100644 --- a/src/audit_db.go +++ b/src/audit_db.go @@ -122,7 +122,10 @@ func (dbobj dbcon) getAuditEvents(userTOKEN string, offset int32, limit int32) ( results = append(results, element) } - resultJSON, _ := json.Marshal(records) + resultJSON, err := json.Marshal(records) + if err != nil { + return nil, 0, err + } //fmt.Printf("Found multiple documents (array of pointers): %+v\n", results) return resultJSON, count, nil } diff --git a/src/bunker.go b/src/bunker.go index c30760d..3162525 100644 --- a/src/bunker.go +++ b/src/bunker.go @@ -275,11 +275,13 @@ func (e mainEnv) dbCleanup() { }() } +// CustomResponseWriter is a custom wrapper for ResponseWriter type CustomResponseWriter struct { w http.ResponseWriter Code int } +// NewCustomResponseWriter function returns CustomResponseWriter object func NewCustomResponseWriter(ww http.ResponseWriter) *CustomResponseWriter { return &CustomResponseWriter{ w: ww, @@ -287,6 +289,7 @@ func NewCustomResponseWriter(ww http.ResponseWriter) *CustomResponseWriter { } } +// Header returns HTTP Header object func (w *CustomResponseWriter) Header() http.Header { return w.w.Header() } @@ -295,6 +298,7 @@ func (w *CustomResponseWriter) Write(b []byte) (int, error) { return w.w.Write(b) } +// WriteHeader function() writes header back to original ResponseWriter func (w *CustomResponseWriter) WriteHeader(statusCode int) { w.Code = statusCode w.w.WriteHeader(statusCode) diff --git a/src/consent_db.go b/src/consent_db.go index 50f625a..9773d9d 100644 --- a/src/consent_db.go +++ b/src/consent_db.go @@ -108,7 +108,7 @@ func (dbobj dbcon) createConsentRecord(userTOKEN string, mode string, usercode s fmt.Printf("error to insert record: %s\n", err) return false, err } - return true, err + return true, nil } // link consent record to userToken @@ -151,6 +151,9 @@ func (dbobj dbcon) listConsentRecords(userTOKEN string) ([]byte, int, error) { } count := len(records) resultJSON, err := json.Marshal(records) + if err != nil { + return nil, 0, err + } //fmt.Printf("Found multiple documents (array of pointers): %+v\n", results) return resultJSON, count, nil } @@ -161,6 +164,9 @@ func (dbobj dbcon) viewConsentRecord(userTOKEN string, brief string) ([]byte, er return nil, err } resultJSON, err := json.Marshal(record) + if err != nil { + return nil, err + } //fmt.Printf("Found multiple documents (array of pointers): %+v\n", results) return resultJSON, nil } @@ -181,6 +187,9 @@ func (dbobj dbcon) filterConsentRecords(brief string, offset int32, limit int32) result = append(result, rec["token"].(string)) } resultJSON, err := json.Marshal(result) + if err != nil { + return nil, 0, err + } //fmt.Printf("Found multiple documents (array of pointers): %+v\n", results) return resultJSON, count, nil } diff --git a/src/qldb.go b/src/qldb.go index 3cf9974..e56aee9 100644 --- a/src/qldb.go +++ b/src/qldb.go @@ -202,7 +202,7 @@ func decodeForCleanup(data interface{}) string { switch t := data.(type) { case primitive.M: - for idx, _ := range data.(primitive.M) { + for idx := range data.(primitive.M) { if len(fields) == 0 { fields = escapeName(idx) + "=null" } else { @@ -211,7 +211,7 @@ func decodeForCleanup(data interface{}) string { } return fields case map[string]interface{}: - for idx, _ := range data.(map[string]interface{}) { + for idx := range data.(map[string]interface{}) { if len(fields) == 0 { fields = escapeName(idx) + "=null" } else { @@ -248,7 +248,7 @@ func decodeForUpdate(bdoc *bson.M, bdel *bson.M) (string, []interface{}) { } if bdel != nil { - for idx, _ := range *bdel { + for idx := range *bdel { if len(fields) == 0 { fields = escapeName(idx) + "=null" } else { @@ -279,13 +279,13 @@ func getTable(t Tbl) string { func (dbobj dbcon) createRecordInTable(tbl string, data interface{}) (int, error) { fields, values := decodeFieldsValues(data) - values_q := "$1" - for idx, _ := range values { + valuesInQ := "$1" + for idx := range values { if idx > 0 { - values_q = values_q + ",$" + (strconv.Itoa(idx + 1)) + valuesInQ = valuesInQ + ",$" + (strconv.Itoa(idx + 1)) } } - q := "insert into " + tbl + " (" + fields + ") values (" + values_q + ")" + q := "insert into " + tbl + " (" + fields + ") values (" + valuesInQ + ")" //fmt.Printf("values: %s\n", values...) tx, err := dbobj.db.Begin() if err != nil { @@ -470,8 +470,8 @@ func (dbobj dbcon) getRecordInTableDo(q string, values []interface{}) (bson.M, e // columnPointers[i] = new(interface{}) //} columns := make([]interface{}, len(columnNames)) - for i, _ := range columns { - columnPointers[i] = &columns[i] + for idx := range columns { + columnPointers[idx] = &columns[idx] } err = rows.Scan(columnPointers...) if err == sql.ErrNoRows { @@ -718,8 +718,8 @@ func (dbobj dbcon) getListDo(q string, keyValue string) ([]bson.M, error) { // columnPointers[i] = new(interface{}) //} columns := make([]interface{}, len(columnNames)) - for i, _ := range columns { - columnPointers[i] = &columns[i] + for idx := range columns { + columnPointers[idx] = &columns[idx] } err = rows.Scan(columnPointers...) diff --git a/src/sessions_db.go b/src/sessions_db.go index a021d2f..91e6821 100644 --- a/src/sessions_db.go +++ b/src/sessions_db.go @@ -16,7 +16,7 @@ type sessionEvent struct { } func (dbobj dbcon) createSessionRecord(userTOKEN string, expiration string, data []byte) (string, error) { - var endtime int32 = 0 + var endtime int32 var err error if len(expiration) > 0 { endtime, err = parseExpiration(expiration) diff --git a/src/userapps_db.go b/src/userapps_db.go index befe735..a61a15f 100644 --- a/src/userapps_db.go +++ b/src/userapps_db.go @@ -47,6 +47,9 @@ func (dbobj dbcon) createAppRecord(jsonData []byte, userTOKEN string, appName st } //fmt.Println("creating new app") record, err := dbobj.getRecordInTable("app_"+appName, "token", userTOKEN) + if err != nil { + return userTOKEN, err + } if record != nil { fmt.Println("update user app") _, err = dbobj.updateRecordInTable("app_"+appName, "token", userTOKEN, &bdoc) @@ -92,9 +95,15 @@ func (dbobj dbcon) updateAppRecord(jsonDataPatch []byte, userTOKEN string, appNa fmt.Printf("old json: %s\n", decrypted) fmt.Printf("json patch: %s\n", jsonDataPatch) newJSON, err := jsonpatch.MergePatch(decrypted, jsonDataPatch) + if err != nil { + return userTOKEN, err + } fmt.Printf("result: %s\n", newJSON) bdoc := bson.M{} encoded, err := encrypt(dbobj.masterKey, recordKey, newJSON) + if err != nil { + return userTOKEN, err + } encodedStr := base64.StdEncoding.EncodeToString(encoded) bdoc["data"] = encodedStr //it is ok to use md5 here, it is only for data sanity diff --git a/src/users_db.go b/src/users_db.go index f124ce3..b47285a 100644 --- a/src/users_db.go +++ b/src/users_db.go @@ -110,11 +110,10 @@ func (dbobj dbcon) validateIndexChange(indexName string, idxOldValue string, raw } //fmt.Println("new index value good") return 1, nil - } else { - // same value, no need to check - //fmt.Println("same index value") - return 0, nil } + // same value, no need to check + //fmt.Println("same index value") + return 0, nil } else if reflect.TypeOf(newIdxValue) == reflect.TypeOf(nil) { //fmt.Println("old index removed!!!") return -1, nil @@ -159,6 +158,9 @@ func (dbobj dbcon) updateUserRecordDo(parsedData userJSON, userTOKEN string, eve } encData0 := oldUserBson["data"].(string) encData, err := base64.StdEncoding.DecodeString(encData0) + if err != nil { + return nil, nil, err + } decrypted, err := decrypt(dbobj.masterKey, recordKey, encData) if err != nil { return nil, nil, err @@ -201,7 +203,7 @@ func (dbobj dbcon) updateUserRecordDo(parsedData userJSON, userTOKEN string, eve otherUserBson, _ := dbobj.lookupUserRecordByIndex(idx, newIdxValue.(string), conf) if otherUserBson != nil { // already exist user with same index value - return nil, nil, errors.New(fmt.Sprintf("duplicate %s index", idx)) + return nil, nil, fmt.Errorf("duplicate %s index", idx) } //fmt.Printf("adding index2? %s\n", raw[idx]) // create login index diff --git a/src/utils.go b/src/utils.go index ccd27f0..1fc0be5 100644 --- a/src/utils.go +++ b/src/utils.go @@ -82,17 +82,17 @@ func normalizeEmail(email0 string) string { return email } -func normalizePhone(phone string, default_country string) string { +func normalizePhone(phone string, defaultCountry string) string { // 4444 is a phone number for testing, no need to normilize it phone = strings.TrimSpace(phone) if phone == "4444" { return "4444" } - if len(default_country) == 0 { + if len(defaultCountry) == 0 { // https://github.com/ttacon/libphonenumber/blob/master/countrycodetoregionmap.go - default_country = "GB" + defaultCountry = "GB" } - res, err := libphonenumber.Parse(phone, default_country) + res, err := libphonenumber.Parse(phone, defaultCountry) if err != nil { fmt.Printf("failed to parse phone number: %s", phone) return "" @@ -155,23 +155,23 @@ func atoi(s string) int32 { return int32(n) } -func setExpiration(max_expiration string, user_expiration string) string { - if len(user_expiration) == 0 { - return max_expiration +func setExpiration(maxExpiration string, userExpiration string) string { + if len(userExpiration) == 0 { + return maxExpiration } - user_expiration_num, _ := parseExpiration(user_expiration) - max_expiration_num, _ := parseExpiration(max_expiration) - if max_expiration_num == 0 { - max_expiration = "6m" - max_expiration_num, _ = parseExpiration(max_expiration) + userExpirationNum, _ := parseExpiration(userExpiration) + maxExpirationNum, _ := parseExpiration(maxExpiration) + if maxExpirationNum == 0 { + maxExpiration = "6m" + maxExpirationNum, _ = parseExpiration(maxExpiration) } - if user_expiration_num == 0 { - return max_expiration + if userExpirationNum == 0 { + return maxExpiration } - if user_expiration_num > max_expiration_num { - return max_expiration + if userExpirationNum > maxExpirationNum { + return maxExpiration } - return user_expiration + return userExpiration } func parseExpiration0(expiration string) (int32, error) { @@ -370,7 +370,7 @@ func getJSONPostData(r *http.Request) (map[string]interface{}, error) { return records, nil } -func getJSONPost(r *http.Request, default_country string) (userJSON, error) { +func getJSONPost(r *http.Request, defaultCountry string) (userJSON, error) { var result userJSON records, err := getJSONPostData(r) if err != nil { @@ -395,7 +395,7 @@ func getJSONPost(r *http.Request, default_country string) (userJSON, error) { if value, ok := records["phone"]; ok { if reflect.TypeOf(value) == reflect.TypeOf("string") { result.phoneIdx = value.(string) - result.phoneIdx = normalizePhone(result.phoneIdx, default_country) + result.phoneIdx = normalizePhone(result.phoneIdx, defaultCountry) } }