Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add utility function for increasing interfaces of type integer #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -90,6 +90,38 @@ func StringToTimestamp(unixTimestamp string) (time.Time, error) {
return time.Unix(int64(sec), int64(dec*(1e9))), nil
}

// IncrementIntInterface takes a value and increase it by the input increment.
// The function assumes the value is an integer and inherently of type integer or float.
func IncrementIntInterface(val interface{}, increment int) (interface{}, error) {
if val != nil {
switch val.(type) {
case float64:
var callNumberOfConsecutiveTurns float64
var ok bool
callNumberOfConsecutiveTurns, ok = val.(float64)
if !ok {
err := errors.NewError(fmt.Sprintf("Error getting consecutive turns number as int: %v", callNumberOfConsecutiveTurns), nil, false)
return 0, err
}

val = int(callNumberOfConsecutiveTurns) + increment

case int:
var callNumberOfConsecutiveTurns int
var ok bool
callNumberOfConsecutiveTurns, ok = val.(int)
if !ok {
err := errors.NewError(fmt.Sprintf("Error getting consecutive turns number as int: %v", callNumberOfConsecutiveTurns), nil, false)
return 0, err
}

val = callNumberOfConsecutiveTurns + increment
}
}

return val, nil
}

// Evaluate ...
func Evaluate(templateText string, metadata map[string]interface{}) string {
return evaluate(templateText, metadata, nil)