From e8facd28f544bb4edbe2a533ca62e6878e025958 Mon Sep 17 00:00:00 2001 From: jianzhiyao Date: Tue, 28 Jul 2020 17:37:36 +0800 Subject: [PATCH] wrap kv --- pkg/common/kv.go | 23 +++++++++++--- pkg/common/kv_test.go | 2 +- pkg/orm/constant.go | 21 ------------ pkg/orm/db_alias.go | 11 ++++--- pkg/orm/db_alias_test.go | 16 +++------- pkg/orm/db_hints.go | 68 +++++++++++++++++++++++++++++++++++++++ pkg/orm/db_hints_test.go | 69 ++++++++++++++++++++++++++++++++++++++++ pkg/orm/models_test.go | 6 +--- 8 files changed, 168 insertions(+), 48 deletions(-) delete mode 100644 pkg/orm/constant.go create mode 100644 pkg/orm/db_hints.go create mode 100644 pkg/orm/db_hints_test.go diff --git a/pkg/common/kv.go b/pkg/common/kv.go index 508e6b5c..86a50132 100644 --- a/pkg/common/kv.go +++ b/pkg/common/kv.go @@ -14,14 +14,29 @@ package common -// KV is common structure to store key-value data. +type KV interface { + GetKey() interface{} + GetValue() interface{} +} + +// SimpleKV is common structure to store key-value data. // when you need something like Pair, you can use this -type KV struct { +type SimpleKV struct { Key interface{} Value interface{} } -// KVs will store KV collection as map +var _ KV = new(SimpleKV) + +func (s *SimpleKV) GetKey() interface{} { + return s.Key +} + +func (s *SimpleKV) GetValue() interface{} { + return s.Value +} + +// KVs will store SimpleKV collection as map type KVs struct { kvs map[interface{}]interface{} } @@ -63,7 +78,7 @@ func NewKVs(kvs ...KV) *KVs { kvs: make(map[interface{}]interface{}, len(kvs)), } for _, kv := range kvs { - res.kvs[kv.Key] = kv.Value + res.kvs[kv.GetKey()] = kv.GetValue() } return res } diff --git a/pkg/common/kv_test.go b/pkg/common/kv_test.go index 45adf5ff..275c6753 100644 --- a/pkg/common/kv_test.go +++ b/pkg/common/kv_test.go @@ -22,7 +22,7 @@ import ( func TestKVs(t *testing.T) { key := "my-key" - kvs := NewKVs(KV{ + kvs := NewKVs(&SimpleKV{ Key: key, Value: 12, }) diff --git a/pkg/orm/constant.go b/pkg/orm/constant.go deleted file mode 100644 index 14f40a7b..00000000 --- a/pkg/orm/constant.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2020 beego-dev -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package orm - -const ( - MaxIdleConnsKey = "MaxIdleConns" - MaxOpenConnsKey = "MaxOpenConns" - ConnMaxLifetimeKey = "ConnMaxLifetime" -) diff --git a/pkg/orm/db_alias.go b/pkg/orm/db_alias.go index a3f2a0b9..53c668ae 100644 --- a/pkg/orm/db_alias.go +++ b/pkg/orm/db_alias.go @@ -18,6 +18,7 @@ import ( "context" "database/sql" "fmt" + "github.com/astaxie/beego/pkg/orm/hints" "sync" "time" @@ -381,14 +382,14 @@ func AddAliasWthDB(aliasName, driverName string, db *sql.DB) error { } // RegisterDataBase Setting the database connect params. Use the database driver self dataSource args. -func RegisterDataBase(aliasName, driverName, dataSource string, params ...common.KV) error { +func RegisterDataBase(aliasName, driverName, dataSource string, hints ...common.KV) error { var ( err error db *sql.DB al *alias ) - kvs := common.NewKVs(params...) + kvs := common.NewKVs(hints...) db, err = sql.Open(driverName, dataSource) if err != nil { @@ -405,11 +406,11 @@ func RegisterDataBase(aliasName, driverName, dataSource string, params ...common detectTZ(al) - kvs.IfContains(MaxIdleConnsKey, func(value interface{}) { + kvs.IfContains(maxIdleConnectionsKey, func(value interface{}) { SetMaxIdleConns(al.Name, value.(int)) - }).IfContains(MaxOpenConnsKey, func(value interface{}) { + }).IfContains(maxOpenConnectionsKey, func(value interface{}) { SetMaxOpenConns(al.Name, value.(int)) - }).IfContains(ConnMaxLifetimeKey, func(value interface{}) { + }).IfContains(connMaxLifetimeKey, func(value interface{}) { SetConnMaxLifetime(al.Name, value.(time.Duration)) }) diff --git a/pkg/orm/db_alias_test.go b/pkg/orm/db_alias_test.go index a0cdcd44..28dd2e6b 100644 --- a/pkg/orm/db_alias_test.go +++ b/pkg/orm/db_alias_test.go @@ -19,21 +19,13 @@ import ( "time" "github.com/stretchr/testify/assert" - - "github.com/astaxie/beego/pkg/common" ) func TestRegisterDataBase(t *testing.T) { - err := RegisterDataBase("test-params", DBARGS.Driver, DBARGS.Source, common.KV{ - Key: MaxIdleConnsKey, - Value: 20, - }, common.KV{ - Key: MaxOpenConnsKey, - Value: 300, - }, common.KV{ - Key: ConnMaxLifetimeKey, - Value: time.Minute, - }) + err := RegisterDataBase("test-params", DBARGS.Driver, DBARGS.Source, + MaxIdleConnections(20), + MaxOpenConnections(300), + ConnMaxLifetime(time.Minute)) assert.Nil(t, err) al := getDbAlias("test-params") diff --git a/pkg/orm/db_hints.go b/pkg/orm/db_hints.go new file mode 100644 index 00000000..8900d599 --- /dev/null +++ b/pkg/orm/db_hints.go @@ -0,0 +1,68 @@ +// Copyright 2020 beego-dev +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package orm + +import ( + "github.com/astaxie/beego/pkg/common" + "time" +) + +type Hint struct { + key interface{} + value interface{} +} + +var _ common.KV = new(Hint) + +// GetKey return key +func (s *Hint) GetKey() interface{} { + return s.key +} + +// GetValue return value +func (s *Hint) GetValue() interface{} { + return s.value +} + +const ( + maxIdleConnectionsKey = "MaxIdleConnections" + maxOpenConnectionsKey = "MaxOpenConnections" + connMaxLifetimeKey = "ConnMaxLifetime" +) + +var _ common.KV = new(Hint) + +// MaxIdleConnections return a hint about MaxIdleConnections +func MaxIdleConnections(v int) *Hint { + return NewHint(maxIdleConnectionsKey, v) +} + +// MaxOpenConnections return a hint about MaxOpenConnections +func MaxOpenConnections(v int) *Hint { + return NewHint(maxOpenConnectionsKey, v) +} + +// ConnMaxLifetime return a hint about ConnMaxLifetime +func ConnMaxLifetime(v time.Duration) *Hint { + return NewHint(connMaxLifetimeKey, v) +} + +// NewHint return a hint +func NewHint(key interface{}, value interface{}) *Hint { + return &Hint{ + key: key, + value: value, + } +} diff --git a/pkg/orm/db_hints_test.go b/pkg/orm/db_hints_test.go new file mode 100644 index 00000000..9b62a730 --- /dev/null +++ b/pkg/orm/db_hints_test.go @@ -0,0 +1,69 @@ +// Copyright 2020 beego-dev +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package orm + +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +func TestNewHint_time(t *testing.T) { + key := "qweqwe" + value := time.Second + hint := NewHint(key, value) + + assert.Equal(t, hint.GetKey(), key) + assert.Equal(t, hint.GetValue(), value) +} + +func TestNewHint_int(t *testing.T) { + key := "qweqwe" + value := 281230 + hint := NewHint(key, value) + + assert.Equal(t, hint.GetKey(), key) + assert.Equal(t, hint.GetValue(), value) +} + +func TestNewHint_float(t *testing.T) { + key := "qweqwe" + value := 21.2459753 + hint := NewHint(key, value) + + assert.Equal(t, hint.GetKey(), key) + assert.Equal(t, hint.GetValue(), value) +} + +func TestMaxOpenConnections(t *testing.T) { + i := 887423 + hint := MaxOpenConnections(i) + assert.Equal(t, hint.GetValue(), i) + assert.Equal(t, hint.GetKey(), maxOpenConnectionsKey) +} + +func TestConnMaxLifetime(t *testing.T) { + i := time.Hour + hint := ConnMaxLifetime(i) + assert.Equal(t, hint.GetValue(), i) + assert.Equal(t, hint.GetKey(), connMaxLifetimeKey) +} + +func TestMaxIdleConnections(t *testing.T) { + i := 42316 + hint := MaxIdleConnections(i) + assert.Equal(t, hint.GetValue(), i) + assert.Equal(t, hint.GetKey(), maxIdleConnectionsKey) +} diff --git a/pkg/orm/models_test.go b/pkg/orm/models_test.go index 4c00050d..ae166dc7 100644 --- a/pkg/orm/models_test.go +++ b/pkg/orm/models_test.go @@ -28,7 +28,6 @@ import ( // As tidb can't use go get, so disable the tidb testing now // _ "github.com/pingcap/tidb" - "github.com/astaxie/beego/pkg/common" ) // A slice string field. @@ -489,10 +488,7 @@ func init() { os.Exit(2) } - err := RegisterDataBase("default", DBARGS.Driver, DBARGS.Source, common.KV{ - Key: MaxIdleConnsKey, - Value: 20, - }) + err := RegisterDataBase("default", DBARGS.Driver, DBARGS.Source, MaxIdleConnections(20)) if err != nil { panic(fmt.Sprintf("can not register database: %v", err))