This commit is contained in:
Shen Junzheng
2025-03-12 01:19:35 +08:00
parent 160040f3e1
commit 78cce92ce3
70 changed files with 10134 additions and 1 deletions

148
pkg/config/config.go Normal file
View File

@@ -0,0 +1,148 @@
/*
* Copyright (c) 2023 shenjunzheng@gmail.com
*
* 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 config
import (
"errors"
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
const (
DefaultConfigType = "json"
)
var (
// ConfigName holds the name of the configuration file.
ConfigName = ""
// ConfigType specifies the type/format of the configuration file.
ConfigType = ""
// ConfigPath denotes the path to the configuration file.
ConfigPath = ""
// ERROR
ErrInvalidDirectory = errors.New("invalid directory path")
ErrMissingConfigName = errors.New("config name not specified")
)
// Init initializes the configuration settings.
// It sets up the name, type, and path for the configuration file.
func Init(name, _type, path string) error {
if len(name) == 0 {
return ErrMissingConfigName
}
if len(_type) == 0 {
_type = DefaultConfigType
}
var err error
if len(path) == 0 {
path, err = os.UserHomeDir()
if err != nil {
path = os.TempDir()
}
path += string(os.PathSeparator) + "." + name
}
if err := PrepareDir(path); err != nil {
return err
}
ConfigName = name
ConfigType = _type
ConfigPath = path
return nil
}
// Load loads the configuration from the previously initialized file.
// It unmarshals the configuration into the provided conf interface.
func Load(conf interface{}) error {
viper.SetConfigName(ConfigName)
viper.SetConfigType(ConfigType)
viper.AddConfigPath(ConfigPath)
if err := viper.ReadInConfig(); err != nil {
if err := viper.SafeWriteConfig(); err != nil {
return err
}
}
if err := viper.Unmarshal(conf); err != nil {
return err
}
SetDefault(conf)
return nil
}
// LoadFile loads the configuration from a specified file.
// It unmarshals the configuration into the provided conf interface.
func LoadFile(file string, conf interface{}) error {
viper.SetConfigFile(file)
if err := viper.ReadInConfig(); err != nil {
return err
}
if err := viper.Unmarshal(conf); err != nil {
return err
}
SetDefault(conf)
return nil
}
// SetConfig sets a configuration key to a specified value.
// It also writes the updated configuration back to the file.
func SetConfig(key string, value interface{}) error {
viper.Set(key, value)
if err := viper.WriteConfig(); err != nil {
return err
}
return nil
}
// ResetConfig resets the configuration to empty.
func ResetConfig() error {
viper.Reset()
viper.SetConfigName(ConfigName)
viper.SetConfigType(ConfigType)
viper.AddConfigPath(ConfigPath)
return viper.WriteConfig()
}
// GetConfig retrieves all configuration settings as a map.
func GetConfig() map[string]interface{} {
return viper.AllSettings()
}
// PrepareDir ensures that the specified directory path exists.
// If the directory does not exist, it attempts to create it.
func PrepareDir(path string) error {
stat, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(path, 0755); err != nil {
return err
}
} else {
return err
}
} else if !stat.IsDir() {
log.Debugf("%s is not a directory", path)
return ErrInvalidDirectory
}
return nil
}

251
pkg/config/default.go Normal file
View File

@@ -0,0 +1,251 @@
/*
* Copyright (c) 2023 shenjunzheng@gmail.com
*
* 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 config
import (
"encoding/json"
"reflect"
"strconv"
)
// DefaultTag is the default tag used to identify default values for struct fields.
var DefaultTag string
func init() {
DefaultTag = "default"
}
// SetDefaultTag updates the tag used to identify default values.
func SetDefaultTag(tag string) {
DefaultTag = tag
}
// SetDefault sets the default values for a given interface{}.
// The interface{} must be a pointer to a struct, bcs the default values are SET on the struct fields.
func SetDefault(v interface{}) {
if v == nil {
return
}
val := reflect.ValueOf(v)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
setDefault(val, "")
}
// setDefault recursively sets default values based on the struct's tags.
func setDefault(val reflect.Value, tag string) {
if !val.CanSet() {
return
}
switch val.Kind() {
case reflect.Struct:
handleStruct(val, tag)
case reflect.Ptr:
handlePtr(val, tag)
case reflect.Interface:
handleInterface(val, tag)
case reflect.Map:
handleMap(val, tag)
case reflect.Slice, reflect.Array:
handleSliceArray(val, tag)
default:
handleSimpleType(val, tag)
}
}
// handleSimpleType handles the assignment of default values for simple data types.
func handleSimpleType(val reflect.Value, tag string) {
if len(tag) == 0 || !val.IsZero() || !val.CanSet() {
return
}
// Assign appropriate default values based on the data type.
switch val.Kind() {
case reflect.String:
val.SetString(tag)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if intValue, err := strconv.ParseInt(tag, 10, 64); err == nil {
val.SetInt(intValue)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if uintVal, err := strconv.ParseUint(tag, 10, 64); err == nil {
val.SetUint(uintVal)
}
case reflect.Float32, reflect.Float64:
if floatValue, err := strconv.ParseFloat(tag, 64); err == nil {
val.SetFloat(floatValue)
}
case reflect.Bool:
if boolVal, err := strconv.ParseBool(tag); err == nil {
val.SetBool(boolVal)
}
}
}
// handleStruct processes struct type fields and sets default values based on their tags.
func handleStruct(val reflect.Value, tag string) {
if val.Kind() != reflect.Struct {
return
}
if !val.IsZero() || len(tag) == 0 {
typ := val.Type()
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
fieldType := typ.Field(i)
setDefault(field, fieldType.Tag.Get(DefaultTag))
}
return
}
if !val.CanSet() {
return
}
// If tag is provided, unmarshal the default value and set it.
newStruct := reflect.New(val.Type()).Interface()
if err := json.Unmarshal([]byte(tag), newStruct); err == nil {
newStructVal := reflect.ValueOf(newStruct).Elem()
for i := 0; i < newStructVal.NumField(); i++ {
field := newStructVal.Field(i)
fieldType := newStructVal.Type().Field(i)
setDefault(field, fieldType.Tag.Get(DefaultTag))
}
val.Set(newStructVal)
}
}
// handleSliceArray sets default values for slice or array types.
func handleSliceArray(val reflect.Value, tag string) {
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
return
}
if !val.IsZero() || len(tag) == 0 {
for i := 0; i < val.Len(); i++ {
setDefault(val.Index(i), "")
}
return
}
if !val.CanSet() {
return
}
// array 提前初始化子结构体,解决 default 长度小于 array 长度的问题
if val.Kind() == reflect.Array {
for i := 0; i < val.Len(); i++ {
setDefault(val.Index(i), "")
}
}
// If tag is provided, unmarshal the default value and set it.
newSlice := reflect.New(reflect.SliceOf(val.Type().Elem())).Interface()
if err := json.Unmarshal([]byte(tag), newSlice); err == nil {
sliceValue := reflect.ValueOf(newSlice).Elem()
for j := 0; j < sliceValue.Len(); j++ {
v := sliceValue.Index(j)
setDefault(v, "")
if val.Kind() == reflect.Array {
if j >= val.Len() {
return
}
val.Index(j).Set(v)
} else {
val.Set(reflect.Append(val, v))
}
}
}
}
// handleMap sets default values for map types.
func handleMap(val reflect.Value, tag string) {
if val.Kind() != reflect.Map {
return
}
if !val.IsZero() || len(tag) == 0 {
for _, key := range val.MapKeys() {
setDefault(val.MapIndex(key), "")
}
return
}
if !val.CanSet() {
return
}
// If tag is provided, unmarshal the default value and set it.
newMap := reflect.New(val.Type()).Interface()
if err := json.Unmarshal([]byte(tag), newMap); err == nil {
newMapVal := reflect.ValueOf(newMap).Elem()
for _, k := range newMapVal.MapKeys() {
v := newMapVal.MapIndex(k)
setDefault(v, "")
}
val.Set(newMapVal)
}
}
// handlePtr handles pointer types and sets default values based on their tags.
func handlePtr(val reflect.Value, tag string) {
if val.Kind() != reflect.Ptr {
return
}
if !val.IsZero() || len(tag) == 0 || !val.CanSet() {
return
}
// If tag is provided, unmarshal the default value and set it.
newPtr := reflect.New(val.Type()).Interface()
if err := json.Unmarshal([]byte(tag), newPtr); err == nil {
newPtrVal := reflect.ValueOf(newPtr).Elem()
setDefault(newPtrVal, "")
val.Set(newPtrVal)
}
}
// handleInterface processes interface types and sets default values based on their tags.
// support pointer interface only, bcs the default values are SET on the struct fields.
func handleInterface(val reflect.Value, tag string) {
if val.Kind() != reflect.Interface {
return
}
if val.IsNil() {
return
}
if val.Elem().Kind() != reflect.Ptr {
return
}
// Recursively set the default for the inner value of the interface.
setDefault(reflect.ValueOf(val.Elem().Interface()).Elem(), tag)
}