Skip to content

Commit 061248b

Browse files
authored
Merge pull request #71 from embano1/issue-70
Add godoc example
2 parents f9338bb + eb7d826 commit 061248b

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

example_test.go

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

0 commit comments

Comments
 (0)