From 7fbb5e6db0a09031ad965c2f988146a7375e49ff Mon Sep 17 00:00:00 2001 From: ivy1996-encode <15055461510@163.com> Date: Mon, 15 Mar 2021 21:19:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=B8=AA=E5=A5=BD?= =?UTF-8?q?=E5=8F=8B=E5=8C=B9=E9=85=8D=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot_test.go | 2 +- relations.go | 42 +++++++++++++++++++++++------------------- user.go | 16 +++++++++------- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/bot_test.go b/bot_test.go index dc85854..d6a543f 100644 --- a/bot_test.go +++ b/bot_test.go @@ -23,7 +23,7 @@ func TestDefaultBot(t *testing.T) { group, _ := self.Groups() friends, _ := self.Friends() fmt.Println(group.Search(Cond{"NickName": "厉害了"})) - fmt.Println(friends.Search(Cond{"RemarkName": "阿青"})) + fmt.Println(friends.Search(Cond{"RemarkName": "阿青", "Sex": 2})) fmt.Println(bot.Block()) } diff --git a/relations.go b/relations.go index 073bbfd..ee2a016 100644 --- a/relations.go +++ b/relations.go @@ -72,7 +72,7 @@ func (f Friends) Search(cond Cond) (friends Friends, found bool) { switch k { case "UserName": if value, ok := v.(string); ok { - return f.SearchByNickName(value) + return f.SearchByUserName(value) } case "NickName": if value, ok := v.(string); ok { @@ -85,19 +85,21 @@ func (f Friends) Search(cond Cond) (friends Friends, found bool) { } } } - for _, member := range f { - value := reflect.ValueOf(member).Elem() + for _, friend := range f { + value := reflect.ValueOf(friend).Elem() + var matchCount int for k, v := range cond { if field := value.FieldByName(k); field.IsValid() { - if field.Interface() == v { - found = true - if friends == nil { - friends = make(Friends, 0) - } - friends = append(friends, member) + if field.Interface() != v { + break } + matchCount++ } } + if matchCount == len(cond) { + found = true + friends = append(friends, friend) + } } return } @@ -184,14 +186,14 @@ func (g Groups) SearchByRemarkName(remarkName string) (results Groups, found boo return } -func (g Groups) Search(cond Cond) (groups Groups, found bool) { +func (g Groups) Search(cond Cond) (results Groups, found bool) { if len(cond) == 1 { for k, v := range cond { switch k { case "UserName": if value, ok := v.(string); ok { - return g.SearchByNickName(value) + return g.SearchByUserName(value) } case "NickName": if value, ok := v.(string); ok { @@ -205,19 +207,21 @@ func (g Groups) Search(cond Cond) (groups Groups, found bool) { } } - for _, member := range g { - value := reflect.ValueOf(member).Elem() + for _, group := range g { + value := reflect.ValueOf(group).Elem() + var matchCount int for k, v := range cond { if field := value.FieldByName(k); field.IsValid() { - if field.Interface() == v { - found = true - if groups == nil { - groups = make(Groups, 0) - } - groups = append(groups, member) + if field.Interface() != v { + break } + matchCount++ } } + if matchCount == len(cond) { + found = true + results = append(results, group) + } } return } diff --git a/user.go b/user.go index e4ab05c..1a07bb3 100644 --- a/user.go +++ b/user.go @@ -343,7 +343,7 @@ func (m Members) Search(cond Cond) (results Members, found bool) { switch k { case "UserName": if value, ok := v.(string); ok { - return m.SearchByNickName(value) + return m.SearchByUserName(value) } case "NickName": if value, ok := v.(string); ok { @@ -359,17 +359,19 @@ func (m Members) Search(cond Cond) (results Members, found bool) { for _, member := range m { value := reflect.ValueOf(member).Elem() + var matchCount int for k, v := range cond { if field := value.FieldByName(k); field.IsValid() { - if field.Interface() == v { - found = true - if results == nil { - results = make(Members, 0) - } - results = append(results, member) + if field.Interface() != v { + break } + matchCount++ } } + if matchCount == len(cond) { + found = true + results = append(results, member) + } } return }