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

feat: 全局配置规范性修正 #1215

Open
wants to merge 3 commits into
base: v1.7.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 2 additions & 3 deletions cmd/data-service/service/global-config/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions pkg/api/core/global-config/global_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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

import "encoding/json"

// 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 json.RawMessage `json:"config_value"`
// ConfigType global config type
ConfigType string `json:"config_type"`
// Memo global config memo
Memo *string `json:"memo"`
}
19 changes: 3 additions & 16 deletions pkg/api/data-service/global_config/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -115,17 +116,3 @@ func (req *FindReq) Validate() error {

return nil
}

// GlobalConfig define cvm table.
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"`
}
18 changes: 9 additions & 9 deletions pkg/dal/table/global-config/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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"`
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion pkg/tools/util/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,32 @@ package util

import (
"fmt"
"reflect"
)

// GetStrByInterface interface to string
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个和 %v 不是等价的么,这个拆分和原版的区别是?


case reflect.Float32, reflect.Float64:
return fmt.Sprintf("%f", a)
case reflect.Slice, reflect.Array:
return fmt.Sprintf("%v", a)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch需要default

}

return ""
}