Skip to content

Commit

Permalink
use pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
RikaCelery committed Oct 10, 2024
1 parent 24877ed commit e810fbf
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ type PatternReplyMatched = struct {
}

// PatternText type zero.PatternTextMatched
func PatternText(regex string) PatternSegment {
func PatternText(regex string) *PatternSegment {
re := regexp.MustCompile(regex)
return PatternSegment{
return &PatternSegment{
Type: "text",
Matcher: func(ctx *Ctx, msg message.MessageSegment) bool {
s := msg.Data["text"]
Expand All @@ -225,8 +225,8 @@ func PatternText(regex string) PatternSegment {
}

// PatternAt type zero.PatternAtMatched
func PatternAt() PatternSegment {
return PatternSegment{
func PatternAt() *PatternSegment {
return &PatternSegment{
Type: "at",
Matcher: func(ctx *Ctx, msg message.MessageSegment) bool {
if _, ok := ctx.State[KEY_PATTERN]; !ok {
Expand All @@ -243,8 +243,8 @@ func PatternAt() PatternSegment {
}

// PatternImage type zero.PatternImageMatched
func PatternImage() PatternSegment {
return PatternSegment{
func PatternImage() *PatternSegment {
return &PatternSegment{
Type: "image",
Matcher: func(ctx *Ctx, msg message.MessageSegment) bool {
if _, ok := ctx.State[KEY_PATTERN]; !ok {
Expand All @@ -259,8 +259,8 @@ func PatternImage() PatternSegment {
}

// PatternReply type zero.PatternReplyMatched
func PatternReply() PatternSegment {
return PatternSegment{
func PatternReply() *PatternSegment {
return &PatternSegment{
Type: "reply",
Matcher: func(ctx *Ctx, msg message.MessageSegment) bool {
if _, ok := ctx.State[KEY_PATTERN]; !ok {
Expand All @@ -273,15 +273,15 @@ func PatternReply() PatternSegment {
},
}
}
func containsOptional(pattern []PatternSegment) bool {
func containsOptional(pattern []*PatternSegment) bool {
for _, p := range pattern {
if p.Optional {
return true
}
}
return false
}
func patternMatch(ctx *Ctx, pattern []PatternSegment, msgs []message.MessageSegment) bool {
func patternMatch(ctx *Ctx, pattern []*PatternSegment, msgs []message.MessageSegment) bool {
if !containsOptional(pattern) && len(pattern) != len(msgs) {
return false
}
Expand All @@ -298,7 +298,7 @@ func patternMatch(ctx *Ctx, pattern []PatternSegment, msgs []message.MessageSegm
}

// PatternRule check if the message can be matched by the pattern
func PatternRule(pattern ...PatternSegment) Rule {
func PatternRule(pattern ...*PatternSegment) Rule {
return func(ctx *Ctx) bool {
// copy messages
msgs := make([]message.MessageSegment, 0, len(ctx.Event.Message))
Expand Down

0 comments on commit e810fbf

Please sign in to comment.