-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from embano1/issue-70
Add godoc example
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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" | ||
} |