Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
brianvoe committed Feb 21, 2024
2 parents 130211d + 9aa5357 commit 70b796c
Show file tree
Hide file tree
Showing 16 changed files with 552 additions and 432 deletions.
644 changes: 319 additions & 325 deletions BENCHMARKS.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ type Foo struct {
Map map[string]int `fakesize:"2"`
Array []string `fakesize:"2"`
ArrayRange []string `fakesize:"2,6"`
Bar Bar
Bar Bar
Skip *string `fake:"skip"` // Set to "skip" to not generate data for
SkipAlt *string `fake:"-"` // Set to "-" to not generate data for
Created time.Time // Can take in a fake tag as well as a format tag
Expand Down
27 changes: 18 additions & 9 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ func (f *Faker) Error() error {
}

func err(f *Faker) error {
return errors.New(generate(f, getRandValue(f, []string{"error", "generic"})))
genStr, _ := generate(f, getRandValue(f, []string{"error", "generic"}))
return errors.New(genStr)
}

// ErrorObject will return a random error object word
Expand All @@ -29,7 +30,8 @@ func (f *Faker) ErrorObject() error {
}

func errorObject(f *Faker) error {
return errors.New(generate(f, getRandValue(f, []string{"error", "object"})))
genStr, _ := generate(f, getRandValue(f, []string{"error", "object"}))
return errors.New(genStr)
}

// ErrorDatabase will return a random database error
Expand All @@ -43,7 +45,8 @@ func (f *Faker) ErrorDatabase() error {
}

func errorDatabase(f *Faker) error {
return errors.New(generate(f, getRandValue(f, []string{"error", "database"})))
genStr, _ := generate(f, getRandValue(f, []string{"error", "database"}))
return errors.New(genStr)
}

// ErrorGRPC will return a random gRPC error
Expand All @@ -57,7 +60,8 @@ func (f *Faker) ErrorGRPC() error {
}

func errorGRPC(f *Faker) error {
return errors.New(generate(f, getRandValue(f, []string{"error", "grpc"})))
genStr, _ := generate(f, getRandValue(f, []string{"error", "grpc"}))
return errors.New(genStr)
}

// ErrorHTTP will return a random HTTP error
Expand All @@ -71,7 +75,8 @@ func (f *Faker) ErrorHTTP() error {
}

func errorHTTP(f *Faker) error {
return errors.New(generate(f, getRandValue(f, []string{"error", "http"})))
genStr, _ := generate(f, getRandValue(f, []string{"error", "http"}))
return errors.New(genStr)
}

// ErrorHTTPClient will return a random HTTP client error response (400-418)
Expand All @@ -85,7 +90,8 @@ func (f *Faker) ErrorHTTPClient() error {
}

func errorHTTPClient(f *Faker) error {
return errors.New(generate(f, getRandValue(f, []string{"error", "http_client"})))
genStr, _ := generate(f, getRandValue(f, []string{"error", "http_client"}))
return errors.New(genStr)
}

// ErrorHTTPServer will return a random HTTP server error response (500-511)
Expand All @@ -99,7 +105,8 @@ func (f *Faker) ErrorHTTPServer() error {
}

func errorHTTPServer(f *Faker) error {
return errors.New(generate(f, getRandValue(f, []string{"error", "http_server"})))
genStr, _ := generate(f, getRandValue(f, []string{"error", "http_server"}))
return errors.New(genStr)
}

// ErrorRuntime will return a random runtime error
Expand All @@ -113,7 +120,8 @@ func (f *Faker) ErrorRuntime() error {
}

func errorRuntime(f *Faker) error {
return errors.New(generate(f, getRandValue(f, []string{"error", "runtime"})))
genStr, _ := generate(f, getRandValue(f, []string{"error", "runtime"}))
return errors.New(genStr)
}

// ErrorValidation will return a random validation error
Expand All @@ -127,7 +135,8 @@ func (f *Faker) ErrorValidation() error {
}

func errorValidation(f *Faker) error {
return errors.New(generate(f, getRandValue(f, []string{"error", "validation"})))
genStr, _ := generate(f, getRandValue(f, []string{"error", "validation"}))
return errors.New(genStr)
}

func addErrorLookup() {
Expand Down
42 changes: 27 additions & 15 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// Ex: ??? - fda - random letters
//
// For a complete list of runnable functions use FuncsLookup
func Generate(dataVal string) string { return generate(GlobalFaker, dataVal) }
func Generate(dataVal string) (string, error) { return generate(GlobalFaker, dataVal) }

// Generate fake information from given string.
// Replaceable values should be within {}
Expand All @@ -39,16 +39,17 @@ func Generate(dataVal string) string { return generate(GlobalFaker, dataVal) }
// Ex: ??? - fda - random letters
//
// For a complete list of runnable functions use FuncsLookup
func (f *Faker) Generate(dataVal string) string { return generate(f, dataVal) }
func (f *Faker) Generate(dataVal string) (string, error) { return generate(f, dataVal) }

func generate(f *Faker, dataVal string) string {
func generate(f *Faker, dataVal string) (string, error) {
// Replace # with numbers and ? with letters
dataVal = replaceWithNumbers(f, dataVal)
dataVal = replaceWithLetters(f, dataVal)

// Check if string has any replaceable values
// Even if it doesnt its ok we will just return the string
if !strings.Contains(dataVal, "{") && !strings.Contains(dataVal, "}") {
return dataVal
return dataVal, nil
}

// Variables to identify the index in which it exists
Expand Down Expand Up @@ -111,8 +112,15 @@ func generate(f *Faker, dataVal string) string {
if paramsLen == 1 && info.Params[0].Type == "string" {
mapParams.Add(info.Params[0].Field, fParams)
} else if paramsLen > 0 && fParams != "" {
splitVals := funcLookupSplit(fParams)
mapParams = addSplitValsToMapParams(splitVals, info, mapParams)
var err error
splitVals, err := funcLookupSplit(fParams)
if err != nil {
return "", err
}
mapParams, err = addSplitValsToMapParams(splitVals, info, mapParams)
if err != nil {
return "", err
}
}
if mapParams.Size() == 0 {
mapParams = nil
Expand All @@ -121,13 +129,12 @@ func generate(f *Faker, dataVal string) string {
// Call function
fValue, err := info.Generate(f, mapParams, info)
if err != nil {
// If we came across an error just dont replace value
dataVal = strings.Replace(dataVal, "{"+fParts+"}", err.Error(), 1)
} else {
// Successfully found, run replace with new value
dataVal = strings.Replace(dataVal, "{"+fParts+"}", fmt.Sprintf("%v", fValue), 1)
return "", err
}

// Successfully found, run replace with new value
dataVal = strings.Replace(dataVal, "{"+fParts+"}", fmt.Sprintf("%v", fValue), 1)

// Reset the curly index back to -1 and reset ignores
startCurly = -1
startCurlyIgnore = []int{}
Expand All @@ -148,7 +155,7 @@ func generate(f *Faker, dataVal string) string {
continue
}

return dataVal
return dataVal, nil
}

// FixedWidthOptions defines values needed for csv generation
Expand Down Expand Up @@ -207,15 +214,20 @@ func fixeWidthFunc(f *Faker, co *FixedWidthOptions) (string, error) {
if funcInfo == nil {
// Try to run the function through generate
for i := 0; i < co.RowCount; i++ {
row = append(row, generate(f, field.Function))
genStr, err := generate(f, field.Function)
if err != nil {
return "", err
}

row = append(row, genStr)
}
} else {
// Generate function value
var err error
for i := 0; i < co.RowCount; i++ {
value, err = funcInfo.Generate(f, &field.Params, funcInfo)
if err != nil {
value = ""
return "", err
}

// Add value to row
Expand Down Expand Up @@ -491,7 +503,7 @@ func addGenerateLookup() {
return nil, errors.New("string length is too large. limit to 1000 characters")
}

return generate(f, str), nil
return generate(f, str)
},
})

Expand Down
86 changes: 55 additions & 31 deletions generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import (

func TestGenerate(t *testing.T) {
output := ""
var err error

numTests := 1000
for i := 0; i < numTests; i++ {
output = Generate("{firstname} {lastname} {email} #?#?#?")
output, err = Generate("{firstname} {lastname} {email} #?#?#?")
if err != nil {
t.Error(err)
}
if strings.Contains(output, "{") || strings.Contains(output, "}") {
t.Error("Output should not contain either { or }. Output: ", output)
}
Expand All @@ -23,7 +27,10 @@ func TestGenerate_Sub(t *testing.T) {
t.Run("Simple", func(t *testing.T) {
Seed(11)

output := Generate("{randomstring:[{firstname},{lastname}]}")
output, err := Generate("{randomstring:[{firstname},{lastname}]}")
if err != nil {
t.Error(err)
}
if output == "" {
t.Error("Output was empty")
}
Expand All @@ -32,7 +39,10 @@ func TestGenerate_Sub(t *testing.T) {
t.Run("Complex", func(t *testing.T) {
Seed(11)

output := Generate("{randomstring:[{randomstring:[{firstname},{lastname}]},{randomstring:[{firstname},{lastname}]}]}")
output, err := Generate("{randomstring:[{randomstring:[{firstname},{lastname}]},{randomstring:[{firstname},{lastname}]}]}")
if err != nil {
t.Error(err)
}
if output == "" {
t.Error("Output was empty")
}
Expand All @@ -42,41 +52,55 @@ func TestGenerate_Sub(t *testing.T) {
func ExampleGenerate() {
Seed(11)

fmt.Println(Generate("{firstname} {lastname} ssn is {ssn} and lives at {street}"))
fmt.Println(Generate("{sentence:3}"))
fmt.Println(Generate("{shuffleints:[1,2,3]}"))
fmt.Println(Generate("{randomint:[-1,2,3,-4]}"))
fmt.Println(Generate("{randomuint:[1,2,3,4]}"))
fmt.Println(Generate("{number:1,50}"))
fmt.Println(Generate("{shufflestrings:[key:value,int:string,1:2,a:b]}"))

// Output: Sonny Stiedemann ssn is 279582238 and lives at 2759 Stationside
// How shall cut.
// [1 2 3]
// 2
genStr, _ := Generate("{firstname} {lastname} {email} #?#?#?")
fmt.Println(genStr)
genStr, _ = Generate("{sentence:3}")
fmt.Println(genStr)
genStr, _ = Generate("{shuffleints:[1,2,3]}")
fmt.Println(genStr)
genStr, _ = Generate("{randomint:[1,2,3,-4]}")
fmt.Println(genStr)
genStr, _ = Generate("{randomuint:[1,2,3,4]}")
fmt.Println(genStr)
genStr, _ = Generate("{number:1,50}")
fmt.Println(genStr)
genStr, _ = Generate("{shufflestrings:[key:value,int:string,1:2,a:b]}")
fmt.Println(genStr)

// Output: Mollie Legros vanceschroeder@turner.com 8K8b1M
// Something am elsewhere.
// [3 2 1]
// 3
// 47
// [1:2 int:string a:b key:value]
// 3
// 18
// [key:value a:b int:string 1:2]
}

func ExampleFaker_Generate() {
f := New(11)

fmt.Println(f.Generate("{firstname} {lastname} ssn is {ssn} and lives at {street}"))
fmt.Println(f.Generate("{sentence:3}"))
fmt.Println(f.Generate("{shuffleints:[1,2,3]}"))
fmt.Println(f.Generate("{randomint:[1,2,3,-4]}"))
fmt.Println(f.Generate("{randomuint:[1,2,3,4]}"))
fmt.Println(f.Generate("{number:1,50}"))
fmt.Println(f.Generate("{shufflestrings:[key:value,int:string,1:2,a:b]}"))

// Output: Sonny Stiedemann ssn is 279582238 and lives at 2759 Stationside
// How shall cut.
// [1 2 3]
// 2
genStr, _ := f.Generate("{firstname} {lastname} {email} #?#?#?")
fmt.Println(genStr)
genStr, _ = f.Generate("{sentence:3}")
fmt.Println(genStr)
genStr, _ = f.Generate("{shuffleints:[1,2,3]}")
fmt.Println(genStr)
genStr, _ = f.Generate("{randomint:[1,2,3,-4]}")
fmt.Println(genStr)
genStr, _ = f.Generate("{randomuint:[1,2,3,4]}")
fmt.Println(genStr)
genStr, _ = f.Generate("{number:1,50}")
fmt.Println(genStr)
genStr, _ = f.Generate("{shufflestrings:[key:value,int:string,1:2,a:b]}")
fmt.Println(genStr)

// Output: Mollie Legros vanceschroeder@turner.com 8K8b1M
// Something am elsewhere.
// [3 2 1]
// 3
// 3
// 47
// [1:2 int:string a:b key:value]
// 18
// [key:value a:b int:string 1:2]
}

func BenchmarkGenerate(b *testing.B) {
Expand Down
4 changes: 3 additions & 1 deletion hacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ func HackerPhrase() string { return hackerPhrase(GlobalFaker) }
func (f *Faker) HackerPhrase() string { return hackerPhrase(f) }

func hackerPhrase(f *Faker) string {
words := strings.Split(generate(f, getRandValue(f, []string{"hacker", "phrase"})), " ")
genStr, _ := generate(f, getRandValue(f, []string{"hacker", "phrase"}))

words := strings.Split(genStr, " ")
words[0] = strings.ToUpper(words[0][0:1]) + words[0][1:]
return strings.Join(words, " ")
}
Expand Down
Loading

0 comments on commit 70b796c

Please sign in to comment.