Skip to content

Commit

Permalink
Refactor datafilter package to use generics instead of interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kaanaktas committed Mar 4, 2023
1 parent 618c6c3 commit 013a8ec
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 34 deletions.
25 changes: 12 additions & 13 deletions datafilter/datafilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,40 @@ import (
"sync"
)

type Executor struct {
type Executor[T Validator] struct {
Actions []policy.Action
Data *string
}

func (e *Executor) Apply() {
func (e *Executor[T]) Apply() {
breaker := make(chan string)
in := make(chan Validator)
in := make(chan T)
closeCh := make(chan struct{})

go processor(e.Actions, in, breaker)
go validator(e.Data, in, closeCh, breaker)
go processor[T](e.Actions, in, breaker)
go validator[T](e.Data, in, closeCh, breaker)

select {
case v := <-breaker:
panic(v)
case <-closeCh:
log.Println("no match with datafilter rules")
}

log.Println("no match with datafilter rules")
}

func processor(actions []policy.Action, in chan<- Validator, breaker <-chan string) {
func processor[T Validator](actions []policy.Action, in chan<- T, breaker <-chan string) {
defer close(in)

for _, v := range actions {
if v.Active {
if rule, ok := cacheIn.Get(v.Name); ok {
processRule(rule.([]Validator), in, breaker)
processRule(rule.([]T), in, breaker)
}
}
}
}

func processRule(patterns []Validator, in chan<- Validator, breaker <-chan string) {
func processRule[T Validator](patterns []T, in chan<- T, breaker <-chan string) {
var wg sync.WaitGroup

for _, pattern := range patterns {
Expand All @@ -65,7 +64,7 @@ func processRule(patterns []Validator, in chan<- Validator, breaker <-chan strin
wg.Wait()
}

func validator(data *string, in <-chan Validator, closeCh chan<- struct{}, breaker chan<- string) {
func validator[T Validator](data *string, in <-chan T, closeCh chan<- struct{}, breaker chan<- string) {
defer func() {
close(closeCh)
}()
Expand All @@ -75,13 +74,13 @@ func validator(data *string, in <-chan Validator, closeCh chan<- struct{}, break
//Distribute work to multiple workers
for i := 0; i < config.NumberOfWorker; i++ {
wg.Add(1)
worker(&wg, data, in, breaker)
worker[T](&wg, data, in, breaker)
}

wg.Wait()
}

func worker(wg *sync.WaitGroup, data *string, in <-chan Validator, breaker chan<- string) {
func worker[T Validator](wg *sync.WaitGroup, data *string, in <-chan T, breaker chan<- string) {
go func() {
defer wg.Done()

Expand Down
2 changes: 1 addition & 1 deletion datafilter/datafilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestExecuteDataFilter(t *testing.T) {
}
}()

executor := &Executor{
executor := &Executor[Validator]{
Actions: actions,
Data: &tt.args.data,
}
Expand Down
2 changes: 1 addition & 1 deletion executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func createExecutor(data string, statementType string, actions []policy.Action)
case config.StatementSchedule:
return &schedule.Executor{Actions: actions}
case config.StatementData:
return &datafilter.Executor{Actions: actions, Data: &data}
return &datafilter.Executor[datafilter.Validator]{Actions: actions, Data: &data}
default:
panic(fmt.Sprintf("StatementType: %s doesn't exist", statementType))
}
Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package main

import "testing"

func TestMain(t *testing.T) {
func TestDefault(t *testing.T) {
main()
}
9 changes: 2 additions & 7 deletions policy/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func LoadPolicies(policyRuleSetPath, commonRulesPath string) {
readPolicies(policyRuleSetPath, &policies)

var commonPolicies []commonPolicies
readCommonPolicies(commonRulesPath, &commonPolicies)
readPolicies(commonRulesPath, &commonPolicies)

statements := make(Statements)
for _, policy := range policies {
Expand All @@ -69,16 +69,11 @@ func LoadPolicies(policyRuleSetPath, commonRulesPath string) {
log.Println("common policies have been loaded successfully")
}

func readPolicies(policyRuleSetPath string, policies *[]policy) {
func readPolicies[T any](policyRuleSetPath string, policies *T) {
content := config.MustReadFile(filepath.Join(config.RootDirectory, policyRuleSetPath))
config.MustUnmarshalYaml(policyRuleSetPath, content, policies)
}

func readCommonPolicies(commonRulesPath string, retrievedCommonPolicies *[]commonPolicies) {
content := config.MustReadFile(filepath.Join(config.RootDirectory, commonRulesPath))
config.MustUnmarshalYaml(commonRulesPath, content, retrievedCommonPolicies)
}

func populatePolicyRules(rule commonPolicies, policyRules Statements, policyRuleKey string) {
sort.Slice(rule.Policy.Statements, func(i, j int) bool {
return rule.Policy.Statements[i].Order < rule.Policy.Statements[j].Order
Expand Down
18 changes: 11 additions & 7 deletions schedule/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,23 @@ func LoadSchedules(scheduleStatementPath string) {
cacheIn.Set(key, schedules, cache.NoExpiration)
}

func generateStartTime(start string) string {
return time.Now().Format(dateLayout) + "T" + start
}

func getCurrentDayOfTheWeek() string {
return time.Now().Weekday().String()
}

func isScheduleMatchWithPolicy(sc schedule) bool {
if isScheduledDayActive(sc.Days) {
if isScheduledDayActive(sc.Days, getCurrentDayOfTheWeek) {
return isScheduledTime(generateStartTime(sc.Start), time.Duration(sc.Duration))
}
return false
}

func generateStartTime(start string) string {
return time.Now().Format(dateLayout) + "T" + start
}

func isScheduledDayActive(days []string) bool {
dayOfTheWeek := time.Now().Weekday().String()
func isScheduledDayActive(days []string, getCurrentDayOfTheWeek func() string) bool {
dayOfTheWeek := getCurrentDayOfTheWeek()
for _, day := range days {
if dayOfTheWeek == day {
return true
Expand Down
35 changes: 33 additions & 2 deletions schedule/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestLoad(t *testing.T) {
},
}

expectedCacheSize := 4
expectedCacheSize := 5

if cachedData, ok := cacheIn.Get(key); ok {
scheduleCache := cachedData.([]schedule)
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestExecutor_Apply(t *testing.T) {
name: "test_schedule_not_permitted",
panic: true,
fields: fields{Actions: []policy.Action{
{Name: "weekdays_all_day", Active: true, Order: 10},
{Name: "week_all_day", Active: true, Order: 10},
}}},
{
name: "test_schedule_not_active",
Expand Down Expand Up @@ -213,3 +213,34 @@ func TestExecutor_Apply(t *testing.T) {
})
}
}

func TestIsScheduledDayActive(t *testing.T) {
tests := []struct {
name string
days []string
currentDayFunc func() string
want bool
}{
{
name: "Day is in the list",
days: []string{"Monday", "Tuesday", "Wednesday"},
currentDayFunc: func() string { return "Tuesday" },
want: true,
},
{
name: "Day is not in the list",
days: []string{"Monday", "Wednesday", "Friday"},
currentDayFunc: func() string { return "Tuesday" },
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := isScheduledDayActive(tt.days, tt.currentDayFunc)
if got != tt.want {
t.Errorf("isScheduledDayActive() = %v, want %v", got, tt.want)
}
})
}
}
16 changes: 14 additions & 2 deletions schedule/testdata/schedule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- Friday
start: 08:00:00
duration: 600
message: The service is not permitted in the weekdays between 08:00 and 18:00
message: The service is not permitted on the weekdays between 08:00 and 18:00
- scheduleName: weekdays_all_day
days:
- Monday
Expand All @@ -24,9 +24,21 @@
- Friday
start: 00:00:00
duration: 1440
message: The service is not permitted in the weekdays
message: The service is not permitted on the weekdays
- scheduleName: no_days_match
days:
start: 00:00:00
duration: 1440
message: The service is permitted
- scheduleName: week_all_day
days:
- Monday
- Tuesday
- Wednesday
- Thursday
- Friday
- Saturday
- Sunday
start: 00:00:00
duration: 1440
message: The service is not permitted on any day of the week

0 comments on commit 013a8ec

Please sign in to comment.