1
0
mirror of https://github.com/go-gitea/gitea synced 2024-12-24 03:35:55 +01:00

refactor: append, build variable and type switch (#4940)

* refactor: append, build variable and type switch

* fix: remove redundant space.
This commit is contained in:
Bo-Yi Wu 2019-05-28 23:45:54 +08:00 committed by GitHub
parent 31557b1274
commit 743697a549
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 42 additions and 46 deletions

@ -110,7 +110,7 @@ func testLinksAsUser(userName string, t *testing.T) {
reqAPI := NewRequestf(t, "GET", "/api/v1/users/%s/repos", userName) reqAPI := NewRequestf(t, "GET", "/api/v1/users/%s/repos", userName)
respAPI := MakeRequest(t, reqAPI, http.StatusOK) respAPI := MakeRequest(t, reqAPI, http.StatusOK)
var apiRepos []api.Repository var apiRepos []*api.Repository
DecodeJSON(t, respAPI, &apiRepos) DecodeJSON(t, respAPI, &apiRepos)
var repoLinks = []string{ var repoLinks = []string{

@ -43,8 +43,7 @@ func TestUpdateAssignee(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
var expectedAssignees []*User var expectedAssignees []*User
expectedAssignees = append(expectedAssignees, user2) expectedAssignees = append(expectedAssignees, user2, user3)
expectedAssignees = append(expectedAssignees, user3)
for in, assignee := range assignees { for in, assignee := range assignees {
assert.Equal(t, assignee.ID, expectedAssignees[in].ID) assert.Equal(t, assignee.ID, expectedAssignees[in].ID)

@ -83,7 +83,7 @@ func addMultipleAssignees(x *xorm.Engine) error {
return err return err
} }
allIssues := []Issue{} allIssues := []*Issue{}
if err := sess.Find(&allIssues); err != nil { if err := sess.Find(&allIssues); err != nil {
return err return err
} }
@ -104,7 +104,7 @@ func addMultipleAssignees(x *xorm.Engine) error {
return err return err
} }
allAssignementComments := []Comment{} allAssignementComments := []*Comment{}
if err := sess.Where("type = ?", 9).Find(&allAssignementComments); err != nil { if err := sess.Where("type = ?", 9).Find(&allAssignementComments); err != nil {
return err return err
} }

@ -13,9 +13,7 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
_ "image/jpeg" // Needed for jpeg support
// Needed for jpeg support
_ "image/jpeg"
"image/png" "image/png"
"os" "os"
"path/filepath" "path/filepath"
@ -1622,7 +1620,7 @@ func SyncExternalUsers() {
var sshKeysNeedUpdate bool var sshKeysNeedUpdate bool
// Find all users with this login type // Find all users with this login type
var users []User var users []*User
x.Where("login_type = ?", LoginLDAP). x.Where("login_type = ?", LoginLDAP).
And("login_source = ?", s.ID). And("login_source = ?", s.ID).
Find(&users) Find(&users)
@ -1641,7 +1639,7 @@ func SyncExternalUsers() {
// Search for existing user // Search for existing user
for _, du := range users { for _, du := range users {
if du.LowerName == strings.ToLower(su.Username) { if du.LowerName == strings.ToLower(su.Username) {
usr = &du usr = du
break break
} }
} }
@ -1724,7 +1722,7 @@ func SyncExternalUsers() {
log.Trace("SyncExternalUsers[%s]: Deactivating user %s", s.Name, usr.Name) log.Trace("SyncExternalUsers[%s]: Deactivating user %s", s.Name, usr.Name)
usr.IsActive = false usr.IsActive = false
err = UpdateUserCols(&usr, "is_active") err = UpdateUserCols(usr, "is_active")
if err != nil { if err != nil {
log.Error("SyncExternalUsers[%s]: Error deactivating user %s: %v", s.Name, usr.Name, err) log.Error("SyncExternalUsers[%s]: Error deactivating user %s: %v", s.Name, usr.Name, err)
} }

@ -465,41 +465,41 @@ func Subtract(left interface{}, right interface{}) interface{} {
var rleft, rright int64 var rleft, rright int64
var fleft, fright float64 var fleft, fright float64
var isInt = true var isInt = true
switch left := left.(type) { switch v := left.(type) {
case int: case int:
rleft = int64(left) rleft = int64(v)
case int8: case int8:
rleft = int64(left) rleft = int64(v)
case int16: case int16:
rleft = int64(left) rleft = int64(v)
case int32: case int32:
rleft = int64(left) rleft = int64(v)
case int64: case int64:
rleft = left rleft = v
case float32: case float32:
fleft = float64(left) fleft = float64(v)
isInt = false isInt = false
case float64: case float64:
fleft = left fleft = v
isInt = false isInt = false
} }
switch right := right.(type) { switch v := right.(type) {
case int: case int:
rright = int64(right) rright = int64(v)
case int8: case int8:
rright = int64(right) rright = int64(v)
case int16: case int16:
rright = int64(right) rright = int64(v)
case int32: case int32:
rright = int64(right) rright = int64(v)
case int64: case int64:
rright = right rright = v
case float32: case float32:
fright = float64(right) fright = float64(v)
isInt = false isInt = false
case float64: case float64:
fright = right fright = v
isInt = false isInt = false
} }

@ -306,21 +306,21 @@ func TestFileSize(t *testing.T) {
func TestSubtract(t *testing.T) { func TestSubtract(t *testing.T) {
toFloat64 := func(n interface{}) float64 { toFloat64 := func(n interface{}) float64 {
switch n := n.(type) { switch v := n.(type) {
case int: case int:
return float64(n) return float64(v)
case int8: case int8:
return float64(n) return float64(v)
case int16: case int16:
return float64(n) return float64(v)
case int32: case int32:
return float64(n) return float64(v)
case int64: case int64:
return float64(n) return float64(v)
case float32: case float32:
return float64(n) return float64(v)
case float64: case float64:
return n return v
default: default:
return 0.0 return 0.0
} }

@ -156,8 +156,7 @@ func NewFuncMap() []template.FuncMap {
var path []string var path []string
index := strings.LastIndex(str, "/") index := strings.LastIndex(str, "/")
if index != -1 && index != len(str) { if index != -1 && index != len(str) {
path = append(path, str[0:index+1]) path = append(path, str[0:index+1], str[index+1:])
path = append(path, str[index+1:])
} else { } else {
path = append(path, str) path = append(path, str)
} }
@ -330,10 +329,10 @@ func ToUTF8(content string) string {
return res return res
} }
// ReplaceLeft replaces all prefixes 'old' in 's' with 'new'. // ReplaceLeft replaces all prefixes 'oldS' in 's' with 'newS'.
func ReplaceLeft(s, old, new string) string { func ReplaceLeft(s, oldS, newS string) string {
oldLen, newLen, i, n := len(old), len(new), 0, 0 oldLen, newLen, i, n := len(oldS), len(newS), 0, 0
for ; i < len(s) && strings.HasPrefix(s[i:], old); n++ { for ; i < len(s) && strings.HasPrefix(s[i:], oldS); n++ {
i += oldLen i += oldLen
} }
@ -348,7 +347,7 @@ func ReplaceLeft(s, old, new string) string {
j := 0 j := 0
for ; j < n*newLen; j += newLen { for ; j < n*newLen; j += newLen {
copy(replacement[j:j+newLen], new) copy(replacement[j:j+newLen], newS)
} }
copy(replacement[j:], s[i:]) copy(replacement[j:], s[i:])

@ -359,11 +359,11 @@ func RegisterOpenIDPost(ctx *context.Context, cpt *captcha.Captcha, form auth.Si
} }
} }
len := setting.MinPasswordLength length := setting.MinPasswordLength
if len < 256 { if length < 256 {
len = 256 length = 256
} }
password, err := generate.GetRandomString(len) password, err := generate.GetRandomString(length)
if err != nil { if err != nil {
ctx.RenderWithErr(err.Error(), tplSignUpOID, form) ctx.RenderWithErr(err.Error(), tplSignUpOID, form)
return return