[feat] 新增 OperationAccess.Merge 方法

This commit is contained in:
what 2024-05-06 14:27:16 +08:00
parent 553f327ae1
commit a2fed1cb5b
2 changed files with 11 additions and 9 deletions

2
go.mod
View File

@ -1,6 +1,6 @@
module git.fsdpf.net/go/contracts module git.fsdpf.net/go/contracts
go 1.18 go 1.21
require ( require (
git.fsdpf.net/go/db v0.0.0-20230621051209-5740d112407f git.fsdpf.net/go/db v0.0.0-20230621051209-5740d112407f

View File

@ -5,6 +5,7 @@ import (
"reflect" "reflect"
"git.fsdpf.net/go/contracts" "git.fsdpf.net/go/contracts"
"github.com/samber/lo"
"github.com/spf13/cast" "github.com/spf13/cast"
) )
@ -36,7 +37,7 @@ func (this *OperationAccess) Push(k string, input ...any) error {
for i := 0; i < len(input); i++ { for i := 0; i < len(input); i++ {
if v, err := this.Convert(input[i]); err != nil { if v, err := this.Convert(input[i]); err != nil {
return err return err
} else { } else if !lo.Contains(this.data[k], v) {
this.data[k] = append(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 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) { func (this OperationAccess) Convert(v any) (any, error) {
if this.typ == reflect.Int { if this.typ == reflect.Int {
return cast.ToIntE(v) return cast.ToIntE(v)
@ -64,13 +72,7 @@ func (this OperationAccess) Exist(k string, v any) bool {
return false return false
} }
for i := 0; i < len(items); i++ { return lo.Contains(items, flag)
if items[i] == flag {
return true
}
}
return false
} }
func (this OperationAccess) MarshalJSON() ([]byte, error) { func (this OperationAccess) MarshalJSON() ([]byte, error) {