fix golint warnings

This commit is contained in:
stremovsky
2020-01-07 21:25:22 +02:00
parent b7c40ca03c
commit 62456dcdaf
8 changed files with 65 additions and 38 deletions

View File

@@ -122,7 +122,10 @@ func (dbobj dbcon) getAuditEvents(userTOKEN string, offset int32, limit int32) (
results = append(results, element) 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) //fmt.Printf("Found multiple documents (array of pointers): %+v\n", results)
return resultJSON, count, nil return resultJSON, count, nil
} }

View File

@@ -275,11 +275,13 @@ func (e mainEnv) dbCleanup() {
}() }()
} }
// CustomResponseWriter is a custom wrapper for ResponseWriter
type CustomResponseWriter struct { type CustomResponseWriter struct {
w http.ResponseWriter w http.ResponseWriter
Code int Code int
} }
// NewCustomResponseWriter function returns CustomResponseWriter object
func NewCustomResponseWriter(ww http.ResponseWriter) *CustomResponseWriter { func NewCustomResponseWriter(ww http.ResponseWriter) *CustomResponseWriter {
return &CustomResponseWriter{ return &CustomResponseWriter{
w: ww, w: ww,
@@ -287,6 +289,7 @@ func NewCustomResponseWriter(ww http.ResponseWriter) *CustomResponseWriter {
} }
} }
// Header returns HTTP Header object
func (w *CustomResponseWriter) Header() http.Header { func (w *CustomResponseWriter) Header() http.Header {
return w.w.Header() return w.w.Header()
} }
@@ -295,6 +298,7 @@ func (w *CustomResponseWriter) Write(b []byte) (int, error) {
return w.w.Write(b) return w.w.Write(b)
} }
// WriteHeader function() writes header back to original ResponseWriter
func (w *CustomResponseWriter) WriteHeader(statusCode int) { func (w *CustomResponseWriter) WriteHeader(statusCode int) {
w.Code = statusCode w.Code = statusCode
w.w.WriteHeader(statusCode) w.w.WriteHeader(statusCode)

View File

@@ -108,7 +108,7 @@ func (dbobj dbcon) createConsentRecord(userTOKEN string, mode string, usercode s
fmt.Printf("error to insert record: %s\n", err) fmt.Printf("error to insert record: %s\n", err)
return false, err return false, err
} }
return true, err return true, nil
} }
// link consent record to userToken // link consent record to userToken
@@ -151,6 +151,9 @@ func (dbobj dbcon) listConsentRecords(userTOKEN string) ([]byte, int, error) {
} }
count := len(records) count := len(records)
resultJSON, err := 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) //fmt.Printf("Found multiple documents (array of pointers): %+v\n", results)
return resultJSON, count, nil return resultJSON, count, nil
} }
@@ -161,6 +164,9 @@ func (dbobj dbcon) viewConsentRecord(userTOKEN string, brief string) ([]byte, er
return nil, err return nil, err
} }
resultJSON, err := json.Marshal(record) resultJSON, err := json.Marshal(record)
if err != nil {
return nil, err
}
//fmt.Printf("Found multiple documents (array of pointers): %+v\n", results) //fmt.Printf("Found multiple documents (array of pointers): %+v\n", results)
return resultJSON, nil return resultJSON, nil
} }
@@ -181,6 +187,9 @@ func (dbobj dbcon) filterConsentRecords(brief string, offset int32, limit int32)
result = append(result, rec["token"].(string)) result = append(result, rec["token"].(string))
} }
resultJSON, err := json.Marshal(result) resultJSON, err := json.Marshal(result)
if err != nil {
return nil, 0, err
}
//fmt.Printf("Found multiple documents (array of pointers): %+v\n", results) //fmt.Printf("Found multiple documents (array of pointers): %+v\n", results)
return resultJSON, count, nil return resultJSON, count, nil
} }

View File

@@ -202,7 +202,7 @@ func decodeForCleanup(data interface{}) string {
switch t := data.(type) { switch t := data.(type) {
case primitive.M: case primitive.M:
for idx, _ := range data.(primitive.M) { for idx := range data.(primitive.M) {
if len(fields) == 0 { if len(fields) == 0 {
fields = escapeName(idx) + "=null" fields = escapeName(idx) + "=null"
} else { } else {
@@ -211,7 +211,7 @@ func decodeForCleanup(data interface{}) string {
} }
return fields return fields
case map[string]interface{}: case map[string]interface{}:
for idx, _ := range data.(map[string]interface{}) { for idx := range data.(map[string]interface{}) {
if len(fields) == 0 { if len(fields) == 0 {
fields = escapeName(idx) + "=null" fields = escapeName(idx) + "=null"
} else { } else {
@@ -248,7 +248,7 @@ func decodeForUpdate(bdoc *bson.M, bdel *bson.M) (string, []interface{}) {
} }
if bdel != nil { if bdel != nil {
for idx, _ := range *bdel { for idx := range *bdel {
if len(fields) == 0 { if len(fields) == 0 {
fields = escapeName(idx) + "=null" fields = escapeName(idx) + "=null"
} else { } else {
@@ -279,13 +279,13 @@ func getTable(t Tbl) string {
func (dbobj dbcon) createRecordInTable(tbl string, data interface{}) (int, error) { func (dbobj dbcon) createRecordInTable(tbl string, data interface{}) (int, error) {
fields, values := decodeFieldsValues(data) fields, values := decodeFieldsValues(data)
values_q := "$1" valuesInQ := "$1"
for idx, _ := range values { for idx := range values {
if idx > 0 { 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...) //fmt.Printf("values: %s\n", values...)
tx, err := dbobj.db.Begin() tx, err := dbobj.db.Begin()
if err != nil { if err != nil {
@@ -470,8 +470,8 @@ func (dbobj dbcon) getRecordInTableDo(q string, values []interface{}) (bson.M, e
// columnPointers[i] = new(interface{}) // columnPointers[i] = new(interface{})
//} //}
columns := make([]interface{}, len(columnNames)) columns := make([]interface{}, len(columnNames))
for i, _ := range columns { for idx := range columns {
columnPointers[i] = &columns[i] columnPointers[idx] = &columns[idx]
} }
err = rows.Scan(columnPointers...) err = rows.Scan(columnPointers...)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
@@ -718,8 +718,8 @@ func (dbobj dbcon) getListDo(q string, keyValue string) ([]bson.M, error) {
// columnPointers[i] = new(interface{}) // columnPointers[i] = new(interface{})
//} //}
columns := make([]interface{}, len(columnNames)) columns := make([]interface{}, len(columnNames))
for i, _ := range columns { for idx := range columns {
columnPointers[i] = &columns[i] columnPointers[idx] = &columns[idx]
} }
err = rows.Scan(columnPointers...) err = rows.Scan(columnPointers...)

View File

@@ -16,7 +16,7 @@ type sessionEvent struct {
} }
func (dbobj dbcon) createSessionRecord(userTOKEN string, expiration string, data []byte) (string, error) { func (dbobj dbcon) createSessionRecord(userTOKEN string, expiration string, data []byte) (string, error) {
var endtime int32 = 0 var endtime int32
var err error var err error
if len(expiration) > 0 { if len(expiration) > 0 {
endtime, err = parseExpiration(expiration) endtime, err = parseExpiration(expiration)

View File

@@ -47,6 +47,9 @@ func (dbobj dbcon) createAppRecord(jsonData []byte, userTOKEN string, appName st
} }
//fmt.Println("creating new app") //fmt.Println("creating new app")
record, err := dbobj.getRecordInTable("app_"+appName, "token", userTOKEN) record, err := dbobj.getRecordInTable("app_"+appName, "token", userTOKEN)
if err != nil {
return userTOKEN, err
}
if record != nil { if record != nil {
fmt.Println("update user app") fmt.Println("update user app")
_, err = dbobj.updateRecordInTable("app_"+appName, "token", userTOKEN, &bdoc) _, 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("old json: %s\n", decrypted)
fmt.Printf("json patch: %s\n", jsonDataPatch) fmt.Printf("json patch: %s\n", jsonDataPatch)
newJSON, err := jsonpatch.MergePatch(decrypted, jsonDataPatch) newJSON, err := jsonpatch.MergePatch(decrypted, jsonDataPatch)
if err != nil {
return userTOKEN, err
}
fmt.Printf("result: %s\n", newJSON) fmt.Printf("result: %s\n", newJSON)
bdoc := bson.M{} bdoc := bson.M{}
encoded, err := encrypt(dbobj.masterKey, recordKey, newJSON) encoded, err := encrypt(dbobj.masterKey, recordKey, newJSON)
if err != nil {
return userTOKEN, err
}
encodedStr := base64.StdEncoding.EncodeToString(encoded) encodedStr := base64.StdEncoding.EncodeToString(encoded)
bdoc["data"] = encodedStr bdoc["data"] = encodedStr
//it is ok to use md5 here, it is only for data sanity //it is ok to use md5 here, it is only for data sanity

View File

@@ -110,11 +110,10 @@ func (dbobj dbcon) validateIndexChange(indexName string, idxOldValue string, raw
} }
//fmt.Println("new index value good") //fmt.Println("new index value good")
return 1, nil return 1, nil
} else { }
// same value, no need to check // same value, no need to check
//fmt.Println("same index value") //fmt.Println("same index value")
return 0, nil return 0, nil
}
} else if reflect.TypeOf(newIdxValue) == reflect.TypeOf(nil) { } else if reflect.TypeOf(newIdxValue) == reflect.TypeOf(nil) {
//fmt.Println("old index removed!!!") //fmt.Println("old index removed!!!")
return -1, nil return -1, nil
@@ -159,6 +158,9 @@ func (dbobj dbcon) updateUserRecordDo(parsedData userJSON, userTOKEN string, eve
} }
encData0 := oldUserBson["data"].(string) encData0 := oldUserBson["data"].(string)
encData, err := base64.StdEncoding.DecodeString(encData0) encData, err := base64.StdEncoding.DecodeString(encData0)
if err != nil {
return nil, nil, err
}
decrypted, err := decrypt(dbobj.masterKey, recordKey, encData) decrypted, err := decrypt(dbobj.masterKey, recordKey, encData)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@@ -201,7 +203,7 @@ func (dbobj dbcon) updateUserRecordDo(parsedData userJSON, userTOKEN string, eve
otherUserBson, _ := dbobj.lookupUserRecordByIndex(idx, newIdxValue.(string), conf) otherUserBson, _ := dbobj.lookupUserRecordByIndex(idx, newIdxValue.(string), conf)
if otherUserBson != nil { if otherUserBson != nil {
// already exist user with same index value // 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]) //fmt.Printf("adding index2? %s\n", raw[idx])
// create login index // create login index

View File

@@ -82,17 +82,17 @@ func normalizeEmail(email0 string) string {
return email 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 // 4444 is a phone number for testing, no need to normilize it
phone = strings.TrimSpace(phone) phone = strings.TrimSpace(phone)
if phone == "4444" { if phone == "4444" {
return "4444" return "4444"
} }
if len(default_country) == 0 { if len(defaultCountry) == 0 {
// https://github.com/ttacon/libphonenumber/blob/master/countrycodetoregionmap.go // 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 { if err != nil {
fmt.Printf("failed to parse phone number: %s", phone) fmt.Printf("failed to parse phone number: %s", phone)
return "" return ""
@@ -155,23 +155,23 @@ func atoi(s string) int32 {
return int32(n) return int32(n)
} }
func setExpiration(max_expiration string, user_expiration string) string { func setExpiration(maxExpiration string, userExpiration string) string {
if len(user_expiration) == 0 { if len(userExpiration) == 0 {
return max_expiration return maxExpiration
} }
user_expiration_num, _ := parseExpiration(user_expiration) userExpirationNum, _ := parseExpiration(userExpiration)
max_expiration_num, _ := parseExpiration(max_expiration) maxExpirationNum, _ := parseExpiration(maxExpiration)
if max_expiration_num == 0 { if maxExpirationNum == 0 {
max_expiration = "6m" maxExpiration = "6m"
max_expiration_num, _ = parseExpiration(max_expiration) maxExpirationNum, _ = parseExpiration(maxExpiration)
} }
if user_expiration_num == 0 { if userExpirationNum == 0 {
return max_expiration return maxExpiration
} }
if user_expiration_num > max_expiration_num { if userExpirationNum > maxExpirationNum {
return max_expiration return maxExpiration
} }
return user_expiration return userExpiration
} }
func parseExpiration0(expiration string) (int32, error) { func parseExpiration0(expiration string) (int32, error) {
@@ -370,7 +370,7 @@ func getJSONPostData(r *http.Request) (map[string]interface{}, error) {
return records, nil 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 var result userJSON
records, err := getJSONPostData(r) records, err := getJSONPostData(r)
if err != nil { if err != nil {
@@ -395,7 +395,7 @@ func getJSONPost(r *http.Request, default_country string) (userJSON, error) {
if value, ok := records["phone"]; ok { if value, ok := records["phone"]; ok {
if reflect.TypeOf(value) == reflect.TypeOf("string") { if reflect.TypeOf(value) == reflect.TypeOf("string") {
result.phoneIdx = value.(string) result.phoneIdx = value.(string)
result.phoneIdx = normalizePhone(result.phoneIdx, default_country) result.phoneIdx = normalizePhone(result.phoneIdx, defaultCountry)
} }
} }