Browse Source

Fix lint errors (#1631)

Fix lint errors

Reviewed-on: #1631
pull/1633/head
Lunny Xiao 3 days ago
parent
commit
fb8eb29d1e
9 changed files with 52 additions and 48 deletions
  1. +3
    -4
      convert.go
  2. +3
    -0
      engine.go
  3. +1
    -0
      engine_group.go
  4. +2
    -0
      engine_group_policy.go
  5. +10
    -10
      processors_test.go
  6. +15
    -15
      session_insert_test.go
  7. +2
    -2
      tags_test.go
  8. +11
    -11
      time_test.go
  9. +5
    -6
      types_test.go

+ 3
- 4
convert.go View File

@@ -25,11 +25,10 @@ func strconvErr(err error) error {
func cloneBytes(b []byte) []byte {
if b == nil {
return nil
} else {
c := make([]byte, len(b))
copy(c, b)
return c
}
c := make([]byte, len(b))
copy(c, b)
return c
}

func asString(src interface{}) string {


+ 3
- 0
engine.go View File

@@ -44,14 +44,17 @@ type Engine struct {
DatabaseTZ *time.Location // The timezone of the database
}

// SetCacher sets cacher for the table
func (engine *Engine) SetCacher(tableName string, cacher caches.Cacher) {
engine.cacherMgr.SetCacher(tableName, cacher)
}

// GetCacher returns the cachher of the special table
func (engine *Engine) GetCacher(tableName string) caches.Cacher {
return engine.cacherMgr.GetCacher(tableName)
}

// SetQuotePolicy sets the special quote policy
func (engine *Engine) SetQuotePolicy(quotePolicy dialects.QuotePolicy) {
engine.dialect.SetQuotePolicy(quotePolicy)
}


+ 1
- 0
engine_group.go View File

@@ -181,6 +181,7 @@ func (eg *EngineGroup) SetPolicy(policy GroupPolicy) *EngineGroup {
return eg
}

// SetQuotePolicy sets the special quote policy
func (eg *EngineGroup) SetQuotePolicy(quotePolicy dialects.QuotePolicy) {
eg.Engine.SetQuotePolicy(quotePolicy)
for i := 0; i < len(eg.slaves); i++ {


+ 2
- 0
engine_group_policy.go View File

@@ -51,6 +51,7 @@ func WeightRandomPolicy(weights []int) GroupPolicyHandler {
}
}

// RoundRobinPolicy returns a group policy handler
func RoundRobinPolicy() GroupPolicyHandler {
var pos = -1
var lock sync.Mutex
@@ -68,6 +69,7 @@ func RoundRobinPolicy() GroupPolicyHandler {
}
}

// WeightRoundRobinPolicy returns a group policy handler
func WeightRoundRobinPolicy(weights []int) GroupPolicyHandler {
var rands = make([]int, 0, len(weights))
for i := 0; i < len(weights); i++ {


+ 10
- 10
processors_test.go View File

@@ -485,7 +485,7 @@ func TestProcessorsTx(t *testing.T) {
t.Error(errors.New("AfterInsertedViaExt is set"))
}

insertedId := p2.Id
insertedID := p2.Id
// --

// test update processors with tx rollback
@@ -513,7 +513,7 @@ func TestProcessorsTx(t *testing.T) {

p = p2 // reset

_, err = session.ID(insertedId).Before(b4UpdateFunc).After(afterUpdateFunc).Update(p)
_, err = session.ID(insertedID).Before(b4UpdateFunc).After(afterUpdateFunc).Update(p)
assert.NoError(t, err)

if p.B4UpdateFlag == 0 {
@@ -548,7 +548,7 @@ func TestProcessorsTx(t *testing.T) {
session.Close()

p2 = &ProcessorsStruct{}
_, err = testEngine.ID(insertedId).Get(p2)
_, err = testEngine.ID(insertedID).Get(p2)
assert.NoError(t, err)

if p2.B4UpdateFlag != 0 {
@@ -572,7 +572,7 @@ func TestProcessorsTx(t *testing.T) {
err = session.Begin()
assert.NoError(t, err)

p = &ProcessorsStruct{Id: insertedId}
p = &ProcessorsStruct{Id: insertedID}

_, err = session.Update(p)
assert.NoError(t, err)
@@ -611,7 +611,7 @@ func TestProcessorsTx(t *testing.T) {

p = &ProcessorsStruct{}

_, err = session.ID(insertedId).Before(b4UpdateFunc).After(afterUpdateFunc).Update(p)
_, err = session.ID(insertedID).Before(b4UpdateFunc).After(afterUpdateFunc).Update(p)
assert.NoError(t, err)

if p.B4UpdateFlag == 0 {
@@ -645,7 +645,7 @@ func TestProcessorsTx(t *testing.T) {

session.Close()
p2 = &ProcessorsStruct{}
_, err = testEngine.ID(insertedId).Get(p2)
_, err = testEngine.ID(insertedID).Get(p2)
assert.NoError(t, err)

if p.B4UpdateFlag == 0 {
@@ -687,7 +687,7 @@ func TestProcessorsTx(t *testing.T) {

p = &ProcessorsStruct{} // reset

_, err = session.ID(insertedId).Before(b4DeleteFunc).After(afterDeleteFunc).Delete(p)
_, err = session.ID(insertedID).Before(b4DeleteFunc).After(afterDeleteFunc).Delete(p)
assert.NoError(t, err)

if p.B4DeleteFlag == 0 {
@@ -721,7 +721,7 @@ func TestProcessorsTx(t *testing.T) {
session.Close()

p2 = &ProcessorsStruct{}
_, err = testEngine.ID(insertedId).Get(p2)
_, err = testEngine.ID(insertedID).Get(p2)
assert.NoError(t, err)

if p2.B4DeleteFlag != 0 {
@@ -747,7 +747,7 @@ func TestProcessorsTx(t *testing.T) {

p = &ProcessorsStruct{}

_, err = session.ID(insertedId).Before(b4DeleteFunc).After(afterDeleteFunc).Delete(p)
_, err = session.ID(insertedID).Before(b4DeleteFunc).After(afterDeleteFunc).Delete(p)
assert.NoError(t, err)

if p.B4DeleteFlag == 0 {
@@ -788,7 +788,7 @@ func TestProcessorsTx(t *testing.T) {
err = session.Begin()
assert.NoError(t, err)

p = &ProcessorsStruct{Id: insertedId}
p = &ProcessorsStruct{Id: insertedID}
_, err = session.Delete(p)
assert.NoError(t, err)



+ 15
- 15
session_insert_test.go View File

@@ -336,9 +336,9 @@ func TestInsertCreated(t *testing.T) {
assert.EqualValues(t, ci6.Created.Unix(), di6.Created.Unix())
}

type JsonTime time.Time
type JSONTime time.Time

func (j JsonTime) format() string {
func (j JSONTime) format() string {
t := time.Time(j)
if t.IsZero() {
return ""
@@ -347,11 +347,11 @@ func (j JsonTime) format() string {
return t.Format("2006-01-02")
}

func (j JsonTime) MarshalText() ([]byte, error) {
func (j JSONTime) MarshalText() ([]byte, error) {
return []byte(j.format()), nil
}

func (j JsonTime) MarshalJSON() ([]byte, error) {
func (j JSONTime) MarshalJSON() ([]byte, error) {
return []byte(`"` + j.format() + `"`), nil
}

@@ -359,40 +359,40 @@ func TestDefaultTime3(t *testing.T) {
type PrepareTask struct {
Id int `xorm:"not null pk autoincr INT(11)" json:"id"`
// ...
StartTime JsonTime `xorm:"not null default '2006-01-02 15:04:05' TIMESTAMP index" json:"start_time"`
EndTime JsonTime `xorm:"not null default '2006-01-02 15:04:05' TIMESTAMP" json:"end_time"`
StartTime JSONTime `xorm:"not null default '2006-01-02 15:04:05' TIMESTAMP index" json:"start_time"`
EndTime JSONTime `xorm:"not null default '2006-01-02 15:04:05' TIMESTAMP" json:"end_time"`
Cuser string `xorm:"not null default '' VARCHAR(64) index" json:"cuser"`
Muser string `xorm:"not null default '' VARCHAR(64)" json:"muser"`
Ctime JsonTime `xorm:"not null default CURRENT_TIMESTAMP TIMESTAMP created" json:"ctime"`
Mtime JsonTime `xorm:"not null default CURRENT_TIMESTAMP TIMESTAMP updated" json:"mtime"`
Ctime JSONTime `xorm:"not null default CURRENT_TIMESTAMP TIMESTAMP created" json:"ctime"`
Mtime JSONTime `xorm:"not null default CURRENT_TIMESTAMP TIMESTAMP updated" json:"mtime"`
}

assert.NoError(t, prepareEngine())
assertSync(t, new(PrepareTask))

prepareTask := &PrepareTask{
StartTime: JsonTime(time.Now()),
StartTime: JSONTime(time.Now()),
Cuser: "userId",
Muser: "userId",
}
cnt, err := testEngine.Omit("end_time").InsertOne(prepareTask)
cnt, err := testEngine.Omit("end_time").Insert(prepareTask)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
}

type MyJsonTime struct {
type MyJSONTime struct {
Id int64 `json:"id"`
Created JsonTime `xorm:"created" json:"created_at"`
Created JSONTime `xorm:"created" json:"created_at"`
}

func TestCreatedJsonTime(t *testing.T) {
assert.NoError(t, prepareEngine())

di5 := new(MyJsonTime)
di5 := new(MyJSONTime)
err := testEngine.Sync2(di5)
assert.NoError(t, err)

ci5 := &MyJsonTime{}
ci5 := &MyJSONTime{}
_, err = testEngine.Insert(ci5)
assert.NoError(t, err)

@@ -401,7 +401,7 @@ func TestCreatedJsonTime(t *testing.T) {
assert.True(t, has)
assert.EqualValues(t, time.Time(ci5.Created).Unix(), time.Time(di5.Created).Unix())

var dis = make([]MyJsonTime, 0)
var dis = make([]MyJSONTime, 0)
err = testEngine.Find(&dis)
assert.NoError(t, err)
}


+ 2
- 2
tags_test.go View File

@@ -686,7 +686,7 @@ func TestCreatedUpdated(t *testing.T) {

assert.True(t, has)

c2.Value -= 1
c2.Value--
_, err = testEngine.ID(c2.Id).Update(c2)
assert.NoError(t, err)
}
@@ -714,7 +714,7 @@ func TestCreatedUpdatedInt64(t *testing.T) {
assert.NoError(t, err)
assert.True(t, has)

c2.Value -= 1
c2.Value--
_, err = testEngine.ID(c2.Id).Update(c2)
assert.NoError(t, err)
}


+ 11
- 11
time_test.go View File

@@ -360,27 +360,27 @@ func TestTimeUserDeletedDiffLoc(t *testing.T) {
fmt.Println("user3", user3.DeletedAt, user4.DeletedAt)
}

type JsonDate time.Time
type JSONDate time.Time

func (j JsonDate) MarshalJSON() ([]byte, error) {
func (j JSONDate) MarshalJSON() ([]byte, error) {
if time.Time(j).IsZero() {
return []byte(`""`), nil
}
return []byte(`"` + time.Time(j).Format("2006-01-02 15:04:05") + `"`), nil
}

func (j *JsonDate) UnmarshalJSON(value []byte) error {
func (j *JSONDate) UnmarshalJSON(value []byte) error {
var v = strings.TrimSpace(strings.Trim(string(value), "\""))

t, err := time.ParseInLocation("2006-01-02 15:04:05", v, time.Local)
if err != nil {
return err
}
*j = JsonDate(t)
*j = JSONDate(t)
return nil
}

func (j *JsonDate) Unix() int64 {
func (j *JSONDate) Unix() int64 {
return (*time.Time)(j).Unix()
}

@@ -389,9 +389,9 @@ func TestCustomTimeUserDeleted(t *testing.T) {

type UserDeleted3 struct {
Id string
CreatedAt JsonDate `xorm:"created"`
UpdatedAt JsonDate `xorm:"updated"`
DeletedAt JsonDate `xorm:"deleted"`
CreatedAt JSONDate `xorm:"created"`
UpdatedAt JSONDate `xorm:"updated"`
DeletedAt JSONDate `xorm:"deleted"`
}

assertSync(t, new(UserDeleted3))
@@ -442,9 +442,9 @@ func TestCustomTimeUserDeletedDiffLoc(t *testing.T) {

type UserDeleted4 struct {
Id string
CreatedAt JsonDate `xorm:"created"`
UpdatedAt JsonDate `xorm:"updated"`
DeletedAt JsonDate `xorm:"deleted"`
CreatedAt JSONDate `xorm:"created"`
UpdatedAt JSONDate `xorm:"updated"`
DeletedAt JSONDate `xorm:"deleted"`
}

assertSync(t, new(UserDeleted4))


+ 5
- 6
types_test.go View File

@@ -302,10 +302,10 @@ type Status struct {

var (
_ convert.Conversion = &Status{}
Registered Status = Status{"Registered", "white"}
Approved Status = Status{"Approved", "green"}
Removed Status = Status{"Removed", "red"}
Statuses map[string]Status = map[string]Status{
Registered = Status{"Registered", "white"}
Approved = Status{"Approved", "green"}
Removed = Status{"Removed", "red"}
Statuses = map[string]Status{
Registered.Name: Registered,
Approved.Name: Approved,
Removed.Name: Removed,
@@ -316,9 +316,8 @@ func (s *Status) FromDB(bytes []byte) error {
if r, ok := Statuses[string(bytes)]; ok {
*s = r
return nil
} else {
return errors.New("no this data")
}
return errors.New("no this data")
}

func (s *Status) ToDB() ([]byte, error) {


Loading…
Cancel
Save