Remove random from generation

This commit is contained in:
Aaron L 2016-08-14 21:13:24 -07:00
parent 1caa8afa73
commit 6e18bbd5f1

View file

@ -1,7 +1,6 @@
package boil package boil
import ( import (
"math/rand"
"reflect" "reflect"
"regexp" "regexp"
"sort" "sort"
@ -180,7 +179,7 @@ func (s *Seed) randomizeField(field reflect.Value, fieldType string, canBeNull b
// depending on the canBeNull flag. // depending on the canBeNull flag.
if canBeNull { if canBeNull {
// 1 in 3 chance of being null or zero value // 1 in 3 chance of being null or zero value
isNull = rand.Intn(3) == 1 isNull = s.nextInt()%3 == 0
} else { } else {
// if canBeNull is false, then never return null values. // if canBeNull is false, then never return null values.
isNull = false isNull = false
@ -259,7 +258,7 @@ func (s *Seed) getStructRandValue(typ reflect.Type) interface{} {
case typeNullBool: case typeNullBool:
return null.NewBool(s.nextInt()%2 == 0, true) return null.NewBool(s.nextInt()%2 == 0, true)
case typeNullString: case typeNullString:
return null.NewString(randStr(1, s.nextInt()), true) return null.NewString(s.randStr(1), true)
case typeNullTime: case typeNullTime:
return null.NewTime(s.randDate(), true) return null.NewTime(s.randDate(), true)
case typeNullFloat32: case typeNullFloat32:
@ -361,13 +360,13 @@ func (s *Seed) getVariableRandValue(kind reflect.Kind, typ reflect.Type) interfa
case reflect.Bool: case reflect.Bool:
return true return true
case reflect.String: case reflect.String:
return randStr(1, s.nextInt()) return s.randStr(1)
case reflect.Slice: case reflect.Slice:
sliceVal := typ.Elem() sliceVal := typ.Elem()
if sliceVal.Kind() != reflect.Uint8 { if sliceVal.Kind() != reflect.Uint8 {
return errors.Errorf("unsupported slice type: %T, was expecting byte slice.", typ.String()) return errors.Errorf("unsupported slice type: %T, was expecting byte slice.", typ.String())
} }
return randByteSlice(5+rand.Intn(20), s.nextInt()) return s.randByteSlice(5 + s.nextInt()%20)
} }
return nil return nil
@ -375,19 +374,19 @@ func (s *Seed) getVariableRandValue(kind reflect.Kind, typ reflect.Type) interfa
const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func randStr(ln int, s int) string { func (s *Seed) randStr(ln int) string {
str := make([]byte, ln) str := make([]byte, ln)
for i := 0; i < ln; i++ { for i := 0; i < ln; i++ {
str[i] = byte(alphabet[s%len(alphabet)]) str[i] = byte(alphabet[s.nextInt()%len(alphabet)])
} }
return string(str) return string(str)
} }
func randByteSlice(ln int, s int) []byte { func (s *Seed) randByteSlice(ln int) []byte {
str := make([]byte, ln) str := make([]byte, ln)
for i := 0; i < ln; i++ { for i := 0; i < ln; i++ {
str[i] = byte(s % 256) str[i] = byte(s.nextInt() % 256)
} }
return str return str