Skip to content

Commit

Permalink
add a bit more templates
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Aug 3, 2022
1 parent e9d24b0 commit 6c9da00
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/job/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"context"
"encoding/base64"
"fmt"
"strconv"
"time"

"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -61,13 +62,26 @@ func logJob(ctx context.Context, args config.Args, globalConfig *GlobalConfig, a
func setVarJob(ctx context.Context, args config.Args, globalConfig *GlobalConfig, a *metrics.Accumulator, logger *zap.Logger) (data any, err error) {
var jobConfig struct {
Value string
Type string
}

if err := mapstructure.Decode(args, &jobConfig); err != nil {
return nil, fmt.Errorf("error parsing job config: %w", err)
}

return templates.ParseAndExecute(logger, jobConfig.Value, ctx), nil
switch jobConfig.Type {
case "int":
return strconv.Atoi(templates.ParseAndExecute(logger, jobConfig.Value, ctx))
case "uint":
val, err := strconv.ParseUint(templates.ParseAndExecute(logger, jobConfig.Value, ctx), 10, 32)
return uint(val), err
case "int64":
return strconv.ParseInt(templates.ParseAndExecute(logger, jobConfig.Value, ctx), 10, 64)
case "uint64":
return strconv.ParseUint(templates.ParseAndExecute(logger, jobConfig.Value, ctx), 10, 64)
default:
return templates.ParseAndExecute(logger, jobConfig.Value, ctx), nil
}
}

// "check" in config
Expand Down
49 changes: 49 additions & 0 deletions src/utils/templates/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package templates

func mod(lhs, rhs int) int {
return lhs % rhs
}

func add(lhs, rhs int) int {
return lhs + rhs
}

func sub(lhs, rhs int) int {
return lhs - rhs
}

func umod(lhs, rhs uint) uint {
return lhs % rhs
}

func uadd(lhs, rhs uint) uint {
return lhs + rhs
}

func usub(lhs, rhs uint) uint {
return lhs - rhs
}

func mod64(lhs, rhs int64) int64 {
return lhs % rhs
}

func add64(lhs, rhs int64) int64 {
return lhs + rhs
}

func sub64(lhs, rhs int64) int64 {
return lhs - rhs
}

func umod64(lhs, rhs uint64) uint64 {
return lhs % rhs
}

func uadd64(lhs, rhs uint64) uint64 {
return lhs + rhs
}

func usub64(lhs, rhs uint64) uint64 {
return lhs - rhs
}
18 changes: 10 additions & 8 deletions src/utils/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ func randomAplhaNum(n int) string {
return randomString(n, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
}

func mod(lhs, rhs int) int {
return lhs % rhs
}

func add(lhs, rhs int) int {
return lhs + rhs
}

// ContextKey used to work with context and not trigger linter
type ContextKey string

Expand Down Expand Up @@ -144,6 +136,16 @@ func Parse(input string) (*template.Template, error) {
"get_url": getURLContent,
"mod": mod,
"add": add,
"sub": sub,
"umod": umod,
"uadd": uadd,
"usub": usub,
"mod64": mod64,
"add64": add64,
"sub64": sub64,
"umod64": umod64,
"uadd64": uadd64,
"usub64": usub64,
"ctx_key": ctxKey,
"cookie_string": cookieString,
}).Parse(input)
Expand Down

0 comments on commit 6c9da00

Please sign in to comment.