Skip to content

Commit

Permalink
adding some examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nurettintopal committed Jul 22, 2024
1 parent 6b9f7a1 commit 63036ba
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 16 deletions.
71 changes: 71 additions & 0 deletions examples/basic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"fmt"

"github.com/nurettintopal/rule"
)

func main() {
input := `{
"country": "Turkey",
"city": "Istanbul",
"district": "Kadikoy",
"population": 2000000.00,
"language": "Turkish"
}`

rules := `{
"conditions":[
{
"all":[
{
"field":"external.score",
"operator":"greaterThan",
"value":4
},
{
"field":"population",
"operator":"custom.eligible",
"value": true
}
],
"any":[
{
"field":"country",
"operator":"in",
"value": ["Turkey", "England"]
}
]
}
]
}`

custom := map[string]rule.CustomOperation{
"score": &CustomCountryScore{},
"eligible": &CustomRuleEligible{},
}

if rule.Execute(input, rules, custom) {
fmt.Printf("Rules have been executed. it passed!")
} else {
fmt.Printf("Rules have been executed. it failed!")
}
}

// CustomOperation implementations
type CustomRuleEligible struct{}

func (o *CustomRuleEligible) Execute(input, value interface{}) interface{} {
fmt.Println("DEBUG: CustomRuleEligible Execute", input, value)
return true
}

// CustomOperation implementations
type CustomCountryScore struct{}

func (o *CustomCountryScore) Execute(input, value interface{}) interface{} {
fmt.Println("DEBUG: CustomCountryScore Execute", input, value)
//TODO: there should be some implementation here.
return 4.6
}
6 changes: 1 addition & 5 deletions rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (rsc RuleSetChecker) CheckRuleSet(obj map[string]interface{}, ruleSet RuleS
return true
}

func execute(input string, rules string, custom map[string]CustomOperation) bool {
func Execute(input string, rules string, custom map[string]CustomOperation) bool {
var objs map[string]interface{}
err := json.Unmarshal([]byte(input), &objs)
if err != nil {
Expand All @@ -311,11 +311,7 @@ func execute(input string, rules string, custom map[string]CustomOperation) bool
return ruleSetChecker.CheckRuleSet(objs, ruleSet, custom)
}



// CustomOperation defines the interface for custom operations
type CustomOperation interface {
Execute(input, value interface{}) interface{}
}


22 changes: 11 additions & 11 deletions rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,47 +373,47 @@ func TestRun(t *testing.T) {
]
}`

if execute(input, rules, nil) != true {
if Execute(input, rules, nil) != true {
t.Errorf("it is not passed")
}

if execute(input2, rules, nil) == true {
if Execute(input2, rules, nil) == true {
t.Errorf("it is passed")
}

if execute(input2, rules2, nil) == true {
if Execute(input2, rules2, nil) == true {
t.Errorf("it is passed")
}

if execute(input2, rules3, nil) == true {
if Execute(input2, rules3, nil) == true {
t.Errorf("it is passed")
}

if execute(input2, rules4, nil) == true {
if Execute(input2, rules4, nil) == true {
t.Errorf("it is passed")
}

custom := map[string]CustomOperation{}

if execute(input2, rules4, custom) == true {
if Execute(input2, rules4, custom) == true {
t.Errorf("it is not passed")
}

if execute(input2, rules4, custom) == true {
if Execute(input2, rules4, custom) == true {
t.Errorf("it is not passed")
}

if execute(input2, rules9, custom) == true {
if Execute(input2, rules9, custom) == true {
t.Errorf("it is passed")
}
}

func TestRunWithInvalidJSON(t *testing.T) {
if execute("", "", nil) == true {
func TestRunWithInvalidJSON(t *testing.T) {
if Execute("", "", nil) == true {
t.Errorf("it is passed")
}

if execute("{}", "", nil) == true {
if Execute("{}", "", nil) == true {
t.Errorf("it is passed")
}
}

0 comments on commit 63036ba

Please sign in to comment.