[feat] 新增 OperationAccess.Merge 方法
This commit is contained in:
parent
553f327ae1
commit
a2fed1cb5b
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
||||
module git.fsdpf.net/go/contracts
|
||||
|
||||
go 1.18
|
||||
go 1.21
|
||||
|
||||
require (
|
||||
git.fsdpf.net/go/db v0.0.0-20230621051209-5740d112407f
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"reflect"
|
||||
|
||||
"git.fsdpf.net/go/contracts"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
@ -36,7 +37,7 @@ func (this *OperationAccess) Push(k string, input ...any) error {
|
||||
for i := 0; i < len(input); i++ {
|
||||
if v, err := this.Convert(input[i]); err != nil {
|
||||
return err
|
||||
} else {
|
||||
} else if !lo.Contains(this.data[k], v) {
|
||||
this.data[k] = append(this.data[k], v)
|
||||
}
|
||||
}
|
||||
@ -44,6 +45,13 @@ func (this *OperationAccess) Push(k string, input ...any) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this OperationAccess) Merge(item OperationAccess) error {
|
||||
for k, v := range item.data {
|
||||
this.Push(k, v...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this OperationAccess) Convert(v any) (any, error) {
|
||||
if this.typ == reflect.Int {
|
||||
return cast.ToIntE(v)
|
||||
@ -64,13 +72,7 @@ func (this OperationAccess) Exist(k string, v any) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := 0; i < len(items); i++ {
|
||||
if items[i] == flag {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
return lo.Contains(items, flag)
|
||||
}
|
||||
|
||||
func (this OperationAccess) MarshalJSON() ([]byte, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user