https://github.com/bsm/qualify
Match a fact against large number of pre-defined rules in Go
https://github.com/bsm/qualify
adtech golang matching qualifier rule-engine rules
Last synced: 10 months ago
JSON representation
Match a fact against large number of pre-defined rules in Go
- Host: GitHub
- URL: https://github.com/bsm/qualify
- Owner: bsm
- License: other
- Created: 2017-11-09T16:30:29.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2023-01-03T10:22:17.000Z (over 3 years ago)
- Last Synced: 2025-06-24T10:49:05.385Z (12 months ago)
- Topics: adtech, golang, matching, qualifier, rule-engine, rules
- Language: Go
- Size: 882 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Qualify!
[](https://travis-ci.org/bsm/qualify)
[](http://godoc.org/github.com/bsm/qualify)
[](https://goreportcard.com/report/github.com/bsm/qualify)
[](https://opensource.org/licenses/Apache-2.0)
Library for fast rules evaluation for Go. Qualify is able to quickly match a fact against large number of pre-defined rules.
### Example:
```go
import (
"fmt"
"github.com/bsm/qualify"
)
// Fact is an example fact
type Fact struct {
Country string
Browser string
OS string
Attrs []int
}
// Enumeration of our fact features
const (
FieldCountry qualify.Field = iota
FieldBrowser
FieldOS
FieldAttrs
)
// factReader is a wrapper around facts to
// make them comply with qualify.Fact
type factReader struct {
Dict qualify.StrDict
Fact
}
func (r *factReader) AppendFieldValues(x []int, f qualify.Field) []int {
switch f {
case FieldCountry:
return r.Dict.Lookup(x, r.Country)
case FieldBrowser:
return r.Dict.Lookup(x, r.Browser)
case FieldOS:
return r.Dict.Lookup(x, r.OS)
case FieldAttrs:
return append(x, r.Attrs...)
}
return x
}
func main() package qualify_test
import (
"fmt"
"github.com/bsm/qualify"
)
func ExampleQualifier() {
// We can use dictionary encoding to translate
// string values into numerics
dict := qualify.NewStrDict()
// Init a new builder popute with rules
// for each outcome.
builder := qualify.NewBuilder()
// Outcome #34 requires:
// * Country to be GBR or FRA, and
// * Attrs to contain 101 or 102, and
// * Attrs to contain 202 or 203
builder.Require(34, FieldCountry,
qualify.OneOf(dict.Add("GBR"), dict.Add("FRA")))
builder.Require(34, FieldAttrs,
qualify.OneOf(101, 102))
builder.Require(34, FieldAttrs,
qualify.OneOf(202, 203))
// Outcome #35 requires:
// * Country NOT to be NLD nor GER, and
// * Browser NOT to be Safari, and
// * OS to be either Android or iOS
builder.Require(35, FieldCountry,
qualify.NoneOf(dict.Add("NLD"), dict.Add("GER")))
builder.Require(35, FieldBrowser,
qualify.NoneOf(dict.Add("Safari")))
builder.Require(35, FieldOS,
qualify.OneOf(dict.Add("Android"), dict.Add("iOS")))
// Setup the qualifier
qfy := builder.Compile()
// Init result set
var res []int
// Matches outcome #34
res = qfy.Qualify(res[:0], &factReader{Dict: dict, Fact: Fact{Country: "GBR", Attrs: []int{101, 202}}})
fmt.Println(res)
// Matches outcome #35
res = qfy.Qualify(res[:0], &factReader{Dict: dict, Fact: Fact{Country: "IRE", OS: "iOS"}})
fmt.Println(res)
// Matches nothing
res = qfy.Qualify(res[:0], &factReader{Dict: dict, Fact: Fact{Country: "NLD"}})
fmt.Println(res)
}
```