From a2fed1cb5b6c51b4f00cec12444a9927da818877 Mon Sep 17 00:00:00 2001 From: what Date: Mon, 6 May 2024 14:27:16 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E6=96=B0=E5=A2=9E=20OperationAccess.M?= =?UTF-8?q?erge=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- support/list.go | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index b15cdd7..96afcb7 100644 --- a/go.mod +++ b/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 diff --git a/support/list.go b/support/list.go index ed7f002..856fe3b 100644 --- a/support/list.go +++ b/support/list.go @@ -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) {