Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions datafiller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"time"

"github.com/Pallinder/go-randomdata"
randomdata "github.com/Pallinder/go-randomdata"
)

func init() {
Expand All @@ -17,8 +17,26 @@ func init() {

const (
taggedStructKey = "datafiller"
// for random string generation
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
)

//generate random string with len 10
func generateString() string {
b := make([]byte, 10)
randSeed := rand.New(rand.NewSource(time.Now().Unix() + rand.Int63n(100)))

for i := 0; i < 10; {
if idx := int(randSeed.Int63() & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i++
}
}
return string(b)
}

// Function Fill takes a pointer to variable of any type and fills the variable
// by with sample data. It panics if the passed value is not a pointer.
func Fill(i interface{}) {
Expand Down Expand Up @@ -112,6 +130,7 @@ func (self *Filler) recursiveSet(val reflect.Value) {
var fullPath string
fullPath = val.Type().PkgPath() + "." + val.Type().Name()
pkgVal, ok := packages[fullPath]

if ok {
val.Set(pkgVal)
return
Expand Down Expand Up @@ -151,7 +170,8 @@ func (self *Filler) recursiveSet(val reflect.Value) {
}
return
} else if val.Kind() == reflect.String {
val.SetString("test")
//generate random string with len 10
val.SetString(generateString())
return
} else if val.Kind() == reflect.Struct {
lngth := val.NumField()
Expand Down
32 changes: 28 additions & 4 deletions datafiller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"reflect"
"testing"
"time"

null "gopkg.in/guregu/null.v3"
)

// Actual tests
Expand Down Expand Up @@ -85,6 +87,30 @@ func TestSimpleTypes(t *testing.T) {
}
}

type PackageTest struct {
NullInt null.Int
NullStr null.String
NullBool null.Bool
}

func TestPackageTypes(t *testing.T) {
var p = PackageTest{}

Fill(&p)

vals := reflect.ValueOf(p)

for i := 0; i < vals.NumField(); i++ {
field := vals.Field(i).Interface()

if nullInt, ok := field.(null.Int); ok {
if !nullInt.Valid {
t.Fatalf("gopkg.in/guregu/null.Int.Valid is not true")
}
}
}
}

// var ArrayV array
// var ChanV chan
// var FuncV func
Expand Down Expand Up @@ -116,14 +142,12 @@ type PFE struct {
}

func TestPointerTypesFilling(t *testing.T) {
expectedOutput := PFF{}
Fill(&expectedOutput)
i := PFE{}

Fill(&i)

if i.PFF == nil || i.PFF.PFQ == "" || i.PFF.PFQ != expectedOutput.PFQ {
t.Errorf("Fill error: Pointer not filled: %v, want %v", i.PFF, expectedOutput)
if i.PFF == nil || i.PFF.PFQ == "" {
t.Errorf("Fill error: Pointer not filled: %v", i.PFF)
}
}

Expand Down
15 changes: 15 additions & 0 deletions packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ import (
"math/rand"
"reflect"
"time"

null "gopkg.in/guregu/null.v3"
)

var packages = make(map[string]reflect.Value)

func packagesInit() {
randSeed := rand.New(rand.NewSource(time.Now().Unix() + rand.Int63n(100)))
packages["time.Time"] = reflect.ValueOf(time.Unix(rand.Int63n(2000000000), 0))
// guregu/null structs have Valid field that is a bool.
// this field was occasionally randomly set to false, meaning whatever value was passsed
// would be interpreted as null. Always want some value, so always set valid = true for null packages
packages["gopkg.in/guregu/null.v3.String"] = reflect.ValueOf(null.NewString(generateString(), true))
packages["gopkg.in/guregu/null.v3.Int"] = reflect.ValueOf(null.NewInt(rand.Int63n(100), true))
if randSeed.Int63n(2) == 0 {
packages["gopkg.in/guregu/null.v3.Bool"] = reflect.ValueOf(null.NewBool(false, true))
} else {
packages["gopkg.in/guregu/null.v3.Bool"] = reflect.ValueOf(null.NewBool(true, true))
}
packages["gopkg.in/guregu/null.v3.Float"] = reflect.ValueOf(null.NewFloat(float64(randSeed.Float32()), true))
packages["gopkg.in/guregu/null.v3.Time"] = reflect.ValueOf(null.NewTime(time.Unix(rand.Int63n(2000000000), 0), true))
}