mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-08 05:29:51 +00:00
refactor: maps utils
This commit is contained in:
parent
94579d65c4
commit
150b666d4b
@ -3,6 +3,8 @@ package domain
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/usual2970/certimate/internal/pkg/utils/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ApplyConfig struct {
|
type ApplyConfig struct {
|
||||||
@ -29,7 +31,7 @@ type DeployConfig struct {
|
|||||||
// 出参:
|
// 出参:
|
||||||
// - 配置项的值。如果配置项不存在或者类型不是字符串,则返回空字符串。
|
// - 配置项的值。如果配置项不存在或者类型不是字符串,则返回空字符串。
|
||||||
func (dc *DeployConfig) GetConfigAsString(key string) string {
|
func (dc *DeployConfig) GetConfigAsString(key string) string {
|
||||||
return dc.GetConfigOrDefaultAsString(key, "")
|
return maps.GetValueAsString(dc.Config, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 以字符串形式获取配置项。
|
// 以字符串形式获取配置项。
|
||||||
@ -41,17 +43,7 @@ func (dc *DeployConfig) GetConfigAsString(key string) string {
|
|||||||
// 出参:
|
// 出参:
|
||||||
// - 配置项的值。如果配置项不存在或者类型不是字符串,则返回默认值。
|
// - 配置项的值。如果配置项不存在或者类型不是字符串,则返回默认值。
|
||||||
func (dc *DeployConfig) GetConfigOrDefaultAsString(key string, defaultValue string) string {
|
func (dc *DeployConfig) GetConfigOrDefaultAsString(key string, defaultValue string) string {
|
||||||
if dc.Config == nil {
|
return maps.GetValueOrDefaultAsString(dc.Config, key, defaultValue)
|
||||||
return defaultValue
|
|
||||||
}
|
|
||||||
|
|
||||||
if value, ok := dc.Config[key]; ok {
|
|
||||||
if result, ok := value.(string); ok {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return defaultValue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 以 32 位整数形式获取配置项。
|
// 以 32 位整数形式获取配置项。
|
||||||
@ -62,7 +54,7 @@ func (dc *DeployConfig) GetConfigOrDefaultAsString(key string, defaultValue stri
|
|||||||
// 出参:
|
// 出参:
|
||||||
// - 配置项的值。如果配置项不存在或者类型不是 32 位整数,则返回 0。
|
// - 配置项的值。如果配置项不存在或者类型不是 32 位整数,则返回 0。
|
||||||
func (dc *DeployConfig) GetConfigAsInt32(key string) int32 {
|
func (dc *DeployConfig) GetConfigAsInt32(key string) int32 {
|
||||||
return dc.GetConfigOrDefaultAsInt32(key, 0)
|
return maps.GetValueAsInt32(dc.Config, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 以 32 位整数形式获取配置项。
|
// 以 32 位整数形式获取配置项。
|
||||||
@ -74,17 +66,7 @@ func (dc *DeployConfig) GetConfigAsInt32(key string) int32 {
|
|||||||
// 出参:
|
// 出参:
|
||||||
// - 配置项的值。如果配置项不存在或者类型不是 32 位整数,则返回默认值。
|
// - 配置项的值。如果配置项不存在或者类型不是 32 位整数,则返回默认值。
|
||||||
func (dc *DeployConfig) GetConfigOrDefaultAsInt32(key string, defaultValue int32) int32 {
|
func (dc *DeployConfig) GetConfigOrDefaultAsInt32(key string, defaultValue int32) int32 {
|
||||||
if dc.Config == nil {
|
return maps.GetValueOrDefaultAsInt32(dc.Config, key, defaultValue)
|
||||||
return defaultValue
|
|
||||||
}
|
|
||||||
|
|
||||||
if value, ok := dc.Config[key]; ok {
|
|
||||||
if result, ok := value.(int32); ok {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return defaultValue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 以布尔形式获取配置项。
|
// 以布尔形式获取配置项。
|
||||||
@ -95,7 +77,7 @@ func (dc *DeployConfig) GetConfigOrDefaultAsInt32(key string, defaultValue int32
|
|||||||
// 出参:
|
// 出参:
|
||||||
// - 配置项的值。如果配置项不存在或者类型不是布尔,则返回 false。
|
// - 配置项的值。如果配置项不存在或者类型不是布尔,则返回 false。
|
||||||
func (dc *DeployConfig) GetConfigAsBool(key string) bool {
|
func (dc *DeployConfig) GetConfigAsBool(key string) bool {
|
||||||
return dc.GetConfigOrDefaultAsBool(key, false)
|
return maps.GetValueAsBool(dc.Config, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 以布尔形式获取配置项。
|
// 以布尔形式获取配置项。
|
||||||
@ -107,17 +89,7 @@ func (dc *DeployConfig) GetConfigAsBool(key string) bool {
|
|||||||
// 出参:
|
// 出参:
|
||||||
// - 配置项的值。如果配置项不存在或者类型不是布尔,则返回默认值。
|
// - 配置项的值。如果配置项不存在或者类型不是布尔,则返回默认值。
|
||||||
func (dc *DeployConfig) GetConfigOrDefaultAsBool(key string, defaultValue bool) bool {
|
func (dc *DeployConfig) GetConfigOrDefaultAsBool(key string, defaultValue bool) bool {
|
||||||
if dc.Config == nil {
|
return maps.GetValueOrDefaultAsBool(dc.Config, key, defaultValue)
|
||||||
return defaultValue
|
|
||||||
}
|
|
||||||
|
|
||||||
if value, ok := dc.Config[key]; ok {
|
|
||||||
if result, ok := value.(bool); ok {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return defaultValue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 以变量字典形式获取配置项。
|
// 以变量字典形式获取配置项。
|
||||||
|
@ -3,7 +3,6 @@ package notify
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
stdhttp "net/http"
|
stdhttp "net/http"
|
||||||
|
|
||||||
@ -74,7 +73,7 @@ func getNotifiers() ([]notifyPackage.Notifier, error) {
|
|||||||
|
|
||||||
for k, v := range rs {
|
for k, v := range rs {
|
||||||
|
|
||||||
if !getBool(v, "enabled") {
|
if !getConfigAsBool(v, "enabled") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,25 +118,18 @@ func getNotifier(channel string, conf map[string]any) (notifyPackage.Notifier, e
|
|||||||
func getWebhookNotifier(conf map[string]any) notifyPackage.Notifier {
|
func getWebhookNotifier(conf map[string]any) notifyPackage.Notifier {
|
||||||
rs := http.New()
|
rs := http.New()
|
||||||
|
|
||||||
rs.AddReceiversURLs(getString(conf, "url"))
|
rs.AddReceiversURLs(getConfigAsString(conf, "url"))
|
||||||
|
|
||||||
return rs
|
return rs
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTelegramNotifier(conf map[string]any) notifyPackage.Notifier {
|
func getTelegramNotifier(conf map[string]any) notifyPackage.Notifier {
|
||||||
rs, err := telegram.New(getString(conf, "apiToken"))
|
rs, err := telegram.New(getConfigAsString(conf, "apiToken"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
chatId := getString(conf, "chatId")
|
rs.AddReceivers(getConfigAsInt64(conf, "chatId"))
|
||||||
|
|
||||||
id, err := strconv.ParseInt(chatId, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
rs.AddReceivers(id)
|
|
||||||
return rs
|
return rs
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,7 +137,7 @@ func getServerChanNotifier(conf map[string]any) notifyPackage.Notifier {
|
|||||||
rs := http.New()
|
rs := http.New()
|
||||||
|
|
||||||
rs.AddReceivers(&http.Webhook{
|
rs.AddReceivers(&http.Webhook{
|
||||||
URL: getString(conf, "url"),
|
URL: getConfigAsString(conf, "url"),
|
||||||
Header: stdhttp.Header{},
|
Header: stdhttp.Header{},
|
||||||
ContentType: "application/json",
|
ContentType: "application/json",
|
||||||
Method: stdhttp.MethodPost,
|
Method: stdhttp.MethodPost,
|
||||||
@ -161,8 +153,8 @@ func getServerChanNotifier(conf map[string]any) notifyPackage.Notifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getBarkNotifier(conf map[string]any) notifyPackage.Notifier {
|
func getBarkNotifier(conf map[string]any) notifyPackage.Notifier {
|
||||||
deviceKey := getString(conf, "deviceKey")
|
deviceKey := getConfigAsString(conf, "deviceKey")
|
||||||
serverURL := getString(conf, "serverUrl")
|
serverURL := getConfigAsString(conf, "serverUrl")
|
||||||
if serverURL == "" {
|
if serverURL == "" {
|
||||||
return bark.New(deviceKey)
|
return bark.New(deviceKey)
|
||||||
}
|
}
|
||||||
@ -171,21 +163,21 @@ func getBarkNotifier(conf map[string]any) notifyPackage.Notifier {
|
|||||||
|
|
||||||
func getDingTalkNotifier(conf map[string]any) notifyPackage.Notifier {
|
func getDingTalkNotifier(conf map[string]any) notifyPackage.Notifier {
|
||||||
return dingding.New(&dingding.Config{
|
return dingding.New(&dingding.Config{
|
||||||
Token: getString(conf, "accessToken"),
|
Token: getConfigAsString(conf, "accessToken"),
|
||||||
Secret: getString(conf, "secret"),
|
Secret: getConfigAsString(conf, "secret"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func getLarkNotifier(conf map[string]any) notifyPackage.Notifier {
|
func getLarkNotifier(conf map[string]any) notifyPackage.Notifier {
|
||||||
return lark.NewWebhookService(getString(conf, "webhookUrl"))
|
return lark.NewWebhookService(getConfigAsString(conf, "webhookUrl"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMailNotifier(conf map[string]any) (notifyPackage.Notifier, error) {
|
func getMailNotifier(conf map[string]any) (notifyPackage.Notifier, error) {
|
||||||
rs, err := NewMail(getString(conf, "senderAddress"),
|
rs, err := NewMail(getConfigAsString(conf, "senderAddress"),
|
||||||
getString(conf, "receiverAddresses"),
|
getConfigAsString(conf, "receiverAddresses"),
|
||||||
getString(conf, "smtpHostAddr"),
|
getConfigAsString(conf, "smtpHostAddr"),
|
||||||
getString(conf, "smtpHostPort"),
|
getConfigAsString(conf, "smtpHostPort"),
|
||||||
getString(conf, "password"),
|
getConfigAsString(conf, "password"),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -1,17 +1,21 @@
|
|||||||
package notify
|
package notify
|
||||||
|
|
||||||
func getString(conf map[string]any, key string) string {
|
import (
|
||||||
if _, ok := conf[key]; !ok {
|
"github.com/usual2970/certimate/internal/pkg/utils/maps"
|
||||||
return ""
|
)
|
||||||
}
|
|
||||||
|
|
||||||
return conf[key].(string)
|
func getConfigAsString(conf map[string]any, key string) string {
|
||||||
|
return maps.GetValueAsString(conf, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBool(conf map[string]any, key string) bool {
|
func getConfigAsInt32(conf map[string]any, key string) int32 {
|
||||||
if _, ok := conf[key]; !ok {
|
return maps.GetValueAsInt32(conf, key)
|
||||||
return false
|
}
|
||||||
}
|
|
||||||
|
func getConfigAsInt64(conf map[string]any, key string) int64 {
|
||||||
return conf[key].(bool)
|
return maps.GetValueAsInt64(conf, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getConfigAsBool(conf map[string]any, key string) bool {
|
||||||
|
return maps.GetValueAsBool(conf, key)
|
||||||
}
|
}
|
||||||
|
166
internal/pkg/utils/maps/maps.go
Normal file
166
internal/pkg/utils/maps/maps.go
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
package maps
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
// 以字符串形式从字典中获取指定键的值。
|
||||||
|
//
|
||||||
|
// 入参:
|
||||||
|
// - dict: 字典。
|
||||||
|
// - key: 键。
|
||||||
|
//
|
||||||
|
// 出参:
|
||||||
|
// - 字典中键对应的值。如果指定键不存在或者类型不是字符串,则返回空字符串。
|
||||||
|
func GetValueAsString(dict map[string]any, key string) string {
|
||||||
|
return GetValueOrDefaultAsString(dict, key, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以字符串形式从字典中获取指定键的值。
|
||||||
|
//
|
||||||
|
// 入参:
|
||||||
|
// - dict: 字典。
|
||||||
|
// - key: 键。
|
||||||
|
// - defaultValue: 默认值。
|
||||||
|
//
|
||||||
|
// 出参:
|
||||||
|
// - 字典中键对应的值。如果指定键不存在或者类型不是字符串,则返回默认值。
|
||||||
|
func GetValueOrDefaultAsString(dict map[string]any, key string, defaultValue string) string {
|
||||||
|
if dict == nil {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
if value, ok := dict[key]; ok {
|
||||||
|
if result, ok := value.(string); ok {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 32 位整数形式从字典中获取指定键的值。
|
||||||
|
//
|
||||||
|
// 入参:
|
||||||
|
// - dict: 字典。
|
||||||
|
// - key: 键。
|
||||||
|
//
|
||||||
|
// 出参:
|
||||||
|
// - 字典中键对应的值。如果指定键不存在或者类型不是 32 位整数,则返回 0。
|
||||||
|
func GetValueAsInt32(dict map[string]any, key string) int32 {
|
||||||
|
return GetValueOrDefaultAsInt32(dict, key, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 32 位整数形式从字典中获取指定键的值。
|
||||||
|
//
|
||||||
|
// 入参:
|
||||||
|
// - dict: 字典。
|
||||||
|
// - key: 键。
|
||||||
|
// - defaultValue: 默认值。
|
||||||
|
//
|
||||||
|
// 出参:
|
||||||
|
// - 字典中键对应的值。如果指定键不存在或者类型不是 32 位整数,则返回默认值。
|
||||||
|
func GetValueOrDefaultAsInt32(dict map[string]any, key string, defaultValue int32) int32 {
|
||||||
|
if dict == nil {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
if value, ok := dict[key]; ok {
|
||||||
|
if result, ok := value.(int32); ok {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// 兼容字符串类型的值
|
||||||
|
if s, ok := value.(string); ok {
|
||||||
|
if result, err := strconv.ParseInt(s, 10, 32); err == nil {
|
||||||
|
return int32(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 64 位整数形式从字典中获取指定键的值。
|
||||||
|
//
|
||||||
|
// 入参:
|
||||||
|
// - dict: 字典。
|
||||||
|
// - key: 键。
|
||||||
|
//
|
||||||
|
// 出参:
|
||||||
|
// - 字典中键对应的值。如果指定键不存在或者类型不是 64 位整数,则返回 0。
|
||||||
|
func GetValueAsInt64(dict map[string]any, key string) int64 {
|
||||||
|
return GetValueOrDefaultAsInt64(dict, key, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以 64 位整数形式从字典中获取指定键的值。
|
||||||
|
//
|
||||||
|
// 入参:
|
||||||
|
// - dict: 字典。
|
||||||
|
// - key: 键。
|
||||||
|
// - defaultValue: 默认值。
|
||||||
|
//
|
||||||
|
// 出参:
|
||||||
|
// - 字典中键对应的值。如果指定键不存在或者类型不是 64 位整数,则返回默认值。
|
||||||
|
func GetValueOrDefaultAsInt64(dict map[string]any, key string, defaultValue int64) int64 {
|
||||||
|
if dict == nil {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
if value, ok := dict[key]; ok {
|
||||||
|
if result, ok := value.(int64); ok {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// 兼容字符串类型的值
|
||||||
|
if s, ok := value.(string); ok {
|
||||||
|
if result, err := strconv.ParseInt(s, 10, 64); err == nil {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以布尔形式从字典中获取指定键的值。
|
||||||
|
//
|
||||||
|
// 入参:
|
||||||
|
// - dict: 字典。
|
||||||
|
// - key: 键。
|
||||||
|
//
|
||||||
|
// 出参:
|
||||||
|
// - 字典中键对应的值。如果指定键不存在或者类型不是布尔,则返回 false。
|
||||||
|
func GetValueAsBool(dict map[string]any, key string) bool {
|
||||||
|
return GetValueOrDefaultAsBool(dict, key, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 以布尔形式从字典中获取指定键的值。
|
||||||
|
//
|
||||||
|
// 入参:
|
||||||
|
// - dict: 字典。
|
||||||
|
// - key: 键。
|
||||||
|
// - defaultValue: 默认值。
|
||||||
|
//
|
||||||
|
// 出参:
|
||||||
|
// - 字典中键对应的值。如果指定键不存在或者类型不是布尔,则返回默认值。
|
||||||
|
func GetValueOrDefaultAsBool(dict map[string]any, key string, defaultValue bool) bool {
|
||||||
|
if dict == nil {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
if value, ok := dict[key]; ok {
|
||||||
|
if result, ok := value.(bool); ok {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// 兼容字符串类型的值
|
||||||
|
if str, ok := value.(string); ok {
|
||||||
|
if str == "true" || str == "True" || str == "1" {
|
||||||
|
return true
|
||||||
|
} else if str == "false" || str == "False" || str == "0" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultValue
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user