forked from timbray/quamina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
54 lines (44 loc) · 1.08 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package quamina_test
import (
"fmt"
"log"
"github.com/timbray/quamina"
)
const userRegisteredEvent = `{
"id": "1c0e1ce4-3d88-4786-a09d-7133c170d02a",
"type": "UserRegistered",
"user": {
"name": "Doe, John",
"premiumAccount": true
}
}
`
const premiumUserPattern = `{
"type":["UserRegistered"],
"user": {"premiumAccount": [true]}
}`
func ExampleNew() {
q, err := quamina.New()
if err != nil {
log.Fatalf("could not create quamina instance: %v", err)
}
const patternName = "premium user"
err = q.AddPattern(patternName, premiumUserPattern)
if err != nil {
log.Fatalf("could not add pattern: %v", err)
}
matches, err := q.MatchesForEvent([]byte(userRegisteredEvent))
if err != nil {
log.Fatalf("could not match for event: %v", err)
}
for _, m := range matches {
if m == patternName {
fmt.Printf("pattern matched for event: %q", patternName)
return
}
}
// you would typically handle no matches cases here, but in this example no
// match is a bug, hence panic :)
panic("no pattern match")
// Output: pattern matched for event: "premium user"
}