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

View File

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

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

View File

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

View File

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

View File

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

View File

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

View File

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