Skip to content

Commit

Permalink
adding random generators - close #234
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed Oct 11, 2020
1 parent 236f3b2 commit ed855c9
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
106 changes: 106 additions & 0 deletions v2/pkg/generators/dsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ import (
"encoding/base64"
"encoding/hex"
"html"
"math"
"math/rand"
"net/url"
"regexp"
"strings"

"github.com/Knetic/govaluate"
)

var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var numbers = "1234567890"

// HelperFunctions contains the dsl functions
func HelperFunctions() (functions map[string]govaluate.ExpressionFunction) {
functions = make(map[string]govaluate.ExpressionFunction)
Expand Down Expand Up @@ -144,5 +149,106 @@ func HelperFunctions() (functions map[string]govaluate.ExpressionFunction) {
return compiled.MatchString(args[1].(string)), nil
}

// random generators
functions["rand_char"] = func(args ...interface{}) (interface{}, error) {
chars := letters + numbers
bad := ""
if len(args) >= 1 {
chars = args[0].(string)
}
if len(args) >= 2 {
bad = args[1].(string)
}

chars = TrimAll(chars, bad)

return chars[rand.Intn(len(chars))], nil
}

functions["rand_base"] = func(args ...interface{}) (interface{}, error) {
l := 0
bad := ""
base := letters + numbers

if len(args) >= 1 {
l = args[0].(int)
}
if len(args) >= 2 {
bad = args[1].(string)
}
if len(args) >= 3 {
base = args[2].(string)
}

base = TrimAll(base, bad)

return RandSeq(base, l), nil
}

functions["rand_text_alphanumeric"] = func(args ...interface{}) (interface{}, error) {
l := 0
bad := ""
chars := letters + numbers

if len(args) >= 1 {
l = args[0].(int)
}
if len(args) >= 2 {
bad = args[1].(string)
}

chars = TrimAll(chars, bad)

return RandSeq(chars, l), nil
}

functions["rand_text_alpha"] = func(args ...interface{}) (interface{}, error) {
l := 0
bad := ""
chars := letters

if len(args) >= 1 {
l = args[0].(int)
}
if len(args) >= 2 {
bad = args[1].(string)
}

chars = TrimAll(chars, bad)

return RandSeq(chars, l), nil
}

functions["rand_text_numeric"] = func(args ...interface{}) (interface{}, error) {
l := 0
bad := ""
chars := numbers

if len(args) >= 1 {
l = args[0].(int)
}
if len(args) >= 2 {
bad = args[1].(string)
}

chars = TrimAll(chars, bad)

return RandSeq(chars, l), nil
}

functions["rand_int"] = func(args ...interface{}) (interface{}, error) {
min := 0
max := math.MaxInt32

if len(args) >= 1 {
min = args[0].(int)
}
if len(args) >= 2 {
max = args[1].(int)
}

return rand.Intn(max-min) + min, nil
}

return functions
}
26 changes: 26 additions & 0 deletions v2/pkg/generators/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generators
import (
"bufio"
"fmt"
"math/rand"
"os"
"strings"
)
Expand Down Expand Up @@ -158,3 +159,28 @@ func FileExists(filename string) bool {

return !info.IsDir()
}

// TrimDelimiters removes trailing brackets
func SliceContins(s []string, k string) bool {
for _, a := range s {
if a == k {
return true
}
}
return false
}

func TrimAll(s string, cutset string) string {
for _, c := range cutset {
s = strings.ReplaceAll(s, string(c), "")
}
return s
}

func RandSeq(base string, n int) string {
b := make([]rune, n)
for i := range b {
b[i] = rune(base[rand.Intn(len(base))])
}
return string(b)
}

0 comments on commit ed855c9

Please sign in to comment.