2016-08-18 09:03:14 +02:00
|
|
|
// Package randomize has helpers for randomization of structs and fields
|
|
|
|
package randomize
|
2016-07-13 18:51:40 +02:00
|
|
|
|
|
|
|
import (
|
2016-09-08 11:07:33 +02:00
|
|
|
"fmt"
|
2016-09-08 16:35:43 +02:00
|
|
|
"math/rand"
|
2016-07-13 18:51:40 +02:00
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2016-08-14 11:31:51 +02:00
|
|
|
"sync/atomic"
|
2016-07-13 18:51:40 +02:00
|
|
|
"time"
|
|
|
|
|
2016-09-07 15:50:54 +02:00
|
|
|
"gopkg.in/nullbio/null.v5"
|
2016-08-09 09:59:30 +02:00
|
|
|
|
2016-08-13 20:36:03 +02:00
|
|
|
"github.com/pkg/errors"
|
2016-08-01 06:56:49 +02:00
|
|
|
"github.com/satori/go.uuid"
|
2016-09-08 11:07:33 +02:00
|
|
|
"github.com/vattle/sqlboiler/boil/types"
|
2016-08-09 09:59:30 +02:00
|
|
|
"github.com/vattle/sqlboiler/strmangle"
|
2016-07-13 18:51:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
typeNullFloat32 = reflect.TypeOf(null.Float32{})
|
|
|
|
typeNullFloat64 = reflect.TypeOf(null.Float64{})
|
|
|
|
typeNullInt = reflect.TypeOf(null.Int{})
|
|
|
|
typeNullInt8 = reflect.TypeOf(null.Int8{})
|
|
|
|
typeNullInt16 = reflect.TypeOf(null.Int16{})
|
|
|
|
typeNullInt32 = reflect.TypeOf(null.Int32{})
|
|
|
|
typeNullInt64 = reflect.TypeOf(null.Int64{})
|
|
|
|
typeNullUint = reflect.TypeOf(null.Uint{})
|
|
|
|
typeNullUint8 = reflect.TypeOf(null.Uint8{})
|
|
|
|
typeNullUint16 = reflect.TypeOf(null.Uint16{})
|
|
|
|
typeNullUint32 = reflect.TypeOf(null.Uint32{})
|
|
|
|
typeNullUint64 = reflect.TypeOf(null.Uint64{})
|
|
|
|
typeNullString = reflect.TypeOf(null.String{})
|
|
|
|
typeNullBool = reflect.TypeOf(null.Bool{})
|
|
|
|
typeNullTime = reflect.TypeOf(null.Time{})
|
2016-09-08 11:07:33 +02:00
|
|
|
typeNullBytes = reflect.TypeOf(null.Bytes{})
|
|
|
|
typeNullJSON = reflect.TypeOf(null.JSON{})
|
2016-07-13 18:51:40 +02:00
|
|
|
typeTime = reflect.TypeOf(time.Time{})
|
2016-09-08 11:07:33 +02:00
|
|
|
typeJSON = reflect.TypeOf(types.JSON{})
|
|
|
|
rgxValidTime = regexp.MustCompile(`[2-9]+`)
|
2016-07-13 18:51:40 +02:00
|
|
|
|
2016-09-08 16:35:43 +02:00
|
|
|
validatedTypes = []string{
|
|
|
|
"inet", "line", "uuid", "interval",
|
|
|
|
"json", "jsonb", "box", "cidr", "circle",
|
|
|
|
"lseg", "macaddr", "path", "pg_lsn", "point",
|
|
|
|
"polygon", "txid_snapshot", "money",
|
|
|
|
}
|
2016-08-15 05:12:01 +02:00
|
|
|
)
|
2016-07-13 18:51:40 +02:00
|
|
|
|
2016-08-15 05:12:01 +02:00
|
|
|
// Seed is an atomic counter for pseudo-randomization structs. Using full
|
|
|
|
// randomization leads to collisions in a domain where uniqueness is an
|
|
|
|
// important factor.
|
|
|
|
type Seed int64
|
2016-07-13 18:51:40 +02:00
|
|
|
|
2016-08-15 05:44:59 +02:00
|
|
|
// NewSeed creates a new seed for pseudo-randomization.
|
|
|
|
func NewSeed() *Seed {
|
|
|
|
s := new(int64)
|
|
|
|
*s = time.Now().Unix()
|
|
|
|
return (*Seed)(s)
|
|
|
|
}
|
|
|
|
|
2016-08-15 05:12:01 +02:00
|
|
|
func (s *Seed) nextInt() int {
|
2016-08-14 11:31:51 +02:00
|
|
|
return int(atomic.AddInt64((*int64)(s), 1))
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 09:03:14 +02:00
|
|
|
// Struct gets its fields filled with random data based on the seed.
|
2016-07-13 18:51:40 +02:00
|
|
|
// It will ignore the fields in the blacklist.
|
2016-08-18 09:03:14 +02:00
|
|
|
// It will ignore fields that have the struct tag boil:"-"
|
|
|
|
func Struct(s *Seed, str interface{}, colTypes map[string]string, canBeNull bool, blacklist ...string) error {
|
2016-07-13 18:51:40 +02:00
|
|
|
// Don't modify blacklist
|
|
|
|
copyBlacklist := make([]string, len(blacklist))
|
|
|
|
copy(copyBlacklist, blacklist)
|
|
|
|
blacklist = copyBlacklist
|
|
|
|
|
|
|
|
sort.Strings(blacklist)
|
|
|
|
|
|
|
|
// Check if it's pointer
|
|
|
|
value := reflect.ValueOf(str)
|
|
|
|
kind := value.Kind()
|
|
|
|
if kind != reflect.Ptr {
|
2016-08-13 20:36:03 +02:00
|
|
|
return errors.Errorf("Outer element should be a pointer, given a non-pointer: %T", str)
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if it's a struct
|
|
|
|
value = value.Elem()
|
|
|
|
kind = value.Kind()
|
|
|
|
if kind != reflect.Struct {
|
2016-08-13 20:36:03 +02:00
|
|
|
return errors.Errorf("Inner element should be a struct, given a non-struct: %T", str)
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
typ := value.Type()
|
|
|
|
nFields := value.NumField()
|
|
|
|
|
|
|
|
// Iterate through fields, randomizing
|
|
|
|
for i := 0; i < nFields; i++ {
|
|
|
|
fieldVal := value.Field(i)
|
|
|
|
fieldTyp := typ.Field(i)
|
|
|
|
|
2016-07-15 12:14:47 +02:00
|
|
|
var found bool
|
|
|
|
for _, v := range blacklist {
|
|
|
|
if strmangle.TitleCase(v) == fieldTyp.Name {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found {
|
2016-07-13 18:51:40 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-08-18 09:03:14 +02:00
|
|
|
if fieldTyp.Tag.Get("boil") == "-" {
|
2016-08-18 06:21:08 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-08-03 02:53:34 +02:00
|
|
|
fieldDBType := colTypes[fieldTyp.Name]
|
2016-08-18 09:03:14 +02:00
|
|
|
if err := randomizeField(s, fieldVal, fieldDBType, canBeNull); err != nil {
|
2016-07-13 18:51:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// randDate generates a random time.Time between 1850 and 2050.
|
|
|
|
// Only the Day/Month/Year columns are set so that Dates and DateTimes do
|
|
|
|
// not cause mismatches in the test data comparisons.
|
2016-08-18 09:03:14 +02:00
|
|
|
func randDate(s *Seed) time.Time {
|
2016-07-13 18:51:40 +02:00
|
|
|
t := time.Date(
|
2016-08-15 05:44:59 +02:00
|
|
|
1850+s.nextInt()%160,
|
|
|
|
time.Month(1+(s.nextInt()%12)),
|
|
|
|
1+(s.nextInt()%25),
|
2016-07-13 18:51:40 +02:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
time.UTC,
|
|
|
|
)
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2016-08-14 17:32:57 +02:00
|
|
|
// randomizeField changes the value at field to a "randomized" value.
|
|
|
|
//
|
|
|
|
// If canBeNull is false:
|
|
|
|
// The value will always be a non-null and non-zero value.
|
|
|
|
|
|
|
|
// If canBeNull is true:
|
|
|
|
// The value has the possibility of being null or non-zero at random.
|
2016-08-18 09:03:14 +02:00
|
|
|
func randomizeField(s *Seed, field reflect.Value, fieldType string, canBeNull bool) error {
|
2016-07-13 18:51:40 +02:00
|
|
|
kind := field.Kind()
|
|
|
|
typ := field.Type()
|
|
|
|
|
2016-08-14 17:32:57 +02:00
|
|
|
var value interface{}
|
|
|
|
var isNull bool
|
2016-07-13 18:51:40 +02:00
|
|
|
|
2016-08-14 17:32:57 +02:00
|
|
|
// Validated columns always need to be set regardless of canBeNull,
|
|
|
|
// and they have to adhere to a strict value format.
|
2016-08-15 05:12:01 +02:00
|
|
|
foundValidated := strmangle.SetInclude(fieldType, validatedTypes)
|
2016-08-14 12:07:23 +02:00
|
|
|
|
2016-08-14 17:32:57 +02:00
|
|
|
if foundValidated {
|
|
|
|
if kind == reflect.Struct {
|
|
|
|
switch typ {
|
|
|
|
case typeNullString:
|
|
|
|
if fieldType == "interval" {
|
2016-08-15 05:12:01 +02:00
|
|
|
value = null.NewString(strconv.Itoa((s.nextInt()%26)+2)+" days", true)
|
2016-08-14 17:32:57 +02:00
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "uuid" {
|
|
|
|
value = null.NewString(uuid.NewV4().String(), true)
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
2016-09-08 16:35:43 +02:00
|
|
|
if fieldType == "box" || fieldType == "line" || fieldType == "lseg" ||
|
|
|
|
fieldType == "path" || fieldType == "polygon" {
|
|
|
|
value = null.NewString(randBox(), true)
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "cidr" || fieldType == "inet" {
|
|
|
|
value = null.NewString(randNetAddr(), true)
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "macaddr" {
|
|
|
|
value = null.NewString(randMacAddr(), true)
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "circle" {
|
|
|
|
value = null.NewString(randCircle(), true)
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "pg_lsn" {
|
|
|
|
value = null.NewString(randLsn(), true)
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "point" {
|
|
|
|
value = null.NewString(randPoint(), true)
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "txid_snapshot" {
|
|
|
|
value = null.NewString(randTxID(), true)
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "money" {
|
|
|
|
value = null.NewString(randMoney(s), true)
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-08 11:07:33 +02:00
|
|
|
case typeNullJSON:
|
|
|
|
value = null.NewJSON([]byte(fmt.Sprintf(`"%s"`, randStr(s, 1))), true)
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
2016-08-14 17:32:57 +02:00
|
|
|
} else {
|
|
|
|
switch kind {
|
|
|
|
case reflect.String:
|
|
|
|
if fieldType == "interval" {
|
2016-08-15 05:12:01 +02:00
|
|
|
value = strconv.Itoa((s.nextInt()%26)+2) + " days"
|
2016-08-14 17:32:57 +02:00
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "uuid" {
|
|
|
|
value = uuid.NewV4().String()
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-08 16:35:43 +02:00
|
|
|
if fieldType == "box" || fieldType == "line" || fieldType == "lseg" ||
|
|
|
|
fieldType == "path" || fieldType == "polygon" {
|
|
|
|
value = randBox()
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "cidr" || fieldType == "inet" {
|
|
|
|
value = randNetAddr()
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "macaddr" {
|
|
|
|
value = randMacAddr()
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "circle" {
|
|
|
|
value = randCircle()
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "pg_lsn" {
|
|
|
|
value = randLsn()
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "point" {
|
|
|
|
value = randPoint()
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "txid_snapshot" {
|
|
|
|
value = randTxID()
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fieldType == "money" {
|
|
|
|
value = randMoney(s)
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
2016-09-08 11:07:33 +02:00
|
|
|
switch typ {
|
|
|
|
case typeJSON:
|
|
|
|
value = []byte(fmt.Sprintf(`"%s"`, randStr(s, 1)))
|
|
|
|
field.Set(reflect.ValueOf(value))
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
2016-08-14 17:32:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the regular columns, these can be set or not set
|
|
|
|
// depending on the canBeNull flag.
|
|
|
|
if canBeNull {
|
|
|
|
// 1 in 3 chance of being null or zero value
|
2016-08-15 06:13:24 +02:00
|
|
|
isNull = s.nextInt()%3 == 0
|
2016-07-13 18:51:40 +02:00
|
|
|
} else {
|
2016-08-14 17:32:57 +02:00
|
|
|
// if canBeNull is false, then never return null values.
|
|
|
|
isNull = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the value to be returned
|
|
|
|
if kind == reflect.Struct {
|
|
|
|
if isNull {
|
|
|
|
value = getStructNullValue(typ)
|
|
|
|
} else {
|
2016-08-18 09:03:14 +02:00
|
|
|
value = getStructRandValue(s, typ)
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
2016-08-14 17:32:57 +02:00
|
|
|
} else {
|
|
|
|
if isNull {
|
2016-08-15 07:00:26 +02:00
|
|
|
value = getVariableZeroValue(kind)
|
2016-08-14 17:32:57 +02:00
|
|
|
} else {
|
2016-08-18 09:03:14 +02:00
|
|
|
value = getVariableRandValue(s, kind, typ)
|
2016-08-14 17:32:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == nil {
|
2016-08-19 11:56:27 +02:00
|
|
|
return errors.Errorf("unsupported type: %s", typ.String())
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 17:32:57 +02:00
|
|
|
field.Set(reflect.ValueOf(value))
|
2016-08-15 07:00:26 +02:00
|
|
|
|
2016-08-14 17:32:57 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getStructNullValue for the matching type.
|
|
|
|
func getStructNullValue(typ reflect.Type) interface{} {
|
|
|
|
switch typ {
|
|
|
|
case typeTime:
|
|
|
|
return time.Time{}
|
|
|
|
case typeNullBool:
|
|
|
|
return null.NewBool(false, false)
|
|
|
|
case typeNullString:
|
|
|
|
return null.NewString("", false)
|
|
|
|
case typeNullTime:
|
|
|
|
return null.NewTime(time.Time{}, false)
|
|
|
|
case typeNullFloat32:
|
|
|
|
return null.NewFloat32(0.0, false)
|
|
|
|
case typeNullFloat64:
|
|
|
|
return null.NewFloat64(0.0, false)
|
|
|
|
case typeNullInt:
|
|
|
|
return null.NewInt(0, false)
|
|
|
|
case typeNullInt8:
|
|
|
|
return null.NewInt8(0, false)
|
|
|
|
case typeNullInt16:
|
|
|
|
return null.NewInt16(0, false)
|
|
|
|
case typeNullInt32:
|
|
|
|
return null.NewInt32(0, false)
|
|
|
|
case typeNullInt64:
|
|
|
|
return null.NewInt64(0, false)
|
|
|
|
case typeNullUint:
|
|
|
|
return null.NewUint(0, false)
|
|
|
|
case typeNullUint8:
|
|
|
|
return null.NewUint8(0, false)
|
|
|
|
case typeNullUint16:
|
|
|
|
return null.NewUint16(0, false)
|
|
|
|
case typeNullUint32:
|
|
|
|
return null.NewUint32(0, false)
|
|
|
|
case typeNullUint64:
|
|
|
|
return null.NewUint64(0, false)
|
2016-09-08 11:07:33 +02:00
|
|
|
case typeNullBytes:
|
|
|
|
return null.NewBytes(nil, false)
|
2016-08-14 17:32:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getStructRandValue returns a "random" value for the matching type.
|
|
|
|
// The randomness is really an incrementation of the global seed,
|
|
|
|
// this is done to avoid duplicate key violations.
|
2016-08-18 09:03:14 +02:00
|
|
|
func getStructRandValue(s *Seed, typ reflect.Type) interface{} {
|
2016-08-14 17:32:57 +02:00
|
|
|
switch typ {
|
|
|
|
case typeTime:
|
2016-08-18 09:03:14 +02:00
|
|
|
return randDate(s)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullBool:
|
2016-08-15 05:12:01 +02:00
|
|
|
return null.NewBool(s.nextInt()%2 == 0, true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullString:
|
2016-08-18 09:03:14 +02:00
|
|
|
return null.NewString(randStr(s, 1), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullTime:
|
2016-08-18 09:03:14 +02:00
|
|
|
return null.NewTime(randDate(s), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullFloat32:
|
2016-08-15 05:12:01 +02:00
|
|
|
return null.NewFloat32(float32(s.nextInt()%10)/10.0+float32(s.nextInt()%10), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullFloat64:
|
2016-08-15 05:12:01 +02:00
|
|
|
return null.NewFloat64(float64(s.nextInt()%10)/10.0+float64(s.nextInt()%10), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullInt:
|
2016-08-15 05:12:01 +02:00
|
|
|
return null.NewInt(s.nextInt(), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullInt8:
|
2016-08-15 05:12:01 +02:00
|
|
|
return null.NewInt8(int8(s.nextInt()), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullInt16:
|
2016-08-15 05:12:01 +02:00
|
|
|
return null.NewInt16(int16(s.nextInt()), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullInt32:
|
2016-08-15 05:12:01 +02:00
|
|
|
return null.NewInt32(int32(s.nextInt()), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullInt64:
|
2016-08-15 05:12:01 +02:00
|
|
|
return null.NewInt64(int64(s.nextInt()), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullUint:
|
2016-08-15 05:12:01 +02:00
|
|
|
return null.NewUint(uint(s.nextInt()), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullUint8:
|
2016-08-15 05:12:01 +02:00
|
|
|
return null.NewUint8(uint8(s.nextInt()), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullUint16:
|
2016-08-15 05:12:01 +02:00
|
|
|
return null.NewUint16(uint16(s.nextInt()), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullUint32:
|
2016-08-15 05:12:01 +02:00
|
|
|
return null.NewUint32(uint32(s.nextInt()), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
case typeNullUint64:
|
2016-08-15 05:12:01 +02:00
|
|
|
return null.NewUint64(uint64(s.nextInt()), true)
|
2016-09-08 11:07:33 +02:00
|
|
|
case typeNullBytes:
|
|
|
|
return null.NewBytes(randByteSlice(s, 16), true)
|
2016-08-14 17:32:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-15 07:00:26 +02:00
|
|
|
// getVariableZeroValue for the matching type.
|
|
|
|
func getVariableZeroValue(kind reflect.Kind) interface{} {
|
2016-08-14 17:32:57 +02:00
|
|
|
switch kind {
|
|
|
|
case reflect.Float32:
|
|
|
|
return float32(0)
|
|
|
|
case reflect.Float64:
|
|
|
|
return float64(0)
|
|
|
|
case reflect.Int:
|
|
|
|
return int(0)
|
|
|
|
case reflect.Int8:
|
|
|
|
return int8(0)
|
|
|
|
case reflect.Int16:
|
|
|
|
return int16(0)
|
|
|
|
case reflect.Int32:
|
|
|
|
return int32(0)
|
|
|
|
case reflect.Int64:
|
|
|
|
return int64(0)
|
|
|
|
case reflect.Uint:
|
|
|
|
return uint(0)
|
|
|
|
case reflect.Uint8:
|
|
|
|
return uint8(0)
|
|
|
|
case reflect.Uint16:
|
|
|
|
return uint16(0)
|
|
|
|
case reflect.Uint32:
|
|
|
|
return uint32(0)
|
|
|
|
case reflect.Uint64:
|
|
|
|
return uint64(0)
|
|
|
|
case reflect.Bool:
|
|
|
|
return false
|
|
|
|
case reflect.String:
|
|
|
|
return ""
|
|
|
|
case reflect.Slice:
|
|
|
|
return []byte(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getVariableRandValue returns a "random" value for the matching kind.
|
|
|
|
// The randomness is really an incrementation of the global seed,
|
|
|
|
// this is done to avoid duplicate key violations.
|
2016-08-18 09:03:14 +02:00
|
|
|
func getVariableRandValue(s *Seed, kind reflect.Kind, typ reflect.Type) interface{} {
|
2016-08-14 17:32:57 +02:00
|
|
|
switch kind {
|
|
|
|
case reflect.Float32:
|
2016-08-15 05:12:01 +02:00
|
|
|
return float32(float32(s.nextInt()%10)/10.0 + float32(s.nextInt()%10))
|
2016-08-14 17:32:57 +02:00
|
|
|
case reflect.Float64:
|
2016-08-15 05:12:01 +02:00
|
|
|
return float64(float64(s.nextInt()%10)/10.0 + float64(s.nextInt()%10))
|
2016-08-14 17:32:57 +02:00
|
|
|
case reflect.Int:
|
2016-08-15 05:12:01 +02:00
|
|
|
return s.nextInt()
|
2016-08-14 17:32:57 +02:00
|
|
|
case reflect.Int8:
|
2016-08-15 05:12:01 +02:00
|
|
|
return int8(s.nextInt())
|
2016-08-14 17:32:57 +02:00
|
|
|
case reflect.Int16:
|
2016-08-15 05:12:01 +02:00
|
|
|
return int16(s.nextInt())
|
2016-08-14 17:32:57 +02:00
|
|
|
case reflect.Int32:
|
2016-08-15 05:12:01 +02:00
|
|
|
return int32(s.nextInt())
|
2016-08-14 17:32:57 +02:00
|
|
|
case reflect.Int64:
|
2016-08-15 05:12:01 +02:00
|
|
|
return int64(s.nextInt())
|
2016-08-14 17:32:57 +02:00
|
|
|
case reflect.Uint:
|
2016-08-15 05:12:01 +02:00
|
|
|
return uint(s.nextInt())
|
2016-08-14 17:32:57 +02:00
|
|
|
case reflect.Uint8:
|
2016-08-15 05:12:01 +02:00
|
|
|
return uint8(s.nextInt())
|
2016-08-14 17:32:57 +02:00
|
|
|
case reflect.Uint16:
|
2016-08-15 05:12:01 +02:00
|
|
|
return uint16(s.nextInt())
|
2016-08-14 17:32:57 +02:00
|
|
|
case reflect.Uint32:
|
2016-08-15 05:12:01 +02:00
|
|
|
return uint32(s.nextInt())
|
2016-08-14 17:32:57 +02:00
|
|
|
case reflect.Uint64:
|
2016-08-15 05:12:01 +02:00
|
|
|
return uint64(s.nextInt())
|
2016-08-14 17:32:57 +02:00
|
|
|
case reflect.Bool:
|
|
|
|
return true
|
|
|
|
case reflect.String:
|
2016-08-18 09:03:14 +02:00
|
|
|
return randStr(s, 1)
|
2016-08-14 17:32:57 +02:00
|
|
|
case reflect.Slice:
|
|
|
|
sliceVal := typ.Elem()
|
|
|
|
if sliceVal.Kind() != reflect.Uint8 {
|
|
|
|
return errors.Errorf("unsupported slice type: %T, was expecting byte slice.", typ.String())
|
|
|
|
}
|
2016-08-18 09:03:14 +02:00
|
|
|
return randByteSlice(s, 5+s.nextInt()%20)
|
2016-08-14 17:32:57 +02:00
|
|
|
}
|
2016-07-13 18:51:40 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
2016-08-18 09:03:14 +02:00
|
|
|
func randStr(s *Seed, ln int) string {
|
2016-07-13 18:51:40 +02:00
|
|
|
str := make([]byte, ln)
|
|
|
|
for i := 0; i < ln; i++ {
|
2016-08-15 06:13:24 +02:00
|
|
|
str[i] = byte(alphabet[s.nextInt()%len(alphabet)])
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return string(str)
|
|
|
|
}
|
|
|
|
|
2016-08-18 09:03:14 +02:00
|
|
|
func randByteSlice(s *Seed, ln int) []byte {
|
2016-07-13 18:51:40 +02:00
|
|
|
str := make([]byte, ln)
|
|
|
|
for i := 0; i < ln; i++ {
|
2016-08-15 06:13:24 +02:00
|
|
|
str[i] = byte(s.nextInt() % 256)
|
2016-07-13 18:51:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
2016-09-08 16:35:43 +02:00
|
|
|
|
|
|
|
func randPoint() string {
|
|
|
|
a := rand.Intn(100)
|
|
|
|
b := a + 1
|
|
|
|
return fmt.Sprintf("(%d,%d)", a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func randBox() string {
|
|
|
|
a := rand.Intn(100)
|
|
|
|
b := a + 1
|
|
|
|
c := a + 2
|
|
|
|
d := a + 3
|
|
|
|
return fmt.Sprintf("(%d,%d),(%d,%d)", a, b, c, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func randCircle() string {
|
|
|
|
a, b, c := rand.Intn(100), rand.Intn(100), rand.Intn(100)
|
|
|
|
return fmt.Sprintf("((%d,%d),%d)", a, b, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func randNetAddr() string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"%d.%d.%d.%d",
|
|
|
|
rand.Intn(254)+1,
|
|
|
|
rand.Intn(254)+1,
|
|
|
|
rand.Intn(254)+1,
|
|
|
|
rand.Intn(254)+1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func randMacAddr() string {
|
|
|
|
buf := make([]byte, 6)
|
|
|
|
_, err := rand.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the local bit
|
|
|
|
buf[0] |= 2
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"%02x:%02x:%02x:%02x:%02x:%02x",
|
|
|
|
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func randLsn() string {
|
|
|
|
a := rand.Int63n(9000000)
|
|
|
|
b := rand.Int63n(9000000)
|
|
|
|
return fmt.Sprintf("%d/%d", a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func randTxID() string {
|
|
|
|
// Order of integers is relevant
|
|
|
|
a := rand.Intn(200) + 100
|
|
|
|
b := a + 100
|
|
|
|
c := a
|
|
|
|
d := a + 50
|
|
|
|
return fmt.Sprintf("%d:%d:%d,%d", a, b, c, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func randMoney(s *Seed) string {
|
|
|
|
return fmt.Sprintf("%d.00", s.nextInt())
|
|
|
|
}
|