-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NEW] add filter to charge-tracker log
- Loading branch information
Showing
6 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package chargeTracker | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
func Filters(filter ...Filter) []Filter { | ||
filters := make([]Filter, 0) | ||
for _, f := range filter { | ||
if f != nil { | ||
filters = append(filters, f) | ||
} | ||
} | ||
return filters | ||
} | ||
|
||
func NewFilter(filterName string, filterValue string, filterNumber int) Filter { | ||
switch filterName { | ||
case "month": | ||
if filterNumber >= 1 && filterNumber <= 12 { | ||
return &MonthFilter{filterValue: filterNumber} | ||
} | ||
case "year": | ||
if filterNumber >= 2000 { | ||
return &YearFilter{filterValue: filterNumber} | ||
} | ||
case "user": | ||
if len(filterValue) > 0 { | ||
return &UserFilter{filterValue: filterValue} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (mf *MonthFilter) Filter(charge *Charge) bool { | ||
return charge.Time.Month() == time.Month(mf.filterValue) | ||
} | ||
|
||
func (yF *YearFilter) Filter(charge *Charge) bool { | ||
return charge.Time.Year() == yF.filterValue | ||
} | ||
|
||
func (uF *UserFilter) Filter(charge *Charge) bool { | ||
return charge.User == uF.filterValue | ||
} |