Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

he #1

Closed
wants to merge 36 commits into from
Closed

he #1

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7ef6c27
optimized: 导出GetPorts函数
ioito Oct 16, 2019
c637651
Merge pull request #31 from ioito/hotfix/qx-rule-port
yunion-ci-robot Oct 16, 2019
1453c5e
fix: 避免写log时直接传err时日志记录为{}
ioito Oct 23, 2019
00964c5
Merge pull request #32 from ioito/hotfix/qx-interface2string
yunion-ci-robot Oct 24, 2019
d832772
prometheus: use promhttp.Handler (#33)
yousong Nov 8, 2019
909a132
feature: add errors consts
Nov 10, 2019
2206ee6
Merge pull request #34 from swordqiu/feature/qj-add-error-consts
yunion-ci-robot Nov 11, 2019
1428145
feature: implement cause of aggregate error
Nov 11, 2019
4346cc3
Merge pull request #35 from swordqiu/feature/qj-aggregate-error-cause
yunion-ci-robot Nov 12, 2019
a1b1426
fix: optimized rules allowList count
Nov 13, 2019
1ad9417
Merge pull request #36 from ioito/hotfix/qx-secgroup-optimized
yunion-ci-robot Nov 16, 2019
90aa488
secrules: use sub-tests
yousong Nov 18, 2019
987750a
secrules: eliminate port range 1-65535
yousong Nov 18, 2019
5513f23
feature: filterclause support multiple params. equivalent to the union
Nov 19, 2019
c646fe8
Merge pull request #37 from yousong/bugfix/yousong-secrules
yunion-ci-robot Nov 20, 2019
d705d1a
fix: 支持匹配google cloud时间格式
Nov 20, 2019
3f1214f
Merge pull request #39 from ioito/hotfix/qx-google-time-parse
yunion-ci-robot Nov 21, 2019
e03b47b
Merge pull request #38 from swordqiu/feature/qj-filterclause-support-…
yunion-ci-robot Nov 21, 2019
1c83499
reflectutils: ignore fields properly
yousong Dec 9, 2019
a6c4a22
gotypes: add constraint notes for DeepCopy
yousong Dec 10, 2019
9b18cc7
Merge pull request #42 from yousong/bugfix/yousong-types
yunion-ci-robot Dec 13, 2019
20f2374
test: Benchmark for FetchStructFieldValueSet
rainzm Dec 17, 2019
3532215
Add StructFieldInfo Cache with FieldInfo's Copy
rainzm Dec 17, 2019
41dda45
Add StructFieldInfo Cache with Wrapped Map without FieldInfo's Copy
rainzm Dec 17, 2019
66e6c7b
Merge pull request #43 from rainzm/feature/CacheStructFieldInfo
yunion-ci-robot Dec 19, 2019
44988cc
fix: Fix the bug in case of adding FieldInfoCache
rainzm Dec 21, 2019
5097f12
Merge pull request #44 from rainzm/bugfix/reflecutil_FieldInfoCache
swordqiu Dec 21, 2019
4fc0c37
github: work around "[error]No such file or directory""
yousong Oct 4, 2019
7d3e74c
reflectutils: fix TestParseStructFieldJsonInfo_Name
yousong Dec 30, 2019
c644d9e
netutils: add tests for NewIPV4Addr
yousong Dec 30, 2019
47e2c39
netutils: use assert test in TestMasklen2Mask
yousong Dec 30, 2019
e211627
netutils: simplify Masklen2Mask
yousong Dec 30, 2019
7c8e0ad
netutils: simplify Mask2Len
yousong Dec 30, 2019
2c859f3
tristate: use assert tests
yousong Dec 30, 2019
591b186
gotypes: use assert tests
yousong Dec 30, 2019
174d962
timeutils: test equalness with time.Equal
yousong Dec 30, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v1
with:
fetch-depth: 1
- uses: actions/setup-go@v1
- uses: actions/setup-go@v1.1.2
with:
go-version: '1.13'
- name: prepare GOPATH
Expand All @@ -25,19 +25,8 @@ jobs:
workdir="$HOME/go/src/yunion.io/x"
mkdir -p "$workdir"
mv "$HOME/work/pkg/pkg" "$workdir/"
- name: go get dependencies
shell: bash
run: |
set -o xtrace
workdir="$HOME/go/src/yunion.io/x"

export GOPATH="$HOME/go"
cd "$workdir/pkg"
go get -v -t -d ./...
- name: run test
shell: bash
run: |
set -o xtrace
workdir="$HOME/go/src/yunion.io/x"
export GOPATH="$HOME/go"
cd "$workdir/pkg"
go test -v ./...
13 changes: 11 additions & 2 deletions errors/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package errors

import (
"errors"
"fmt"
)

Expand Down Expand Up @@ -78,6 +77,16 @@ func (agg aggregate) Errors() []error {
return []error(agg)
}

func (agg aggregate) Cause() error {
if len(agg) == 0 {
return nil
}
if len(agg) == 1 {
return Cause(agg[0])
}
return ErrAggregate
}

// Matcher is used to match errors. Returns true if the error matches.
type Matcher func(error) bool

Expand Down Expand Up @@ -196,4 +205,4 @@ func AggregateGoroutines(funcs ...func() error) Aggregate {
}

// ErrPreconditionViolated is returned when the precondition is violated
var ErrPreconditionViolated = errors.New("precondition is violated")
// var ErrPreconditionViolated = errors.New("precondition is violated")
12 changes: 12 additions & 0 deletions errors/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func TestSingularAggregate(t *testing.T) {
if s := agg.Errors()[0].Error(); s != "err" {
t.Errorf("expected 'err', got %q", s)
}
if s := Cause(agg).Error(); s != "err" {
t.Errorf("Cause(agg) expected 'err', got %q", s)
}

err = agg.(error)
if err == nil {
Expand All @@ -115,6 +118,9 @@ func TestSingularAggregate(t *testing.T) {
if s := err.Error(); s != "err" {
t.Errorf("expected 'err', got %q", s)
}
if s := Cause(err).Error(); s != "err" {
t.Errorf("Cause(err) expected 'err', got %q", s)
}
}

func TestPluralAggregate(t *testing.T) {
Expand All @@ -135,6 +141,9 @@ func TestPluralAggregate(t *testing.T) {
if s := agg.Errors()[0].Error(); s != "abc" {
t.Errorf("expected '[abc, 123]', got %q", s)
}
if s := Cause(agg); s != ErrAggregate {
t.Errorf("Cause(agg) expect %q, got %q", ErrAggregate, s)
}

err = agg.(error)
if err == nil {
Expand All @@ -143,6 +152,9 @@ func TestPluralAggregate(t *testing.T) {
if s := err.Error(); s != "[abc, 123]" {
t.Errorf("expected '[abc, 123]', got %q", s)
}
if s := Cause(err); s != ErrAggregate {
t.Errorf("Cause(err) expect %q, got %q", ErrAggregate, s)
}
}

func TestFilterOut(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions errors/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package errors

const (
ErrServer = Error("ServerError")
ErrClient = Error("ClientError")
ErrUnclassified = Error("UnclassifiedError")

ErrNotFound = Error("NotFoundError")
ErrDuplicateId = Error("DuplicateIdError")
ErrInvalidStatus = Error("InvalidStatusError")
ErrTimeout = Error("TimeoutError")
ErrNotImplemented = Error("NotImplementedError")
ErrNotSupported = Error("NotSupportedError")

ErrAggregate = Error("AggregateError")
)
7 changes: 7 additions & 0 deletions gotypes/deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ type IDeepCopy interface {

type DeepCopyFlags uintptr

// DeepCopy copies the passed argument recursively
//
// There are a few constraint
//
// - It cannot handle un-exported fields
// - It cannot handle circular references. Doing so will cause runtime stack
// overflow panic
func DeepCopy(v interface{}) interface{} {
rv := reflect.ValueOf(v)
cpRv := DeepCopyRv(rv)
Expand Down
27 changes: 21 additions & 6 deletions gotypes/gotypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,29 @@ func TestInCollection(t *testing.T) {
}

func TestGetInstanceTypeName(t *testing.T) {
var a int32
t.Logf("%s", GetInstanceTypeName(a))

type STestStruct struct {
{
var (
a int32
want = "int32"
)
if got := GetInstanceTypeName(a); got != want {
t.Fatalf("want %s, got %s", want, got)
}
}

t.Logf("%s", GetInstanceTypeName(STestStruct{}))
t.Logf("%s", GetInstanceTypeName(&STestStruct{}))
{
type STestStruct struct{}
var (
want = "STestStruct"
a = STestStruct{}
)
if got := GetInstanceTypeName(a); got != want {
t.Fatalf("want %s, got %s", want, got)
}
if got := GetInstanceTypeName(&a); got != want {
t.Fatalf("ptr type name: want %s, got %s", want, got)
}
}
}

func TestIsNil(t *testing.T) {
Expand Down
19 changes: 8 additions & 11 deletions tristate/tristate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ package tristate
import "testing"

func TestTriState(t *testing.T) {
if True.Bool() {
t.Logf("True == true")
}
if !False.Bool() {
t.Logf("False == false")
}
if !None.Bool() {
t.Logf("None == false")
assert := func(hold bool, fmtStr string, args ...interface{}) {
if !hold {
t.Fatalf(fmtStr, args...)
}
}
assert(True.Bool(), "True != true")
assert(!False.Bool(), "False != false")
assert(!None.Bool(), "None != false")

var val TriState
if val.IsNone() {
t.Logf("val is None")
}
assert(val.IsNone(), "val is None")
}
36 changes: 21 additions & 15 deletions util/filterclause/filterclause.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ func (jfc *SJointFilterClause) GetJointModelName() string {
return jfc.JointModel[:len(jfc.JointModel)-1]
}

func condFunc(field sqlchemy.IQueryField, params []string, cond func(field sqlchemy.IQueryField, val string) sqlchemy.ICondition) sqlchemy.ICondition {
if len(params) == 1 {
return cond(field, params[0])
} else if len(params) > 1 {
conds := make([]sqlchemy.ICondition, len(params))
for i := range params {
conds[i] = cond(field, params[i])
}
return sqlchemy.OR(conds...)
} else {
return nil
}
}

func (fc *SFilterClause) QueryCondition(q *sqlchemy.SQuery) sqlchemy.ICondition {
field := q.Field(fc.field)
if field == nil {
Expand Down Expand Up @@ -78,25 +92,17 @@ func (fc *SFilterClause) QueryCondition(q *sqlchemy.SQuery) sqlchemy.ICondition
return sqlchemy.LT(field, fc.params[0])
}
case "like":
if len(fc.params) == 1 {
return sqlchemy.Like(field, fc.params[0])
}
return condFunc(field, fc.params, sqlchemy.Like)
case "contains":
if len(fc.params) == 1 {
return sqlchemy.Contains(field, fc.params[0])
}
return condFunc(field, fc.params, sqlchemy.Contains)
case "startswith":
if len(fc.params) == 1 {
return sqlchemy.Startswith(field, fc.params[0])
}
return condFunc(field, fc.params, sqlchemy.Startswith)
case "endswith":
if len(fc.params) == 1 {
return sqlchemy.Endswith(field, fc.params[0])
}
return condFunc(field, fc.params, sqlchemy.Endswith)
case "equals":
if len(fc.params) == 1 {
return sqlchemy.Equals(field, fc.params[0])
}
return condFunc(field, fc.params, func(f sqlchemy.IQueryField, p string) sqlchemy.ICondition {
return sqlchemy.Equals(f, p)
})
case "notequals":
if len(fc.params) == 1 {
return sqlchemy.NOT(sqlchemy.Equals(field, fc.params[0]))
Expand Down
12 changes: 6 additions & 6 deletions util/netutils/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
package netutils

import (
"github.com/pkg/errors"
"yunion.io/x/pkg/errors"
)

var (
ErrInvalidNumber = errors.New("invalid number")
ErrOutOfRange = errors.New("ip number out of range [0-255]")
ErrInvalidIPAddr = errors.New("invalid ip address")
ErrInvalidMask = errors.New("invalid mask")
ErrOutOfRangeMask = errors.New("out of range masklen [0-32]")
ErrInvalidNumber = errors.Error("invalid number")
ErrOutOfRange = errors.Error("ip number out of range [0-255]")
ErrInvalidIPAddr = errors.Error("invalid ip address")
ErrInvalidMask = errors.Error("invalid mask")
ErrOutOfRangeMask = errors.Error("out of range masklen [0-32]")
)
32 changes: 17 additions & 15 deletions util/netutils/netutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ package netutils

import (
"fmt"
"math/bits"
"math/rand"
"net"
"strconv"
"strings"

"github.com/pkg/errors"

"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/regutils"
)

Expand Down Expand Up @@ -206,6 +206,19 @@ func (ar IPV4AddrRange) EndIp() IPV4Addr {
return ar.end
}

func (ar IPV4AddrRange) Merge(ar2 IPV4AddrRange) (*IPV4AddrRange, bool) {
if ar.IsOverlap(ar2) || ar.end+1 == ar2.start || ar2.end+1 == ar.start {
if ar2.start < ar.start {
ar.start = ar2.start
}
if ar2.end > ar.end {
ar.end = ar2.end
}
return &ar, true
}
return nil, false
}

func (ar IPV4AddrRange) IsOverlap(ar2 IPV4AddrRange) bool {
if ar.start > ar2.end || ar.end < ar2.start {
return false
Expand Down Expand Up @@ -308,11 +321,7 @@ func (ar IPV4AddrRange) equals(ar2 IPV4AddrRange) bool {
}

func Masklen2Mask(maskLen int8) IPV4Addr {
var mask uint32 = 0
for i := 0; i < int(maskLen); i += 1 {
mask |= 1 << uint(31-i)
}
return IPV4Addr(mask)
return IPV4Addr(^(uint32(1<<(32-maskLen)) - 1))
}

type IPV4Prefix struct {
Expand All @@ -337,14 +346,7 @@ func (pref *IPV4Prefix) Equals(pref1 *IPV4Prefix) bool {
}

func Mask2Len(mask IPV4Addr) int8 {
var maskLen int8 = 0
for {
if (mask & (1 << uint(31-maskLen))) == 0 {
break
}
maskLen += 1
}
return maskLen
return int8(bits.LeadingZeros32(^uint32(mask)))
}

func ParsePrefix(prefix string) (IPV4Addr, int8, error) {
Expand Down
Loading