-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chaitanya Munukutla <[email protected]>
- Loading branch information
Showing
4 changed files
with
129 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package broker | ||
|
||
import "strings" | ||
|
||
const ( | ||
PatternSeparator = "." | ||
SingleTokenMatcher = "*" | ||
MultiTokenMatcher = ">" | ||
) | ||
|
||
type Subscription struct { | ||
active bool | ||
group string | ||
pattern string | ||
} | ||
|
||
func (s *Subscription) IsActive() bool { | ||
return s.active | ||
} | ||
|
||
func (s *Subscription) GetGroup() string { | ||
return s.group | ||
} | ||
|
||
func (s *Subscription) GetPattern() string { | ||
return s.pattern | ||
} | ||
|
||
func (s *Subscription) Matches(topic string) bool { | ||
|
||
patternTokens := strings.Split(s.pattern, PatternSeparator) | ||
topicTokens := strings.Split(topic, PatternSeparator) | ||
|
||
if len(patternTokens) > len(topicTokens) { | ||
return false | ||
} | ||
|
||
for tIdx, topicToken := range topicTokens { | ||
if tIdx > len(patternTokens)-1 { | ||
return false | ||
} | ||
if patternTokens[tIdx] == MultiTokenMatcher { | ||
break | ||
} | ||
if patternTokens[tIdx] == topicToken || patternTokens[tIdx] == SingleTokenMatcher { | ||
continue | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package broker | ||
|
||
import "testing" | ||
|
||
var matchTests = []struct { | ||
pattern string | ||
topic string | ||
expected bool | ||
}{ | ||
{ | ||
pattern: "time.us.*", | ||
topic: "time.us.east", | ||
expected: true, | ||
}, | ||
{ | ||
pattern: "time.us.*", | ||
topic: "time.us.east.atlanta", | ||
expected: false, | ||
}, | ||
{ | ||
pattern: "time.us.>", | ||
topic: "time.us.east.atlanta", | ||
expected: true, | ||
}, | ||
{ | ||
pattern: "time.*.east", | ||
topic: "time.us.east", | ||
expected: true, | ||
}, | ||
{ | ||
pattern: "time.*.east", | ||
topic: "time.eu.east", | ||
expected: true, | ||
}, | ||
{ | ||
pattern: ">", | ||
topic: "time.eu.east", | ||
expected: true, | ||
}, | ||
} | ||
|
||
func TestSubscription_Matches(t *testing.T) { | ||
for _, tt := range matchTests { | ||
t.Run(tt.pattern, func(_t *testing.T) { | ||
subscription := &Subscription{ | ||
active: true, | ||
group: "", | ||
pattern: tt.pattern, | ||
} | ||
ok := subscription.Matches(tt.topic) | ||
if ok != tt.expected { | ||
_t.Fatal("doesn't match") | ||
} | ||
}) | ||
} | ||
} |