diff --git a/pkg/lakego-pkg/go-cryptobin/tool/config.go b/pkg/lakego-pkg/go-cryptobin/tool/config.go index 4bb696c4..4b57124e 100644 --- a/pkg/lakego-pkg/go-cryptobin/tool/config.go +++ b/pkg/lakego-pkg/go-cryptobin/tool/config.go @@ -2,6 +2,7 @@ package tool import ( "time" + "sync" "encoding/json" ) @@ -18,11 +19,18 @@ func NewConfig() *Config { * @author deatil */ type Config struct { + // 锁定 + mu sync.RWMutex + + // 数据 data map[string]any } // 设置 func (this *Config) WithData(data map[string]any) *Config { + this.mu.Lock() + defer this.mu.Unlock() + this.data = data return this @@ -30,12 +38,28 @@ func (this *Config) WithData(data map[string]any) *Config { // 设置 func (this *Config) Set(name string, data any) *Config { + this.mu.Lock() + defer this.mu.Unlock() + this.data[name] = data return this } +// 删除 +func (this *Config) Remove(name string) *Config { + this.mu.Lock() + defer this.mu.Unlock() + + delete(this.data, name) + + return this +} + func (this *Config) Has(name string) bool { + this.mu.RLock() + defer this.mu.RUnlock() + if _, ok := this.data[name]; ok { return true } @@ -44,6 +68,9 @@ func (this *Config) Has(name string) bool { } func (this *Config) Get(name string) any { + this.mu.RLock() + defer this.mu.RUnlock() + if data, ok := this.data[name]; ok { return data } @@ -51,6 +78,38 @@ func (this *Config) Get(name string) any { return nil } +func (this *Config) All() map[string]any { + return this.data +} + +func (this *Config) Names() []string { + names := make([]string, 0) + for name, _ := range this.data { + names = append(names, name) + } + + return names +} + +func (this *Config) Len() int { + return len(this.data) +} + +func (this *Config) Clean() { + this.mu.Lock() + defer this.mu.Unlock() + + for name, _ := range this.data { + delete(this.data, name) + } +} + +func (this *Config) String() string { + data, _ := json.Marshal(this.data) + + return string(data) +} + func (this *Config) GetString(name string) string { if data, ok := this.Get(name).(string); ok { return data @@ -274,17 +333,3 @@ func (this *Config) GetDurationSlice(name string) []time.Duration { return make([]time.Duration, 0) } - -func (this *Config) All() map[string]any { - return this.data -} - -func (this *Config) String() string { - data, _ := json.Marshal(this.data) - - return string(data) -} - -func (this *Config) Reset() { - this.data = make(map[string]any) -} diff --git a/pkg/lakego-pkg/go-datebin/datebin/datebin.go b/pkg/lakego-pkg/go-datebin/datebin/datebin.go index 50b2a17d..02a3134a 100644 --- a/pkg/lakego-pkg/go-datebin/datebin/datebin.go +++ b/pkg/lakego-pkg/go-datebin/datebin/datebin.go @@ -159,9 +159,9 @@ func (this Datebin) GetLocationString() string { // 设置时区 func (this Datebin) WithTimezone(timezone string) Datebin { - location, err := this.GetLocationByTimezone(timezone) + loc, err := this.GetLocationByTimezone(timezone) if err == nil { - this.loc = location + this.loc = loc } return this diff --git a/pkg/lakego-pkg/go-datebin/datebin/get.go b/pkg/lakego-pkg/go-datebin/datebin/get.go index 97c2a907..873e1230 100644 --- a/pkg/lakego-pkg/go-datebin/datebin/get.go +++ b/pkg/lakego-pkg/go-datebin/datebin/get.go @@ -7,12 +7,7 @@ import ( // 当前 func (this Datebin) Now(timezone ...string) Datebin { if len(timezone) > 0 { - loc, err := this.GetLocationByTimezone(timezone[0]) - if err == nil { - this.loc = loc - } - - this.AppendError(err) + this = this.WithTimezone(timezone[0]) } this.time = time.Now().In(this.loc) @@ -22,12 +17,7 @@ func (this Datebin) Now(timezone ...string) Datebin { // 今天 func (this Datebin) Today(timezone ...string) Datebin { if len(timezone) > 0 { - loc, err := this.GetLocationByTimezone(timezone[0]) - if err == nil { - this.loc = loc - } - - this.AppendError(err) + this = this.WithTimezone(timezone[0]) } var datetime Datebin @@ -45,12 +35,7 @@ func (this Datebin) Today(timezone ...string) Datebin { // 明天 func (this Datebin) Tomorrow(timezone ...string) Datebin { if len(timezone) > 0 { - loc, err := this.GetLocationByTimezone(timezone[0]) - if err == nil { - this.loc = loc - } - - this.AppendError(err) + this = this.WithTimezone(timezone[0]) } var datetime Datebin @@ -68,12 +53,7 @@ func (this Datebin) Tomorrow(timezone ...string) Datebin { // 昨天 func (this Datebin) Yesterday(timezone ...string) Datebin { if len(timezone) > 0 { - loc, err := this.GetLocationByTimezone(timezone[0]) - if err == nil { - this.loc = loc - } - - this.AppendError(err) + this = this.WithTimezone(timezone[0]) } var datetime Datebin diff --git a/pkg/lakego-pkg/go-datebin/datebin/offset.go b/pkg/lakego-pkg/go-datebin/datebin/offset.go index 9489dc2f..fe13a94a 100644 --- a/pkg/lakego-pkg/go-datebin/datebin/offset.go +++ b/pkg/lakego-pkg/go-datebin/datebin/offset.go @@ -8,12 +8,7 @@ import ( // 间隔 func (this Datebin) Offset(field string, offset int, timezone ...string) Datebin { if len(timezone) > 0 { - loc, error := this.GetLocationByTimezone(timezone[0]) - if error == nil { - this.loc = loc - } - - this.AppendError(error) + this = this.WithTimezone(timezone[0]) } // 设置时区 @@ -104,10 +99,10 @@ func (this Datebin) OffsetMonthsNoOverflow(months int) Datebin { // 按照持续时长字符串增加时间 func (this Datebin) AddDuration(duration string) Datebin { td, err := this.ParseDuration(duration) + this.time = this.time.In(this.loc).Add(td) - this.AppendError(err) - return this + return this.AppendError(err) } // 按照持续时长字符串减少时间 diff --git a/pkg/lakego-pkg/go-datebin/datebin/parse.go b/pkg/lakego-pkg/go-datebin/datebin/parse.go index 3526457b..b4b861be 100644 --- a/pkg/lakego-pkg/go-datebin/datebin/parse.go +++ b/pkg/lakego-pkg/go-datebin/datebin/parse.go @@ -43,7 +43,9 @@ func (this Datebin) Parse(date string) Datebin { } time, err := time.Parse(layout, date) - this.AppendError(err) + if err != nil { + return this.AppendError(err) + } this.time = time @@ -53,17 +55,14 @@ func (this Datebin) Parse(date string) Datebin { // 用布局字符解析时间字符 func (this Datebin) ParseWithLayout(date string, layout string, timezone ...string) Datebin { if len(timezone) > 0 { - loc, err := this.GetLocationByTimezone(timezone[0]) - if err == nil { - this.loc = loc - } - - this.AppendError(err) + this = this.WithTimezone(timezone[0]) } time, err := time.ParseInLocation(layout, date, this.loc) - this.AppendError(err) - + if err != nil { + return this.AppendError(err) + } + this.time = time return this diff --git a/pkg/lakego-pkg/go-datebin/datebin/to.go b/pkg/lakego-pkg/go-datebin/datebin/to.go index 2c42a7a3..6090d1b1 100644 --- a/pkg/lakego-pkg/go-datebin/datebin/to.go +++ b/pkg/lakego-pkg/go-datebin/datebin/to.go @@ -14,12 +14,7 @@ func (this Datebin) String() string { // 返回字符 func (this Datebin) ToString(timezone ...string) string { if len(timezone) > 0 { - loc, error := this.GetLocationByTimezone(timezone[0]) - if error == nil { - this.loc = loc - } - - this.AppendError(error) + this = this.WithTimezone(timezone[0]) } if this.IsInvalid() { @@ -32,12 +27,7 @@ func (this Datebin) ToString(timezone ...string) string { // 返回星座名称 func (this Datebin) ToStarString(timezone ...string) string { if len(timezone) > 0 { - loc, error := this.GetLocationByTimezone(timezone[0]) - if error == nil { - this.loc = loc - } - - this.AppendError(error) + this = this.WithTimezone(timezone[0]) } if this.IsInvalid() { @@ -95,12 +85,7 @@ func (this Datebin) ToStarString(timezone ...string) string { // 返回当前季节,以气象划分 func (this Datebin) ToSeasonString(timezone ...string) string { if len(timezone) > 0 { - loc, error := this.GetLocationByTimezone(timezone[0]) - if error == nil { - this.loc = loc - } - - this.AppendError(error) + this = this.WithTimezone(timezone[0]) } if this.IsInvalid() { @@ -132,12 +117,7 @@ func (this Datebin) ToSeasonString(timezone ...string) string { // 周几 func (this Datebin) ToWeekdayString(timezone ...string) string { if len(timezone) > 0 { - loc, error := this.GetLocationByTimezone(timezone[0]) - if error == nil { - this.loc = loc - } - - this.AppendError(error) + this = this.WithTimezone(timezone[0]) } if this.IsInvalid() { @@ -152,12 +132,7 @@ func (this Datebin) ToWeekdayString(timezone ...string) string { // 原始格式 func (this Datebin) Layout(layout string, timezone ...string) string { if len(timezone) > 0 { - loc, error := this.GetLocationByTimezone(timezone[0]) - if error == nil { - this.loc = loc - } - - this.AppendError(error) + this = this.WithTimezone(timezone[0]) } if this.IsInvalid() { @@ -175,10 +150,7 @@ func (this Datebin) ToLayoutString(layout string, timezone ...string) string { // 输出指定布局的时间字符串 func (this Datebin) Format(layout string, timezone ...string) string { if len(timezone) > 0 { - loc, error := this.GetLocationByTimezone(timezone[0]) - - this.loc = loc - this.AppendError(error) + this = this.WithTimezone(timezone[0]) } if this.IsInvalid() {