Skip to content

Commit

Permalink
Merge pull request #71 from embano1/issue-70
Browse files Browse the repository at this point in the history
Add godoc example
  • Loading branch information
timbray authored Jun 16, 2022
2 parents f9338bb + eb7d826 commit 061248b
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions example_test.go
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"
}

0 comments on commit 061248b

Please sign in to comment.