github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/isyscore/isc-gobase/listener"
     6  	"log"
     7  	"os"
     8  	"path"
     9  	"path/filepath"
    10  	"reflect"
    11  	"strings"
    12  	"sync"
    13  
    14  	"github.com/gin-gonic/gin"
    15  	"github.com/isyscore/isc-gobase/file"
    16  
    17  	"github.com/isyscore/isc-gobase/isc"
    18  	"gopkg.in/yaml.v2"
    19  )
    20  
    21  var appProperty *ApplicationProperty
    22  var configExist = false
    23  var loadLock sync.Mutex
    24  var configLoaded = false
    25  var CurrentProfile = ""
    26  
    27  func LoadConfig() {
    28  	loadLock.Lock()
    29  	defer loadLock.Unlock()
    30  	if configLoaded {
    31  		return
    32  	}
    33  
    34  	LoadConfigFromRelativePath("")
    35  	configLoaded = true
    36  }
    37  
    38  func LoadConfigFromRelativePath(resourceAbsPath string) {
    39  	dir, _ := os.Getwd()
    40  	pkg := strings.Replace(dir, "\\", "/", -1)
    41  
    42  	LoadConfigFromAbsPath(path.Join(pkg, "", resourceAbsPath))
    43  }
    44  
    45  func LoadConfigFromAbsPath(resourceAbsPath string) {
    46  	doLoadConfigFromAbsPath(resourceAbsPath)
    47  
    48  	cmPath := os.Getenv("base.config.additional-location")
    49  	if cmPath == "" {
    50  		cmPath = "./config/application-default.yml"
    51  	}
    52  	AppendConfigFromRelativePath(cmPath)
    53  
    54  	ApiModule = GetValueString("api-module")
    55  
    56  	if err := GetValueObject("base", &BaseCfg); err != nil {
    57  		log.Printf("加载 Base 配置失败(%v)", err)
    58  	}
    59  }
    60  
    61  func AppendConfigFromRelativePath(fileName string) {
    62  	dir, _ := os.Getwd()
    63  	pkg := strings.Replace(dir, "\\", "/", -1)
    64  	fileName = path.Join(pkg, "", fileName)
    65  	extend := getFileExtension(fileName)
    66  	extend = strings.ToLower(extend)
    67  	switch extend {
    68  	case "yaml":
    69  		AppendYamlFile(fileName)
    70  	case "yml":
    71  		AppendYamlFile(fileName)
    72  	case "properties":
    73  		AppendPropertyFile(fileName)
    74  	case "json":
    75  		AppendJsonFile(fileName)
    76  	}
    77  }
    78  
    79  func AppendConfigFromAbsPath(fileName string) {
    80  	extend := getFileExtension(fileName)
    81  	extend = strings.ToLower(extend)
    82  	switch extend {
    83  	case "yaml":
    84  		AppendYamlFile(fileName)
    85  	case "yml":
    86  		AppendYamlFile(fileName)
    87  	case "properties":
    88  		AppendPropertyFile(fileName)
    89  	case "json":
    90  		AppendJsonFile(fileName)
    91  	}
    92  }
    93  
    94  type EnvProperty struct {
    95  	Key   string
    96  	Value string
    97  }
    98  
    99  func ExistConfigFile() bool {
   100  	return configExist
   101  }
   102  
   103  func GetConfigValues(c *gin.Context) {
   104  	if nil != appProperty {
   105  		c.Data(200, "application/json; charset=utf-8", []byte(isc.ObjectToJson(appProperty.ValueMap)))
   106  	} else {
   107  		c.Data(200, "application/json; charset=utf-8", []byte("{}"))
   108  	}
   109  }
   110  
   111  func GetConfigDeepValues(c *gin.Context) {
   112  	if nil != appProperty {
   113  		c.Data(200, "application/json; charset=utf-8", []byte(isc.ObjectToJson(appProperty.ValueDeepMap)))
   114  	} else {
   115  		c.Data(200, "application/json; charset=utf-8", []byte("{}"))
   116  	}
   117  }
   118  
   119  func GetConfigValue(c *gin.Context) {
   120  	if nil != appProperty {
   121  		value := GetValue(c.Param("key"))
   122  		if nil == value {
   123  			c.Data(200, "application/json; charset=utf-8", []byte(""))
   124  			return
   125  		}
   126  		if isc.IsBaseType(reflect.TypeOf(value)) {
   127  			c.Data(200, "application/json; charset=utf-8", []byte(isc.ToString(value)))
   128  		} else {
   129  			c.Data(200, "application/json; charset=utf-8", []byte(isc.ObjectToJson(value)))
   130  		}
   131  	} else {
   132  		c.Data(200, "application/json; charset=utf-8", []byte("{}"))
   133  	}
   134  }
   135  
   136  func UpdateConfig(c *gin.Context) {
   137  	valueMap := map[string]any{}
   138  	err := isc.DataToObject(c.Request.Body, &valueMap)
   139  	if err != nil {
   140  		log.Printf("解析失败,%v", err.Error())
   141  		return
   142  	}
   143  
   144  	key, _ := valueMap["key"]
   145  	value, _ := valueMap["value"]
   146  
   147  	SetValue(key.(string), value)
   148  }
   149  
   150  // 多种格式优先级:json > properties > yaml > yml
   151  func doLoadConfigFromAbsPath(resourceAbsPath string) {
   152  	if !strings.HasSuffix(resourceAbsPath, "/") {
   153  		resourceAbsPath += "/"
   154  	}
   155  	files, err := os.ReadDir(resourceAbsPath)
   156  	if err != nil {
   157  		return
   158  	}
   159  
   160  	if appProperty == nil {
   161  		appProperty = &ApplicationProperty{}
   162  		appProperty.ValueMap = make(map[string]interface{})
   163  		appProperty.ValueDeepMap = make(map[string]interface{})
   164  	} else if appProperty.ValueMap == nil {
   165  		appProperty.ValueMap = make(map[string]interface{})
   166  	} else if appProperty.ValueDeepMap == nil {
   167  		appProperty.ValueDeepMap = make(map[string]interface{})
   168  	}
   169  
   170  	LoadYamlFile(resourceAbsPath + "application.yaml")
   171  	LoadYamlFile(resourceAbsPath + "application.yml")
   172  	LoadPropertyFile(resourceAbsPath + "application.properties")
   173  	LoadJsonFile(resourceAbsPath + "application.json")
   174  
   175  	for _, fileInfo := range files {
   176  		if fileInfo.IsDir() {
   177  			continue
   178  		}
   179  
   180  		fileName := fileInfo.Name()
   181  		if !strings.HasPrefix(fileName, "application") {
   182  			continue
   183  		}
   184  
   185  		// 默认配置
   186  		if fileName == "application.yaml" {
   187  			configExist = true
   188  			break
   189  		} else if fileName == "application.yml" {
   190  			configExist = true
   191  			break
   192  		} else if fileName == "application.properties" {
   193  			configExist = true
   194  			break
   195  		} else if fileName == "application.json" {
   196  			configExist = true
   197  			break
   198  		}
   199  
   200  		profile := getActiveProfile()
   201  		if profile != "" {
   202  			CurrentProfile = profile
   203  			SetValue("base.profiles.active", profile)
   204  			currentProfile := getProfileFromFileName(fileName)
   205  			if currentProfile == profile {
   206  				configExist = true
   207  				AppendFile(resourceAbsPath + fileName)
   208  			}
   209  		}
   210  	}
   211  }
   212  
   213  func LoadFile(filePath string) {
   214  	extend := getFileExtension(filePath)
   215  	extend = strings.ToLower(extend)
   216  	if extend == "yaml" {
   217  		configExist = true
   218  		LoadYamlFile(filePath)
   219  	} else if extend == "yml" {
   220  		configExist = true
   221  		LoadYamlFile(filePath)
   222  	} else if extend == "properties" {
   223  		configExist = true
   224  		LoadPropertyFile(filePath)
   225  	} else if extend == "json" {
   226  		configExist = true
   227  		LoadJsonFile(filePath)
   228  	}
   229  }
   230  
   231  func AppendFile(filePath string) {
   232  	extend := getFileExtension(filePath)
   233  	extend = strings.ToLower(extend)
   234  	if extend == "yaml" {
   235  		AppendYamlFile(filePath)
   236  	} else if extend == "yml" {
   237  		AppendYamlFile(filePath)
   238  	} else if extend == "properties" {
   239  		AppendPropertyFile(filePath)
   240  	} else if extend == "json" {
   241  		AppendJsonFile(filePath)
   242  	}
   243  }
   244  
   245  // 临时写死
   246  // 优先级:环境变量 > 本地配置
   247  func getActiveProfile() string {
   248  	profile := os.Getenv("base.profiles.active")
   249  	if profile != "" {
   250  		return profile
   251  	}
   252  
   253  	profile = GetValueString("base.profiles.active")
   254  	if profile != "" {
   255  		return profile
   256  	}
   257  	return ""
   258  }
   259  
   260  func getProfileFromFileName(fileName string) string {
   261  	if strings.HasPrefix(fileName, "application-") {
   262  		words := strings.SplitN(fileName, ".", 2)
   263  		appNames := words[0]
   264  
   265  		appNameAndProfile := strings.SplitN(appNames, "-", 2)
   266  		return appNameAndProfile[1]
   267  	}
   268  	return ""
   269  }
   270  
   271  func getFileExtension(fileName string) string {
   272  	if strings.Contains(fileName, ".") {
   273  		lastIndex := strings.LastIndex(fileName, ".")
   274  		if lastIndex > -1 {
   275  			return fileName[lastIndex+1:]
   276  		}
   277  	}
   278  	return ""
   279  }
   280  
   281  func LoadYamlFile(filePath string) {
   282  	if !file.FileExists(filePath) {
   283  		return
   284  	}
   285  	content, err := os.ReadFile(filePath)
   286  	if err != nil {
   287  		// log.Printf("读取文件失败(%v)", err)
   288  		return
   289  	}
   290  
   291  	if appProperty == nil {
   292  		appProperty = &ApplicationProperty{}
   293  		appProperty.ValueMap = make(map[string]interface{})
   294  		appProperty.ValueDeepMap = make(map[string]interface{})
   295  	} else if appProperty.ValueMap == nil {
   296  		appProperty.ValueMap = make(map[string]interface{})
   297  	} else if appProperty.ValueDeepMap == nil {
   298  		appProperty.ValueDeepMap = make(map[string]interface{})
   299  	}
   300  
   301  	property, err := isc.YamlToProperties(string(content))
   302  	if err != nil {
   303  		return
   304  	}
   305  	valueMap, _ := isc.PropertiesToMap(property)
   306  	appProperty.ValueMap = valueMap
   307  
   308  	yamlMap, err := isc.YamlToMap(string(content))
   309  	if err != nil {
   310  		return
   311  	}
   312  	appProperty.ValueDeepMap = yamlMap
   313  }
   314  
   315  func AppendYamlFile(filePath string) {
   316  	if !file.FileExists(filePath) {
   317  		return
   318  	}
   319  	content, err := os.ReadFile(filePath)
   320  	if err != nil {
   321  		// log.Printf("读取文件失败(%v)", err)
   322  		return
   323  	}
   324  
   325  	if appProperty == nil {
   326  		appProperty = &ApplicationProperty{}
   327  		appProperty.ValueMap = make(map[string]interface{})
   328  		appProperty.ValueDeepMap = make(map[string]interface{})
   329  	} else if appProperty.ValueMap == nil {
   330  		appProperty.ValueMap = make(map[string]interface{})
   331  	} else if appProperty.ValueDeepMap == nil {
   332  		appProperty.ValueDeepMap = make(map[string]interface{})
   333  	}
   334  
   335  	property, err := isc.YamlToProperties(string(content))
   336  	if err != nil {
   337  		return
   338  	}
   339  	AppendValue(property)
   340  }
   341  
   342  func LoadPropertyFile(filePath string) {
   343  	if !file.FileExists(filePath) {
   344  		return
   345  	}
   346  	content, err := os.ReadFile(filePath)
   347  	if err != nil {
   348  		// log.Printf("读取文件失败(%v)", err)
   349  		return
   350  	}
   351  
   352  	if appProperty == nil {
   353  		appProperty = &ApplicationProperty{}
   354  		appProperty.ValueMap = make(map[string]interface{})
   355  		appProperty.ValueDeepMap = make(map[string]interface{})
   356  	} else if appProperty.ValueMap == nil {
   357  		appProperty.ValueMap = make(map[string]interface{})
   358  	} else if appProperty.ValueDeepMap == nil {
   359  		appProperty.ValueDeepMap = make(map[string]interface{})
   360  	}
   361  
   362  	valueMap, _ := isc.PropertiesToMap(string(content))
   363  	appProperty.ValueMap = valueMap
   364  
   365  	yamlStr, _ := isc.PropertiesToYaml(string(content))
   366  	yamlMap, _ := isc.YamlToMap(yamlStr)
   367  	appProperty.ValueDeepMap = yamlMap
   368  }
   369  
   370  func AppendPropertyFile(filePath string) {
   371  	if !file.FileExists(filePath) {
   372  		return
   373  	}
   374  	content, err := os.ReadFile(filePath)
   375  	if err != nil {
   376  		log.Printf("读取文件失败(%v)", err)
   377  		return
   378  	}
   379  
   380  	if appProperty == nil {
   381  		appProperty = &ApplicationProperty{}
   382  		appProperty.ValueMap = make(map[string]interface{})
   383  		appProperty.ValueDeepMap = make(map[string]interface{})
   384  	} else if appProperty.ValueMap == nil {
   385  		appProperty.ValueMap = make(map[string]interface{})
   386  	} else if appProperty.ValueDeepMap == nil {
   387  		appProperty.ValueDeepMap = make(map[string]interface{})
   388  	}
   389  
   390  	valueMap, err := isc.PropertiesToMap(string(content))
   391  	if err != nil {
   392  		return
   393  	}
   394  	propertiesValue, err := isc.MapToProperties(valueMap)
   395  	if err != nil {
   396  		return
   397  	}
   398  
   399  	AppendValue(propertiesValue)
   400  }
   401  
   402  func LoadJsonFile(filePath string) {
   403  	if !file.FileExists(filePath) {
   404  		return
   405  	}
   406  	content, err := os.ReadFile(filePath)
   407  	if err != nil {
   408  		// log.Printf("读取文件失败(%v)", err)
   409  		return
   410  	}
   411  
   412  	if appProperty == nil {
   413  		appProperty = &ApplicationProperty{}
   414  		appProperty.ValueMap = make(map[string]interface{})
   415  		appProperty.ValueDeepMap = make(map[string]interface{})
   416  	} else if appProperty.ValueMap == nil {
   417  		appProperty.ValueMap = make(map[string]interface{})
   418  	} else if appProperty.ValueDeepMap == nil {
   419  		appProperty.ValueDeepMap = make(map[string]interface{})
   420  	}
   421  
   422  	yamlStr, _ := isc.JsonToYaml(string(content))
   423  	property, _ := isc.YamlToProperties(yamlStr)
   424  	valueMap, _ := isc.PropertiesToMap(property)
   425  	appProperty.ValueMap = valueMap
   426  
   427  	yamlMap, _ := isc.YamlToMap(yamlStr)
   428  	appProperty.ValueDeepMap = yamlMap
   429  }
   430  
   431  func AppendJsonFile(filePath string) {
   432  	if !file.FileExists(filePath) {
   433  		return
   434  	}
   435  	content, err := os.ReadFile(filePath)
   436  	if err != nil {
   437  		log.Printf("读取文件失败(%v)", err)
   438  		return
   439  	}
   440  
   441  	if appProperty == nil {
   442  		appProperty = &ApplicationProperty{}
   443  		appProperty.ValueMap = make(map[string]interface{})
   444  		appProperty.ValueDeepMap = make(map[string]interface{})
   445  	} else if appProperty.ValueMap == nil {
   446  		appProperty.ValueMap = make(map[string]interface{})
   447  	} else if appProperty.ValueDeepMap == nil {
   448  		appProperty.ValueDeepMap = make(map[string]interface{})
   449  	}
   450  
   451  	yamlStr, err := isc.JsonToYaml(string(content))
   452  	if err != nil {
   453  		return
   454  	}
   455  	property, err := isc.YamlToProperties(yamlStr)
   456  	if err != nil {
   457  		return
   458  	}
   459  
   460  	AppendValue(property)
   461  }
   462  
   463  func AppendValue(propertiesNewValue string) {
   464  	pMap, err := isc.PropertiesToMap(propertiesNewValue)
   465  	for k, v := range pMap {
   466  		appProperty.ValueMap[k] = v
   467  	}
   468  
   469  	propertiesValueOfOriginal, err := isc.MapToProperties(appProperty.ValueMap)
   470  	if err != nil {
   471  		return
   472  	}
   473  
   474  	resultYaml, err := isc.PropertiesToYaml(propertiesValueOfOriginal)
   475  	if err != nil {
   476  		return
   477  	}
   478  	resultDeepMap, err := isc.YamlToMap(resultYaml)
   479  	if err != nil {
   480  		return
   481  	}
   482  	appProperty.ValueDeepMap = resultDeepMap
   483  }
   484  
   485  func SetValue(key string, value any) {
   486  	if nil == value {
   487  		return
   488  	}
   489  	if appProperty == nil {
   490  		appProperty = &ApplicationProperty{}
   491  		appProperty.ValueMap = make(map[string]interface{})
   492  		appProperty.ValueDeepMap = make(map[string]interface{})
   493  	} else if appProperty.ValueMap == nil {
   494  		appProperty.ValueMap = make(map[string]interface{})
   495  	} else if appProperty.ValueDeepMap == nil {
   496  		appProperty.ValueDeepMap = make(map[string]interface{})
   497  	}
   498  
   499  	if oldValue, exist := appProperty.ValueMap[key]; exist {
   500  		if !isc.IsBaseType(reflect.TypeOf(oldValue)) {
   501  			if reflect.TypeOf(oldValue) != reflect.TypeOf(value) {
   502  				return
   503  			}
   504  		}
   505  	}
   506  	propertiesValueOfOriginal, err := isc.MapToProperties(appProperty.ValueDeepMap)
   507  	if err != nil {
   508  		return
   509  	}
   510  	resultMap, err := isc.PropertiesToMap(propertiesValueOfOriginal)
   511  	if err != nil {
   512  		return
   513  	}
   514  
   515  	resultMap, err = parseProperties(key, value, resultMap)
   516  
   517  	appProperty.ValueMap = resultMap
   518  
   519  	mapProperties, err := isc.MapToProperties(resultMap)
   520  	if err != nil {
   521  		return
   522  	}
   523  	mapYaml, err := isc.PropertiesToYaml(mapProperties)
   524  	if err != nil {
   525  		return
   526  	}
   527  	resultDeepMap, err := isc.YamlToMap(mapYaml)
   528  	if err != nil {
   529  		return
   530  	}
   531  	appProperty.ValueDeepMap = resultDeepMap
   532  
   533  	// 发布配置变更事件
   534  	listener.PublishEvent(listener.ConfigChangeEvent{Key: key, Value: isc.ToString(isc.ObjectToData(value))})
   535  }
   536  
   537  func parseProperties(key string, value any, resultMap map[string]any) (map[string]any, error) {
   538  	if reflect.ValueOf(value).Kind() == reflect.Map || reflect.ValueOf(value).Kind() == reflect.Struct {
   539  		valueMap, err := isc.JsonToMap(isc.ObjectToJson(value))
   540  		if err != nil {
   541  			return resultMap, err
   542  		}
   543  		for k, v := range valueMap {
   544  			resultMap, err = parseProperties(key + "." + k, v, resultMap)
   545  		}
   546  	} else if reflect.ValueOf(value).Kind() == reflect.Slice || reflect.ValueOf(value).Kind() == reflect.Array {
   547  		values := []any{}
   548  		err := isc.DataToObject(isc.ObjectToJson(value), &values)
   549  		if err != nil {
   550  			return resultMap, err
   551  		}
   552  		for i, v := range values {
   553  			resultMap[key + "[" + isc.ToString(i) + "]"] = v
   554  			resultMap, err = parseProperties(key + "[" + isc.ToString(i) + "]", v, resultMap)
   555  		}
   556  	} else {
   557  		if reflect.ValueOf(value).Kind() == reflect.String && isc.ToString(value) != "" {
   558  			resultMap[key] = value
   559  		} else if value == nil{
   560  			resultMap[key] = value
   561  		}
   562  	}
   563  	return resultMap, nil
   564  }
   565  
   566  func GetValueString(key string) string {
   567  	if nil == appProperty {
   568  		return ""
   569  	}
   570  	if value, exist := appProperty.ValueMap[key]; exist {
   571  		return isc.ToString(value)
   572  	}
   573  	return ""
   574  }
   575  
   576  func GetValueInt(key string) int {
   577  	if nil == appProperty {
   578  		return 0
   579  	}
   580  	if value, exist := appProperty.ValueMap[key]; exist {
   581  		return isc.ToInt(value)
   582  	}
   583  	return 0
   584  }
   585  
   586  func GetValueInt8(key string) int8 {
   587  	if nil == appProperty {
   588  		return 0
   589  	}
   590  	if value, exist := appProperty.ValueMap[key]; exist {
   591  		return isc.ToInt8(value)
   592  	}
   593  	return 0
   594  }
   595  
   596  func GetValueInt16(key string) int16 {
   597  	if nil == appProperty {
   598  		return 0
   599  	}
   600  	if value, exist := appProperty.ValueMap[key]; exist {
   601  		return isc.ToInt16(value)
   602  	}
   603  	return 0
   604  }
   605  
   606  func GetValueInt32(key string) int32 {
   607  	if nil == appProperty {
   608  		return 0
   609  	}
   610  	if value, exist := appProperty.ValueMap[key]; exist {
   611  		return isc.ToInt32(value)
   612  	}
   613  	return 0
   614  }
   615  
   616  func GetValueInt64(key string) int64 {
   617  	if nil == appProperty {
   618  		return 0
   619  	}
   620  	if value, exist := appProperty.ValueMap[key]; exist {
   621  		return isc.ToInt64(value)
   622  	}
   623  	return 0
   624  }
   625  
   626  func GetValueUInt(key string) uint {
   627  	if nil == appProperty {
   628  		return 0
   629  	}
   630  	if value, exist := appProperty.ValueMap[key]; exist {
   631  		return isc.ToUInt(value)
   632  	}
   633  	return 0
   634  }
   635  
   636  func GetValueUInt8(key string) uint8 {
   637  	if nil == appProperty {
   638  		return 0
   639  	}
   640  	if value, exist := appProperty.ValueMap[key]; exist {
   641  		return isc.ToUInt8(value)
   642  	}
   643  	return 0
   644  }
   645  
   646  func GetValueUInt16(key string) uint16 {
   647  	if nil == appProperty {
   648  		return 0
   649  	}
   650  	if value, exist := appProperty.ValueMap[key]; exist {
   651  		return isc.ToUInt16(value)
   652  	}
   653  	return 0
   654  }
   655  
   656  func GetValueUInt32(key string) uint32 {
   657  	if nil == appProperty {
   658  		return 0
   659  	}
   660  	if value, exist := appProperty.ValueMap[key]; exist {
   661  		return isc.ToUInt32(value)
   662  	}
   663  	return 0
   664  }
   665  
   666  func GetValueUInt64(key string) uint64 {
   667  	if nil == appProperty {
   668  		return 0
   669  	}
   670  	if value, exist := appProperty.ValueMap[key]; exist {
   671  		return isc.ToUInt64(value)
   672  	}
   673  	return 0
   674  }
   675  
   676  func GetValueFloat32(key string) float32 {
   677  	if nil == appProperty {
   678  		return 0
   679  	}
   680  	if value, exist := appProperty.ValueMap[key]; exist {
   681  		return isc.ToFloat32(value)
   682  	}
   683  	return 0
   684  }
   685  
   686  func GetValueFloat64(key string) float64 {
   687  	if nil == appProperty {
   688  		return 0
   689  	}
   690  	if value, exist := appProperty.ValueMap[key]; exist {
   691  		return isc.ToFloat64(value)
   692  	}
   693  	return 0
   694  }
   695  
   696  func GetValueBool(key string) bool {
   697  	if nil == appProperty {
   698  		return false
   699  	}
   700  	if value, exist := appProperty.ValueMap[key]; exist {
   701  		return isc.ToBool(value)
   702  	}
   703  	return false
   704  }
   705  
   706  func GetValueStringDefault(key, defaultValue string) string {
   707  	if nil == appProperty {
   708  		return defaultValue
   709  	}
   710  	if value, exist := appProperty.ValueMap[key]; exist {
   711  		return isc.ToString(value)
   712  	}
   713  	return defaultValue
   714  }
   715  
   716  func GetValueIntDefault(key string, defaultValue int) int {
   717  	if nil == appProperty {
   718  		return defaultValue
   719  	}
   720  	if value, exist := appProperty.ValueMap[key]; exist {
   721  		return isc.ToInt(value)
   722  	}
   723  	return defaultValue
   724  }
   725  
   726  func GetValueInt8Default(key string, defaultValue int8) int8 {
   727  	if nil == appProperty {
   728  		return defaultValue
   729  	}
   730  	if value, exist := appProperty.ValueMap[key]; exist {
   731  		return isc.ToInt8(value)
   732  	}
   733  	return defaultValue
   734  }
   735  
   736  func GetValueInt16Default(key string, defaultValue int16) int16 {
   737  	if nil == appProperty {
   738  		return defaultValue
   739  	}
   740  	if value, exist := appProperty.ValueMap[key]; exist {
   741  		return isc.ToInt16(value)
   742  	}
   743  	return defaultValue
   744  }
   745  
   746  func GetValueInt32Default(key string, defaultValue int32) int32 {
   747  	if nil == appProperty {
   748  		return defaultValue
   749  	}
   750  	if value, exist := appProperty.ValueMap[key]; exist {
   751  		return isc.ToInt32(value)
   752  	}
   753  	return defaultValue
   754  }
   755  
   756  func GetValueInt64Default(key string, defaultValue int64) int64 {
   757  	if nil == appProperty {
   758  		return defaultValue
   759  	}
   760  	if value, exist := appProperty.ValueMap[key]; exist {
   761  		return isc.ToInt64(value)
   762  	}
   763  	return defaultValue
   764  }
   765  
   766  func GetValueUIntDefault(key string, defaultValue uint) uint {
   767  	if nil == appProperty {
   768  		return defaultValue
   769  	}
   770  	if value, exist := appProperty.ValueMap[key]; exist {
   771  		return isc.ToUInt(value)
   772  	}
   773  	return defaultValue
   774  }
   775  
   776  func GetValueUInt8Default(key string, defaultValue uint8) uint8 {
   777  	if nil == appProperty {
   778  		return defaultValue
   779  	}
   780  	if value, exist := appProperty.ValueMap[key]; exist {
   781  		return isc.ToUInt8(value)
   782  	}
   783  	return defaultValue
   784  }
   785  
   786  func GetValueUInt16Default(key string, defaultValue uint16) uint16 {
   787  	if nil == appProperty {
   788  		return defaultValue
   789  	}
   790  	if value, exist := appProperty.ValueMap[key]; exist {
   791  		return isc.ToUInt16(value)
   792  	}
   793  	return defaultValue
   794  }
   795  
   796  func GetValueUInt32Default(key string, defaultValue uint32) uint32 {
   797  	if nil == appProperty {
   798  		return defaultValue
   799  	}
   800  	if value, exist := appProperty.ValueMap[key]; exist {
   801  		return isc.ToUInt32(value)
   802  	}
   803  	return defaultValue
   804  }
   805  
   806  func GetValueUInt64Default(key string, defaultValue uint64) uint64 {
   807  	if nil == appProperty {
   808  		return defaultValue
   809  	}
   810  	if value, exist := appProperty.ValueMap[key]; exist {
   811  		return isc.ToUInt64(value)
   812  	}
   813  	return defaultValue
   814  }
   815  
   816  func GetValueFloat32Default(key string, defaultValue float32) float32 {
   817  	if nil == appProperty {
   818  		return defaultValue
   819  	}
   820  	if value, exist := appProperty.ValueMap[key]; exist {
   821  		return isc.ToFloat32(value)
   822  	}
   823  	return defaultValue
   824  }
   825  
   826  func GetValueFloat64Default(key string, defaultValue float64) float64 {
   827  	if nil == appProperty {
   828  		return defaultValue
   829  	}
   830  	if value, exist := appProperty.ValueMap[key]; exist {
   831  		return isc.ToFloat64(value)
   832  	}
   833  	return defaultValue
   834  }
   835  
   836  func GetValueBoolDefault(key string, defaultValue bool) bool {
   837  	if nil == appProperty {
   838  		return defaultValue
   839  	}
   840  	if value, exist := appProperty.ValueMap[key]; exist {
   841  		return isc.ToBool(value)
   842  	}
   843  	return defaultValue
   844  }
   845  
   846  func GetValueObject(key string, targetPtrObj any) error {
   847  	if nil == appProperty {
   848  		return nil
   849  	}
   850  	data := doGetValue(appProperty.ValueDeepMap, key)
   851  	err := isc.DataToObject(data, targetPtrObj)
   852  	if err != nil {
   853  		return err
   854  	}
   855  	return nil
   856  }
   857  
   858  func GetValueArray(key string) []any {
   859  	if nil == appProperty {
   860  		return nil
   861  	}
   862  
   863  	var arrayResult []any
   864  	data := doGetValue(appProperty.ValueDeepMap, key)
   865  	err := isc.DataToObject(data, &arrayResult)
   866  	if err != nil {
   867  		return arrayResult
   868  	}
   869  	return arrayResult
   870  }
   871  
   872  func GetValueArrayInt(key string) []int {
   873  	if nil == appProperty {
   874  		return nil
   875  	}
   876  
   877  	var arrayResult []int
   878  	data := doGetValue(appProperty.ValueDeepMap, key)
   879  	err := isc.DataToObject(data, &arrayResult)
   880  	if err != nil {
   881  		return arrayResult
   882  	}
   883  	return arrayResult
   884  }
   885  
   886  func GetValue(key string) any {
   887  	if nil == appProperty {
   888  		return nil
   889  	}
   890  	return doGetValue(appProperty.ValueDeepMap, key)
   891  }
   892  
   893  func doGetValue(parentValue any, key string) any {
   894  	if key == "" {
   895  		return parentValue
   896  	}
   897  	parentValueKind := reflect.ValueOf(parentValue).Kind()
   898  	if parentValueKind == reflect.Map {
   899  		keys := strings.SplitN(key, ".", 2)
   900  		v1 := reflect.ValueOf(parentValue).MapIndex(reflect.ValueOf(keys[0]))
   901  		emptyValue := reflect.Value{}
   902  		if v1 == emptyValue {
   903  			return nil
   904  		}
   905  		if len(keys) == 1 {
   906  			return doGetValue(v1.Interface(), "")
   907  		} else {
   908  			return doGetValue(v1.Interface(), fmt.Sprintf("%v", keys[1]))
   909  		}
   910  	}
   911  	return nil
   912  }
   913  
   914  type ApplicationProperty struct {
   915  	ValueMap     map[string]any
   916  	ValueDeepMap map[string]any
   917  }
   918  
   919  //LoadYamlConfig read fileName from private path fileName,eg:application.yml, and transform it to AConfig
   920  //note: AConfig must be a pointer
   921  func LoadYamlConfig(fileName string, AConfig any, handler func(data []byte, AConfig any) error) error {
   922  	pwd, _ := os.Getwd()
   923  	fp := filepath.Join(pwd, fileName)
   924  	return LoadYamlConfigByAbsolutPath(fp, AConfig, handler)
   925  }
   926  
   927  //LoadYamlConfigByAbsolutPath read fileName from absolute path fileName,eg:/home/isc-gobase/application.yml, and transform it to AConfig
   928  //note: AConfig must be a pointer
   929  func LoadYamlConfigByAbsolutPath(path string, AConfig any, handler func(data []byte, AConfig any) error) error {
   930  	data, err := os.ReadFile(path)
   931  	if err != nil {
   932  		log.Printf("读取文件异常(%v)", err)
   933  	}
   934  	return handler(data, AConfig)
   935  }
   936  
   937  //LoadSpringConfig read fileName from current dictionary and fileName is application.yml,eg:/home/isc-gobase/application.yml, and transform it to AConfig
   938  //note: AConfig must be a pointer
   939  //note: if it has Spring.Profiles.Active,eg: Spring.Profiles.Active=dev,will load config from /home/isc-gobase/application-dev.yml,and same key
   940  //will write in the last one.
   941  
   942  func LoadSpringConfig(AConfig any) {
   943  	_ = LoadYamlConfig("application.yml", AConfig, func(data []byte, AConfig any) error {
   944  		err := yaml.Unmarshal(data, AConfig)
   945  		if err != nil {
   946  			log.Printf("读取 application.yml 异常(%v)", err)
   947  			return err
   948  		}
   949  		v1 := reflect.ValueOf(AConfig).Elem()
   950  		o1 := v1.FieldByName("Spring").Interface()
   951  		v2 := reflect.ValueOf(o1)
   952  		o2 := v2.FieldByName("Profiles").Interface()
   953  		v3 := reflect.ValueOf(o2)
   954  		act := v3.FieldByName("Active").String()
   955  		if act != "" && act != "default" {
   956  			yamlAdditional, err := os.ReadFile(fmt.Sprintf("./application-%s.yml", act))
   957  			if err != nil {
   958  				log.Printf("读取 application-%s.yml 失败", act)
   959  				return err
   960  			} else {
   961  				err = yaml.Unmarshal(yamlAdditional, AConfig)
   962  				if err != nil {
   963  					log.Printf("读取 application-%s.yml 异常", act)
   964  					return err
   965  				}
   966  			}
   967  		}
   968  		return nil
   969  	})
   970  }