From 1d1423d9fc02c8e94dddb22c6155ad7643fe84ae Mon Sep 17 00:00:00 2001 From: zouxingyuks <1308345487@qq.com> Date: Wed, 11 Dec 2024 17:29:40 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E4=BF=AE=E5=A4=8D=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E9=85=8D=E7=BD=AE=E4=B8=AD=E7=9A=84=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83=E6=80=A7=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/api/data-service/global_config/global_config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/api/data-service/global_config/global_config.go b/pkg/api/data-service/global_config/global_config.go index 95fcfd2d6..cf62fcde9 100644 --- a/pkg/api/data-service/global_config/global_config.go +++ b/pkg/api/data-service/global_config/global_config.go @@ -116,7 +116,7 @@ func (req *FindReq) Validate() error { return nil } -// GlobalConfig define cvm table. +// GlobalConfig ... type GlobalConfig struct { // ID global config id ID string `json:"id"` From 8520b9419bab59f44da31ddca909a7491d8a5ce0 Mon Sep 17 00:00:00 2001 From: zouxingyuks <1308345487@qq.com> Date: Wed, 11 Dec 2024 19:30:44 +0800 Subject: [PATCH 2/4] =?UTF-8?q?feat(global-config):=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E9=85=8D=E7=BD=AE=E7=BB=93=E6=9E=84=E4=BD=93?= =?UTF-8?q?=E5=8F=8A=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/api/core/global-config/global_config.go | 35 +++++++++++++++++++ .../global_config/global_config.go | 19 ++-------- pkg/dal/table/global-config/global_config.go | 18 +++++----- pkg/tools/util/conv.go | 22 +++++++++++- 4 files changed, 68 insertions(+), 26 deletions(-) create mode 100644 pkg/api/core/global-config/global_config.go diff --git a/pkg/api/core/global-config/global_config.go b/pkg/api/core/global-config/global_config.go new file mode 100644 index 000000000..558228c5b --- /dev/null +++ b/pkg/api/core/global-config/global_config.go @@ -0,0 +1,35 @@ +/* + * TencentBlueKing is pleased to support the open source community by making + * 蓝鲸智云 - 混合云管理平台 (BlueKing - Hybrid Cloud Management System) available. + * Copyright (C) 2022 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * 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. + * + * We undertake not to change the open source license (MIT license) applicable + * + * to the current version of the project delivered to anyone in the future. + */ + +// Package core ... +package core + +// GlobalConfig ... +type GlobalConfig struct { + // ID global config id + ID string `json:"id"` + // ConfigKey global config key, key+type is unique + ConfigKey string `json:"config_key"` + // ConfigValue global config value, json format + ConfigValue interface{} `json:"config_value"` + // ConfigType global config type + ConfigType string `json:"config_type"` + // Memo global config memo + Memo *string `json:"memo"` +} diff --git a/pkg/api/data-service/global_config/global_config.go b/pkg/api/data-service/global_config/global_config.go index cf62fcde9..02b26a80e 100644 --- a/pkg/api/data-service/global_config/global_config.go +++ b/pkg/api/data-service/global_config/global_config.go @@ -24,6 +24,7 @@ import ( "fmt" "hcm/pkg/api/core" + cgconf "hcm/pkg/api/core/global-config" "hcm/pkg/criteria/validator" tablegconf "hcm/pkg/dal/table/global-config" ) @@ -51,7 +52,7 @@ type ListResp core.ListResultT[tablegconf.GlobalConfigTable] // BatchCreateReq ... type BatchCreateReq struct { - Configs []GlobalConfig `json:"configs" validate:"required,min=1"` + Configs []cgconf.GlobalConfig `json:"configs" validate:"required,min=1,max=100"` } // Validate BatchCreateReq @@ -65,7 +66,7 @@ func (req *BatchCreateReq) Validate() error { // BatchUpdateReq ... type BatchUpdateReq struct { - Configs []GlobalConfig `json:"configs" validate:"required,min=1"` + Configs []cgconf.GlobalConfig `json:"configs" validate:"required,min=1,max=100"` } // Validate BatchUpdateReq @@ -115,17 +116,3 @@ func (req *FindReq) Validate() error { return nil } - -// GlobalConfig ... -type GlobalConfig struct { - // ID global config id - ID string `json:"id"` - // ConfigKey global config key, key+type is unique - ConfigKey string `json:"config_key"` - // ConfigValue global config value, json format - ConfigValue interface{} `json:"config_value"` - // ConfigType global config type - ConfigType string `json:"config_type"` - // Memo global config memo - Memo *string `json:"memo"` -} diff --git a/pkg/dal/table/global-config/global_config.go b/pkg/dal/table/global-config/global_config.go index 110be214b..9ea70ea3e 100644 --- a/pkg/dal/table/global-config/global_config.go +++ b/pkg/dal/table/global-config/global_config.go @@ -31,10 +31,10 @@ import ( "hcm/pkg/dal/table/utils" ) -// GlobalConfigTableColumns defines all the cvm table's columns. +// GlobalConfigTableColumns defines all the global config table's columns. var GlobalConfigTableColumns = utils.MergeColumns(nil, GlobalConfigTableColumnDescriptors) -// GlobalConfigTableColumnDescriptors is cvm table column descriptors. +// GlobalConfigTableColumnDescriptors is global config table column descriptors. var GlobalConfigTableColumnDescriptors = utils.ColumnDescriptors{ {Column: "id", NamedC: "id", Type: enumor.String}, {Column: "config_key", NamedC: "config_key", Type: enumor.String}, @@ -47,7 +47,7 @@ var GlobalConfigTableColumnDescriptors = utils.ColumnDescriptors{ {Column: "updated_at", NamedC: "updated_at", Type: enumor.String}, } -// GlobalConfigTable define cvm table. +// GlobalConfigTable define global config table. type GlobalConfigTable struct { // ID global config id ID string `db:"id" json:"id"` @@ -69,27 +69,27 @@ type GlobalConfigTable struct { UpdatedAt types.Time `db:"updated_at" json:"updated_at"` } -// UniqueKey returns the unique key of BizOrgRel table. +// UniqueKey returns the unique key of global config table. func (t GlobalConfigTable) UniqueKey() string { return fmt.Sprintf("(%s,%s)", t.ConfigType, t.ConfigKey) } -// Columns return cvm table columns. +// Columns return global config table columns. func (t GlobalConfigTable) Columns() *utils.Columns { return GlobalConfigTableColumns } -// ColumnDescriptors define cvm table column descriptor. +// ColumnDescriptors define global config table column descriptor. func (t GlobalConfigTable) ColumnDescriptors() utils.ColumnDescriptors { return GlobalConfigTableColumnDescriptors } -// TableName return cvm table name. +// TableName return global config table name. func (t GlobalConfigTable) TableName() table.Name { return table.GlobalConfigTable } -// InsertValidate cvm table when insert. +// InsertValidate global config table when insert. func (t GlobalConfigTable) InsertValidate() error { // length validate. if err := validator.Validate.Struct(t); err != nil { @@ -127,7 +127,7 @@ func (t GlobalConfigTable) InsertValidate() error { return nil } -// UpdateValidate cvm table when update. +// UpdateValidate global config table when update. func (t GlobalConfigTable) UpdateValidate() error { if err := validator.Validate.Struct(t); err != nil { return err diff --git a/pkg/tools/util/conv.go b/pkg/tools/util/conv.go index 50bcb4397..5cf26710f 100644 --- a/pkg/tools/util/conv.go +++ b/pkg/tools/util/conv.go @@ -15,6 +15,7 @@ package util import ( "fmt" + "reflect" ) // GetStrByInterface interface to string @@ -22,5 +23,24 @@ func GetStrByInterface(a interface{}) string { if nil == a { return "" } - return fmt.Sprintf("%v", a) + + typeOf := reflect.TypeOf(a) + for typeOf.Kind() == reflect.Ptr { + typeOf = typeOf.Elem() + } + + switch typeOf.Kind() { + case reflect.String: + return a.(string) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return fmt.Sprintf("%d", a) + + case reflect.Float32, reflect.Float64: + return fmt.Sprintf("%f", a) + case reflect.Slice, reflect.Array: + return fmt.Sprintf("%v", a) + } + + return "" } From ff0b6b05361f193b7ef3cda7a1c96e14edc7b7a7 Mon Sep 17 00:00:00 2001 From: zouxingyuks <1308345487@qq.com> Date: Fri, 20 Dec 2024 16:21:14 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix(global-config):=20=E7=A7=BB=E9=99=A4uti?= =?UTF-8?q?l=E5=8C=85=E4=BE=9D=E8=B5=96=E5=B9=B6=E4=BC=98=E5=8C=96ConfigVa?= =?UTF-8?q?lue=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/data-service/service/global-config/global_config.go | 5 ++--- pkg/api/core/global-config/global_config.go | 4 +++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/data-service/service/global-config/global_config.go b/cmd/data-service/service/global-config/global_config.go index 01e718b69..59fc532c9 100644 --- a/cmd/data-service/service/global-config/global_config.go +++ b/cmd/data-service/service/global-config/global_config.go @@ -33,7 +33,6 @@ import ( dtypes "hcm/pkg/dal/table/types" "hcm/pkg/logs" "hcm/pkg/rest" - "hcm/pkg/tools/util" "github.com/jmoiron/sqlx" ) @@ -57,7 +56,7 @@ func (svc *service) BatchCreateGlobalConfigs(cts *rest.Contexts) (interface{}, e for index, config := range req.Configs { globalConfigs[index] = tablegconf.GlobalConfigTable{ ConfigKey: config.ConfigKey, - ConfigValue: dtypes.JsonField(util.GetStrByInterface(config.ConfigValue)), + ConfigValue: dtypes.JsonField(config.ConfigValue), ConfigType: config.ConfigType, Memo: config.Memo, Creator: cts.Kit.User, @@ -133,7 +132,7 @@ func (svc *service) BatchUpdateGlobalConfigs(cts *rest.Contexts) (interface{}, e } if config.ConfigValue != nil { - record.ConfigValue = dtypes.JsonField(util.GetStrByInterface(config.ConfigValue)) + record.ConfigValue = dtypes.JsonField(config.ConfigValue) } if config.Memo != nil { diff --git a/pkg/api/core/global-config/global_config.go b/pkg/api/core/global-config/global_config.go index 558228c5b..4396c7f80 100644 --- a/pkg/api/core/global-config/global_config.go +++ b/pkg/api/core/global-config/global_config.go @@ -20,6 +20,8 @@ // Package core ... package core +import "encoding/json" + // GlobalConfig ... type GlobalConfig struct { // ID global config id @@ -27,7 +29,7 @@ type GlobalConfig struct { // ConfigKey global config key, key+type is unique ConfigKey string `json:"config_key"` // ConfigValue global config value, json format - ConfigValue interface{} `json:"config_value"` + ConfigValue json.RawMessage `json:"config_value"` // ConfigType global config type ConfigType string `json:"config_type"` // Memo global config memo From 7192b9f4acff9322a4abb33d9471aa3c10477c2d Mon Sep 17 00:00:00 2001 From: chenjr15 Date: Wed, 8 Jan 2025 12:08:23 +0800 Subject: [PATCH 4/4] remove unused function --- pkg/tools/util/conv.go | 46 ------------------------------------------ 1 file changed, 46 deletions(-) delete mode 100644 pkg/tools/util/conv.go diff --git a/pkg/tools/util/conv.go b/pkg/tools/util/conv.go deleted file mode 100644 index 5cf26710f..000000000 --- a/pkg/tools/util/conv.go +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making 蓝鲸 available. - * Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved. - * Licensed under the MIT License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * http://opensource.org/licenses/MIT - * 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 util ... -package util - -import ( - "fmt" - "reflect" -) - -// GetStrByInterface interface to string -func GetStrByInterface(a interface{}) string { - if nil == a { - return "" - } - - typeOf := reflect.TypeOf(a) - for typeOf.Kind() == reflect.Ptr { - typeOf = typeOf.Elem() - } - - switch typeOf.Kind() { - case reflect.String: - return a.(string) - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fmt.Sprintf("%d", a) - - case reflect.Float32, reflect.Float64: - return fmt.Sprintf("%f", a) - case reflect.Slice, reflect.Array: - return fmt.Sprintf("%v", a) - } - - return "" -}