1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
option.go 3.18 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-11-30 16:46 . BUG
package option
const (
TypeMaxColumn = iota
TypeFullSql
TypeSqlColumnConvert
TypeSubQuery
TypeQuery
TypeNameKey //子查询
TypeTimeOut
TypeAlarm
TypeSqlDns = 1000
TypeSqlTls = 1001
TypeRedisTls = 1002
TypeSqlServerPubKey = 1003
)
type Option interface {
String() string
Type() int
Value() interface{}
}
func Filter(filter func(opt Option) bool, opts ...Option) {
for i := range opts {
if filter(opts[i]) {
return
}
}
}
func Exist(ty int, opts ...Option) (Option, bool) {
for i := range opts {
if opts[i].Type() == ty {
return opts[i], true
}
}
return nil, false
}
type Opt[T any] struct {
v T
t int
d string
}
func WithOpt[T any](v T, desc string, t int) Option {
return &Opt[T]{v: v, d: desc, t: t}
}
func (o Opt[T]) String() string {
return o.d
}
func (o Opt[T]) Type() int { return o.t }
func (o Opt[T]) Value() interface{} { return o.v }
func OptExist[T any](t int, defValue T, opts ...Option) (T, bool) {
if r, ok := Exist(t, opts...); ok {
return r.Value().(T), true
}
return defValue, false
}
type Query struct {
Q map[string]interface{}
}
func WithQuery() Option {
return &Query{Q: map[string]interface{}{}}
}
func (t Query) String() string {
return "query"
}
func (t Query) Type() int { return TypeQuery }
func (t Query) Value() interface{} { return t.Q }
func QueryExist(opts ...Option) *Query {
if r, ok := Exist(TypeQuery, opts...); ok {
return r.(*Query)
}
return nil
}
type QueryFunc func(q *Query)
func (f QueryFunc) Apply(q *Query) {
f(q)
}
func WithMaxColumn(c int) Option {
return WithOpt(c, "max:column", TypeMaxColumn)
}
func MaxColumnExist(opts ...Option) int {
r, _ := OptExist[int](TypeMaxColumn, 6, opts...)
return r
}
func WithFullSQL() Option {
return WithOpt(true, "full:sql", TypeFullSql)
}
func FullSqlExist(opts ...Option) bool {
r, _ := OptExist[bool](TypeFullSql, false, opts...)
return r
}
func SqlColumnConvert(f func(col string) string) Option {
return ColumnConvertFunc(f)
}
type ColumnConvertFunc func(col string) string
func (t ColumnConvertFunc) String() string {
return "sql:column:convert"
}
func (t ColumnConvertFunc) Type() int { return TypeSqlColumnConvert }
func (t ColumnConvertFunc) Value() interface{} { return t }
func SqlColumnConvertExist(opts ...Option) ColumnConvertFunc {
if r, ok := Exist(TypeSqlColumnConvert, opts...); ok {
return r.Value().(ColumnConvertFunc)
}
return nil
}
func WithSubQuerySQL(as string) Option {
return WithOpt(as, "subquery:sql", TypeSubQuery)
}
func SubQueryExist(opts ...Option) (string, bool) {
return OptExist[string](TypeSubQuery, "", opts...)
}
func WithNameKey(name string) Option {
return WithOpt(name, "db.name.key", TypeNameKey)
}
func NameKeyExist(opts ...Option) (string, bool) {
return OptExist[string](TypeNameKey, "", opts...)
}
func WithTimeOutKey(timeout int64) Option {
return WithOpt(timeout, "db.timeout.key", TypeTimeOut)
}
func TimeOutExist(opts ...Option) (int64, bool) {
return OptExist[int64](TypeTimeOut, 3000, opts...)
}
func WithAlarmKey() Option {
return WithOpt(true, "db.alarm.key", TypeAlarm)
}
func AlarmExist(opts ...Option) bool {
r, _ := OptExist(TypeAlarm, false, opts...)
return r
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.78

搜索帮助