1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
option.go 3.17 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2024-08-12 20:05 . sql & from
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 (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 WithOpt[T any](v T, desc string, t int) Option {
return &Opt[T]{v: v, d: desc, t: t}
}
func Select[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, _ := Select[int](TypeMaxColumn, 6, opts...)
return r
}
func WithFullSQL() Option {
return WithOpt(true, "full:sql", TypeFullSql)
}
func FullSqlExist(opts ...Option) bool {
r, _ := Select[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 Select[string](TypeSubQuery, "", opts...)
}
func WithNameKey(name string) Option {
return WithOpt(name, "db.name.key", TypeNameKey)
}
func NameKeyExist(opts ...Option) (string, bool) {
return Select[string](TypeNameKey, "", opts...)
}
func WithTimeOutKey(timeout int64) Option {
return WithOpt(timeout, "db.timeout.key", TypeTimeOut)
}
func TimeOutExist(opts ...Option) (int64, bool) {
return Select[int64](TypeTimeOut, 3000, opts...)
}
func WithAlarmKey() Option {
return WithOpt(true, "db.alarm.key", TypeAlarm)
}
func AlarmExist(opts ...Option) bool {
r, _ := Select(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.123

搜索帮助