From 2fdda76882a17bd27c6f36b4b6a445ec46197af7 Mon Sep 17 00:00:00 2001 From: letu <282130106@qq.com> Date: Thu, 13 May 2021 16:16:02 +0800 Subject: [PATCH 1/9] add template functions eq,lt to support uint and int compare. --- server/web/templatefunc.go | 86 ++++++++++++++++++++------------- server/web/templatefunc_test.go | 20 ++++++++ 2 files changed, 72 insertions(+), 34 deletions(-) diff --git a/server/web/templatefunc.go b/server/web/templatefunc.go index 53c99018..f83fc572 100644 --- a/server/web/templatefunc.go +++ b/server/web/templatefunc.go @@ -607,25 +607,34 @@ func eq(arg1 interface{}, arg2 ...interface{}) (bool, error) { if err != nil { return false, err } - if k1 != k2 { - return false, errBadComparison - } truth := false - switch k1 { - case boolKind: - truth = v1.Bool() == v2.Bool() - case complexKind: - truth = v1.Complex() == v2.Complex() - case floatKind: - truth = v1.Float() == v2.Float() - case intKind: - truth = v1.Int() == v2.Int() - case stringKind: - truth = v1.String() == v2.String() - case uintKind: - truth = v1.Uint() == v2.Uint() - default: - panic("invalid kind") + if k1 != k2 { + // Special case: Can compare integer values regardless of type's sign. + switch { + case k1 == intKind && k2 == uintKind: + truth = v1.Int() >= 0 && uint64(v1.Int()) == v2.Uint() + case k1 == uintKind && k2 == intKind: + truth = v2.Int() >= 0 && v1.Uint() == uint64(v2.Int()) + default: + return false, errBadComparison + } + } else { + switch k1 { + case boolKind: + truth = v1.Bool() == v2.Bool() + case complexKind: + truth = v1.Complex() == v2.Complex() + case floatKind: + truth = v1.Float() == v2.Float() + case intKind: + truth = v1.Int() == v2.Int() + case stringKind: + truth = v1.String() == v2.String() + case uintKind: + truth = v1.Uint() == v2.Uint() + default: + panic("invalid kind") + } } if truth { return true, nil @@ -653,23 +662,32 @@ func lt(arg1, arg2 interface{}) (bool, error) { if err != nil { return false, err } - if k1 != k2 { - return false, errBadComparison - } truth := false - switch k1 { - case boolKind, complexKind: - return false, errBadComparisonType - case floatKind: - truth = v1.Float() < v2.Float() - case intKind: - truth = v1.Int() < v2.Int() - case stringKind: - truth = v1.String() < v2.String() - case uintKind: - truth = v1.Uint() < v2.Uint() - default: - panic("invalid kind") + if k1 != k2 { + // Special case: Can compare integer values regardless of type's sign. + switch { + case k1 == intKind && k2 == uintKind: + truth = v1.Int() < 0 || uint64(v1.Int()) < v2.Uint() + case k1 == uintKind && k2 == intKind: + truth = v2.Int() >= 0 && v1.Uint() < uint64(v2.Int()) + default: + return false, errBadComparison + } + } else { + switch k1 { + case boolKind, complexKind: + return false, errBadComparisonType + case floatKind: + truth = v1.Float() < v2.Float() + case intKind: + truth = v1.Int() < v2.Int() + case stringKind: + truth = v1.String() < v2.String() + case uintKind: + truth = v1.Uint() < v2.Uint() + default: + panic("invalid kind") + } } return truth, nil } diff --git a/server/web/templatefunc_test.go b/server/web/templatefunc_test.go index df5cfa40..0c6f17f7 100644 --- a/server/web/templatefunc_test.go +++ b/server/web/templatefunc_test.go @@ -378,3 +378,23 @@ func TestMapGet(t *testing.T) { t.Errorf("Error happens %v", err) } } + +func Test_eq(t *testing.T) { + var a uint = 1 + var b int32 = 1 + if res, err := eq(a, b); err != nil { + if !res { + t.Error("uint(1) and int32(1) should not be eq") + } + } +} + +func Test_lt(t *testing.T) { + var a uint = 1 + var b int32 = 2 + if res, err := lt(a, b); err != nil { + if !res { + t.Error("uint(1) not lt int32(2)") + } + } +} From 7e15ea4169ec923c1d742bb8560df623d4a298a2 Mon Sep 17 00:00:00 2001 From: jianzhiyao <739319867@qq.com> Date: Fri, 14 May 2021 11:23:49 +0800 Subject: [PATCH 2/9] optimize code struct --- server/web/templatefunc.go | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/server/web/templatefunc.go b/server/web/templatefunc.go index f83fc572..0fb23ffd 100644 --- a/server/web/templatefunc.go +++ b/server/web/templatefunc.go @@ -618,27 +618,25 @@ func eq(arg1 interface{}, arg2 ...interface{}) (bool, error) { default: return false, errBadComparison } - } else { - switch k1 { - case boolKind: - truth = v1.Bool() == v2.Bool() - case complexKind: - truth = v1.Complex() == v2.Complex() - case floatKind: - truth = v1.Float() == v2.Float() - case intKind: - truth = v1.Int() == v2.Int() - case stringKind: - truth = v1.String() == v2.String() - case uintKind: - truth = v1.Uint() == v2.Uint() - default: - panic("invalid kind") - } - } - if truth { return true, nil } + switch k1 { + case boolKind: + truth = v1.Bool() == v2.Bool() + case complexKind: + truth = v1.Complex() == v2.Complex() + case floatKind: + truth = v1.Float() == v2.Float() + case intKind: + truth = v1.Int() == v2.Int() + case stringKind: + truth = v1.String() == v2.String() + case uintKind: + truth = v1.Uint() == v2.Uint() + default: + return false, errBadComparisonType + } + return truth, nil } return false, nil } From f4d4591962fa102605f8aed4d748359fe2263ace Mon Sep 17 00:00:00 2001 From: jianzhiyao <739319867@qq.com> Date: Fri, 14 May 2021 11:34:46 +0800 Subject: [PATCH 3/9] Update templatefunc.go --- server/web/templatefunc.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/server/web/templatefunc.go b/server/web/templatefunc.go index 0fb23ffd..de4e8168 100644 --- a/server/web/templatefunc.go +++ b/server/web/templatefunc.go @@ -618,7 +618,9 @@ func eq(arg1 interface{}, arg2 ...interface{}) (bool, error) { default: return false, errBadComparison } - return true, nil + if truth { + return true, nil + } } switch k1 { case boolKind: @@ -636,7 +638,9 @@ func eq(arg1 interface{}, arg2 ...interface{}) (bool, error) { default: return false, errBadComparisonType } - return truth, nil + if truth { + return true, nil + } } return false, nil } From 27f7095eec8a0fb998784e03140c8cead7532d3c Mon Sep 17 00:00:00 2001 From: letu <282130106@qq.com> Date: Fri, 14 May 2021 22:55:56 +0800 Subject: [PATCH 4/9] Add change log,add template functions eq,lt to support uint and int compare. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31eaaa0e..e7e84889 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # developing +- Add template functions eq,lt to support uint and int compare. [4607](https://github.com/beego/beego/pull/4607) - Add: Resp() method for web.Controller. [4588](https://github.com/beego/beego/pull/4588) - Web mock and test support. [4565](https://github.com/beego/beego/pull/4565) [4574](https://github.com/beego/beego/pull/4574) - Error codes definition of cache module. [4493](https://github.com/beego/beego/pull/4493) From 7655b3faa9b0072ba559d5c57871bfa401a96c53 Mon Sep 17 00:00:00 2001 From: jianzhiyao <739319867@qq.com> Date: Tue, 18 May 2021 18:29:20 +0800 Subject: [PATCH 5/9] Update templatefunc.go --- server/web/templatefunc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/web/templatefunc.go b/server/web/templatefunc.go index de4e8168..545de998 100644 --- a/server/web/templatefunc.go +++ b/server/web/templatefunc.go @@ -688,7 +688,7 @@ func lt(arg1, arg2 interface{}) (bool, error) { case uintKind: truth = v1.Uint() < v2.Uint() default: - panic("invalid kind") + return false, errBadComparisonType } } return truth, nil From 3985ce81595886384a62c5010d195904a2468951 Mon Sep 17 00:00:00 2001 From: jianzhiyao <739319867@qq.com> Date: Tue, 18 May 2021 18:30:30 +0800 Subject: [PATCH 6/9] Update templatefunc.go --- server/web/templatefunc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/web/templatefunc.go b/server/web/templatefunc.go index 545de998..c620a4b5 100644 --- a/server/web/templatefunc.go +++ b/server/web/templatefunc.go @@ -636,7 +636,7 @@ func eq(arg1 interface{}, arg2 ...interface{}) (bool, error) { case uintKind: truth = v1.Uint() == v2.Uint() default: - return false, errBadComparisonType + panic("invalid kind") } if truth { return true, nil From debd4fca01be9d07f7dcddaf788fcf5d8d0d792a Mon Sep 17 00:00:00 2001 From: letu <282130106@qq.com> Date: Thu, 20 May 2021 19:53:24 +0800 Subject: [PATCH 7/9] provided template function eq,lt unit test. --- server/web/templatefunc_test.go | 212 ++++++++++++++++++++++++++++++-- 1 file changed, 202 insertions(+), 10 deletions(-) diff --git a/server/web/templatefunc_test.go b/server/web/templatefunc_test.go index 0c6f17f7..7892cddf 100644 --- a/server/web/templatefunc_test.go +++ b/server/web/templatefunc_test.go @@ -380,21 +380,213 @@ func TestMapGet(t *testing.T) { } func Test_eq(t *testing.T) { - var a uint = 1 - var b int32 = 1 - if res, err := eq(a, b); err != nil { - if !res { - t.Error("uint(1) and int32(1) should not be eq") + tests := []struct { + a interface{} + b interface{} + result bool + }{ + {uint8(1), int(1), true}, + {uint8(3), int(1), false}, + {uint16(1), int(1), true}, + {uint16(3), int(1), false}, + {uint32(1), int(1), true}, + {uint32(3), int(1), false}, + {uint(1), int(1), true}, + {uint(3), int(1), false}, + {uint64(1), int(1), true}, + {uint64(3), int(1), false}, + + + {int8(-1), uint(1), false}, + {int16(-2), uint(1), false}, + {int32(-3), uint(1), false}, + {int64(-4), uint(1), false}, + {int8(1), uint(1), true}, + {int16(1), uint(1), true}, + {int32(1), uint(1), true}, + {int64(1), uint(1), true}, + + {int8(-1), uint8(1), false}, + {int16(-2), uint8(1), false}, + {int32(-3), uint8(1), false}, + {int64(-4), uint8(1), false}, + {int8(1), uint8(1), true}, + {int16(1), uint8(1), true}, + {int32(1), uint8(1), true}, + {int64(1), uint8(1), true}, + + {int8(-1), uint16(1), false}, + {int16(-2), uint16(1), false}, + {int32(-3), uint16(1), false}, + {int64(-4), uint16(1), false}, + {int8(1), uint16(1), true}, + {int16(1), uint16(1), true}, + {int32(1), uint16(1), true}, + {int64(1), uint16(1), true}, + + {int8(-1), uint32(1), false}, + {int16(-2), uint32(1), false}, + {int32(-3), uint32(1), false}, + {int64(-4), uint32(1), false}, + {int8(1), uint32(1), true}, + {int16(1), uint32(1), true}, + {int32(1), uint32(1), true}, + {int64(1), uint32(1), true}, + + {int8(-1), uint64(1), false}, + {int16(-2), uint64(1), false}, + {int32(-3), uint64(1), false}, + {int64(-4), uint64(1), false}, + {int8(1), uint64(1), true}, + {int16(1), uint64(1), true}, + {int32(1), uint64(1), true}, + {int64(1), uint64(1), true}, + } + for _, test := range tests { + if res, err := eq(test.a, test.b); err != nil { + if res != test.result { + t.Errorf("a:%v(%T) equals b:%v(%T) should be %v", test.a, test.a, test.b, test.b, test.result) + } } } } func Test_lt(t *testing.T) { - var a uint = 1 - var b int32 = 2 - if res, err := lt(a, b); err != nil { - if !res { - t.Error("uint(1) not lt int32(2)") + tests := []struct { + a interface{} + b interface{} + result bool + }{ + {uint8(1), int(3), true}, + {uint8(1), int(1), false}, + {uint8(3), int(1), false}, + {uint16(1), int(3), true}, + {uint16(1), int(1), false}, + {uint16(3), int(1), false}, + {uint32(1), int(3), true}, + {uint32(1), int(1), false}, + {uint32(3), int(1), false}, + {uint(1), int(3), true}, + {uint(1), int(1), false}, + {uint(3), int(1), false}, + {uint64(1), int(3), true}, + {uint64(1), int(1), false}, + {uint64(3), int(1), false}, + {int(-1), int(1), true}, + {int(1), int(1), false}, + {int(1), int(3), true}, + {int(3), int(1), false}, + + {int8(-1), uint(1), true}, + {int8(1), uint(1), false}, + {int8(1), uint(3), true}, + {int8(3), uint(1), false}, + {int16(-1), uint(1), true}, + {int16(1), uint(1), false}, + {int16(1), uint(3), true}, + {int16(3), uint(1), false}, + {int32(-1), uint(1), true}, + {int32(1), uint(1), false}, + {int32(1), uint(3), true}, + {int32(3), uint(1), false}, + {int64(-1), uint(1), true}, + {int64(1), uint(1), false}, + {int64(1), uint(3), true}, + {int64(3), uint(1), false}, + {int(-1), uint(1), true}, + {int(1), uint(1), false}, + {int(1), uint(3), true}, + {int(3), uint(1), false}, + + {int8(-1), uint8(1), true}, + {int8(1), uint8(1), false}, + {int8(1), uint8(3), true}, + {int8(3), uint8(1), false}, + {int16(-1), uint8(1), true}, + {int16(1), uint8(1), false}, + {int16(1), uint8(3), true}, + {int16(3), uint8(1), false}, + {int32(-1), uint8(1), true}, + {int32(1), uint8(1), false}, + {int32(1), uint8(3), true}, + {int32(3), uint8(1), false}, + {int64(-1), uint8(1), true}, + {int64(1), uint8(1), false}, + {int64(1), uint8(3), true}, + {int64(3), uint8(1), false}, + {int(-1), uint8(1), true}, + {int(1), uint8(1), false}, + {int(1), uint8(3), true}, + {int(3), uint8(1), false}, + + {int8(-1), uint16(1), true}, + {int8(1), uint16(1), false}, + {int8(1), uint16(3), true}, + {int8(3), uint16(1), false}, + {int16(-1), uint16(1), true}, + {int16(1), uint16(1), false}, + {int16(1), uint16(3), true}, + {int16(3), uint16(1), false}, + {int32(-1), uint16(1), true}, + {int32(1), uint16(1), false}, + {int32(1), uint16(3), true}, + {int32(3), uint16(1), false}, + {int64(-1), uint16(1), true}, + {int64(1), uint16(1), false}, + {int64(1), uint16(3), true}, + {int64(3), uint16(1), false}, + {int(-1), uint16(1), true}, + {int(1), uint16(1), false}, + {int(1), uint16(3), true}, + {int(3), uint16(1), false}, + + {int8(-1), uint32(1), true}, + {int8(1), uint32(1), false}, + {int8(1), uint32(3), true}, + {int8(3), uint32(1), false}, + {int16(-1), uint32(1), true}, + {int16(1), uint32(1), false}, + {int16(1), uint32(3), true}, + {int16(3), uint32(1), false}, + {int32(-1), uint32(1), true}, + {int32(1), uint32(1), false}, + {int32(1), uint32(3), true}, + {int32(3), uint32(1), false}, + {int64(-1), uint32(1), true}, + {int64(1), uint32(1), false}, + {int64(1), uint32(3), true}, + {int64(3), uint32(1), false}, + {int(-1), uint32(1), true}, + {int(1), uint32(1), false}, + {int(1), uint32(3), true}, + {int(3), uint32(1), false}, + + {int8(-1), uint64(1), true}, + {int8(1), uint64(1), false}, + {int8(1), uint64(3), true}, + {int8(3), uint64(1), false}, + {int16(-1), uint64(1), true}, + {int16(1), uint64(1), false}, + {int16(1), uint64(3), true}, + {int16(3), uint64(1), false}, + {int32(-1), uint64(1), true}, + {int32(1), uint64(1), false}, + {int32(1), uint64(3), true}, + {int32(3), uint64(1), false}, + {int64(-1), uint64(1), true}, + {int64(1), uint64(1), false}, + {int64(1), uint64(3), true}, + {int64(3), uint64(1), false}, + {int(-1), uint64(1), true}, + {int(1), uint64(1), false}, + {int(1), uint64(3), true}, + {int(3), uint64(1), false}, + } + for _, test := range tests { + if res, err := lt(test.a, test.b); err != nil { + if res != test.result { + t.Errorf("a:%v(%T) lt b:%v(%T) should be %v", test.a, test.a, test.b, test.b, test.result) + } } } } From a4e186065920c78ae5fd04ce5a23603e2f26c72d Mon Sep 17 00:00:00 2001 From: letu <282130106@qq.com> Date: Wed, 16 Jun 2021 23:18:07 +0800 Subject: [PATCH 8/9] provided template function eq,lt unit test. --- server/web/templatefunc_test.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/server/web/templatefunc_test.go b/server/web/templatefunc_test.go index 7892cddf..c94c3731 100644 --- a/server/web/templatefunc_test.go +++ b/server/web/templatefunc_test.go @@ -395,8 +395,6 @@ func Test_eq(t *testing.T) { {uint(3), int(1), false}, {uint64(1), int(1), true}, {uint64(3), int(1), false}, - - {int8(-1), uint(1), false}, {int16(-2), uint(1), false}, {int32(-3), uint(1), false}, @@ -405,7 +403,6 @@ func Test_eq(t *testing.T) { {int16(1), uint(1), true}, {int32(1), uint(1), true}, {int64(1), uint(1), true}, - {int8(-1), uint8(1), false}, {int16(-2), uint8(1), false}, {int32(-3), uint8(1), false}, @@ -414,7 +411,6 @@ func Test_eq(t *testing.T) { {int16(1), uint8(1), true}, {int32(1), uint8(1), true}, {int64(1), uint8(1), true}, - {int8(-1), uint16(1), false}, {int16(-2), uint16(1), false}, {int32(-3), uint16(1), false}, @@ -423,7 +419,6 @@ func Test_eq(t *testing.T) { {int16(1), uint16(1), true}, {int32(1), uint16(1), true}, {int64(1), uint16(1), true}, - {int8(-1), uint32(1), false}, {int16(-2), uint32(1), false}, {int32(-3), uint32(1), false}, @@ -432,7 +427,6 @@ func Test_eq(t *testing.T) { {int16(1), uint32(1), true}, {int32(1), uint32(1), true}, {int64(1), uint32(1), true}, - {int8(-1), uint64(1), false}, {int16(-2), uint64(1), false}, {int32(-3), uint64(1), false}, @@ -442,6 +436,7 @@ func Test_eq(t *testing.T) { {int32(1), uint64(1), true}, {int64(1), uint64(1), true}, } + for _, test := range tests { if res, err := eq(test.a, test.b); err != nil { if res != test.result { @@ -476,7 +471,6 @@ func Test_lt(t *testing.T) { {int(1), int(1), false}, {int(1), int(3), true}, {int(3), int(1), false}, - {int8(-1), uint(1), true}, {int8(1), uint(1), false}, {int8(1), uint(3), true}, @@ -497,7 +491,6 @@ func Test_lt(t *testing.T) { {int(1), uint(1), false}, {int(1), uint(3), true}, {int(3), uint(1), false}, - {int8(-1), uint8(1), true}, {int8(1), uint8(1), false}, {int8(1), uint8(3), true}, @@ -518,7 +511,6 @@ func Test_lt(t *testing.T) { {int(1), uint8(1), false}, {int(1), uint8(3), true}, {int(3), uint8(1), false}, - {int8(-1), uint16(1), true}, {int8(1), uint16(1), false}, {int8(1), uint16(3), true}, @@ -539,7 +531,6 @@ func Test_lt(t *testing.T) { {int(1), uint16(1), false}, {int(1), uint16(3), true}, {int(3), uint16(1), false}, - {int8(-1), uint32(1), true}, {int8(1), uint32(1), false}, {int8(1), uint32(3), true}, @@ -560,7 +551,6 @@ func Test_lt(t *testing.T) { {int(1), uint32(1), false}, {int(1), uint32(3), true}, {int(3), uint32(1), false}, - {int8(-1), uint64(1), true}, {int8(1), uint64(1), false}, {int8(1), uint64(3), true}, @@ -582,6 +572,7 @@ func Test_lt(t *testing.T) { {int(1), uint64(3), true}, {int(3), uint64(1), false}, } + for _, test := range tests { if res, err := lt(test.a, test.b); err != nil { if res != test.result { From 92b520d8102f162c7dd95d91eb74a67cb9038e87 Mon Sep 17 00:00:00 2001 From: letu <282130106@qq.com> Date: Fri, 25 Jun 2021 00:51:55 +0800 Subject: [PATCH 9/9] provided template function eq,lt unit test. --- server/web/templatefunc.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/web/templatefunc.go b/server/web/templatefunc.go index 6cd42faf..fce1c970 100644 --- a/server/web/templatefunc.go +++ b/server/web/templatefunc.go @@ -619,6 +619,8 @@ func eq(arg1 interface{}, arg2 ...interface{}) (bool, error) { } if truth { return true, nil + } else { + return false, nil } } switch k1 {