Ce package a été créé en s'inspirant du go-ruler de Thomas pour exécuter un ensemble simple de règles contre une entité.
Cette version comprend quelques fonctionnalités supplémentaires, notamment les composites AND et OR et la possibilité d'ajouter des comparateurs personnalisés.
Remarque : ce package ne compare que deux types : string
et float64
, cela fonctionne bien avec encoding/json
.
// Create a new instance of an engine with some default comparators
e , err := NewJSONEngine ( json . RawMessage ( `{"composites":[{"operator":"or","rules":[{"comparator":"always-false","path":"user.name","value":"Trevor"},{"comparator":"eq","path":"user.name","value":"Trevor"}]}]}` ))
if err != nil {
panic ( err )
}
// Add a new, custom comparator
e = e . AddComparator ( "always-false" , func ( a , b interface {}) bool {
return false
})
// Give some properties, this map can be deeper and supports interfaces
props := map [ string ] interface {}{
"user" : map [ string ] interface {}{
"name" : "Trevor" ,
}
}
// Run the engine on the props
res := e . Evaluate ( props )
// res == true
eq
retournera vrai si a == b
neq
retournera vrai si a != b
lt
retournera vrai si a < b
lte
retournera vrai si a <= b
gt
retournera vrai si a > b
gte
retournera vrai si a >= b
contains
retournera vrai si a
contient b
ncontains
retournera vrai si a
ne contient pas b
oneof
retournera vrai si a
est l'un des b
noneof
retournera vrai si a
n'est pas l'un des b
regex
retournera vrai si a
correspond b
contains
et ncontains
fonctionnent pour les comparaisons de sous-chaînes ainsi que pour les comparaisons d'éléments dans la collection.
Lorsqu'il est utilisé pour des comparaisons d'éléments dans une collection, contains
s'attend à ce que le premier argument soit une tranche. contains
est différent de oneof
dans la mesure où oneof
s'attend à ce que le deuxième argument soit une tranche.
Référence | N | Vitesse | Utilisé | Allocations |
---|---|---|---|---|
BenchmarkEqual-12 | 650602549 | 5,52 ns/opération | 0 B/op | 0 allocation/opération |
BenchmarkNotEqual-12 | 876894124 | 4.09 ns/op | 0 B/op | 0 allocation/opération |
RéférenceLessThan-12 | 1000000000 | 2,84 ns/opération | 0 B/op | 0 allocation/opération |
RéférenceLessThanEqual-12 | 1000000000 | 2,57 ns/opération | 0 B/op | 0 allocation/opération |
RéférenceGreaterThan-12 | 1000000000 | 2,07 ns/op | 0 B/op | 0 allocation/opération |
RéférenceGreaterThanEqual-12 | 1000000000 | 2,86 ns/opération | 0 B/op | 0 allocation/opération |
RéférenceRegex-12 | 4524237 | 793 ns/op | 753 B/op | 11 allocations/opération |
RéférenceRegexPhone-12 | 1000000 | 3338 ns/op | 3199 B/op | 30 allocations/opération |
BenchmarkContains-12 | 499627219 | 7,16 ns/op | 0 B/op | 0 allocation/opération |
BenchmarkStringContains-12 | 405497102 | 8,87 ns/opération | 0 B/op | 0 allocation/opération |
BenchmarkContainsLong50000-12 | 18992 | 184016 ns/op | 0 B/op | 0 allocation/opération |
BenchmarkNotContains-12 | 292932907 | 12,3 ns/opération | 0 B/op | 0 allocation/opération |
BenchmarkStringNotContains-12 | 392618857 | 9.14 ns/op | 0 B/op | 0 allocation/opération |
BenchmarkNotContainsLong50000-12 | 19243 | 191787 ns/op | 0 B/op | 0 allocation/opération |
BenchmarkOneOf-12 | 1000000000 | 1,80 ns/opération | 0 B/op | 0 allocation/opération |
RéférenceAucunDe-12 | 1000000000 | 1,79 ns/opération | 0 B/op | 0 allocation/opération |
RéférencePluckShallow-12 | 85997188 | 41,6 ns/opération | 16 B/op | 1 allocation/opération |
RéférencePluckDeep-12 | 18789103 | 194 ns/op | 112 B/op | 1 allocation/opération |
BenchmarkRule_evaluate-12 | 69558996 | 51,1 ns/op | 16 B/op | 1 allocation/opération |
BenchmarkComposite_evaluate-12 | 59484760 | 55,7 ns/op | 16 B/op | 1 allocation/opération |
BenchmarkEngine_Evaluate-12 | 47892318 | 75,0 ns/opération | 16 B/op | 1 allocation/opération |
Pour exécuter des tests de performances :
go test -run none -bench . -benchtime 3s -benchmem
Tous les benchmarks ont été exécutés sur :
Mac OS High Sierra 2,6 GHz Intel Core i7 16 Go 2 400 MHz DDR4
Copyright © 2019 Trevor Hutto
Sous licence Apache, version 2.0 (la « Licence » ); vous ne pouvez pas utiliser cette œuvre sauf en conformité avec la licence. Vous pouvez obtenir une copie de la Licence dans le fichier LICENSE, ou à :
http://www.apache.org/licenses/LICENSE-2.0
Sauf disposition contraire de la loi applicable ou accord écrit, le logiciel distribué sous la licence est distribué « EN L'ÉTAT », SANS GARANTIE OU CONDITION D'AUCUNE SORTE, expresse ou implicite. Consultez la licence pour connaître la langue spécifique régissant les autorisations et les limitations en vertu de la licence.