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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A Golang package for filling structs by random data.

# Installation

`go get github.com/erggo/datafiller`
`go get github.com/HelpfulHuman/datafiller`

# Sample Usage
```go
Expand All @@ -16,7 +16,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/erggo/datafiller"
"github.com/HelpfulHuman/datafiller"
)

type S struct {
Expand Down
18 changes: 15 additions & 3 deletions datafiller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ 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
)

// Function Fill takes a pointer to variable of any type and fills the variable
Expand Down Expand Up @@ -122,14 +126,14 @@ func (self *Filler) recursiveSet(val reflect.Value) {
val.Kind() == reflect.Int16 ||
val.Kind() == reflect.Int32 ||
val.Kind() == reflect.Int64 {
val.SetInt(self.randSeed.Int63n(100))
val.SetInt(self.randSeed.Int63n(100) + 1)
return
} else if val.Kind() == reflect.Uint ||
val.Kind() == reflect.Uint8 ||
val.Kind() == reflect.Uint16 ||
val.Kind() == reflect.Uint32 ||
val.Kind() == reflect.Uint64 {
val.SetUint(uint64(self.randSeed.Int63n(100)))
val.SetUint(uint64(self.randSeed.Int63n(100) + 1))
return
} else if val.Kind() == reflect.Float32 ||
val.Kind() == reflect.Float64 {
Expand All @@ -151,7 +155,15 @@ func (self *Filler) recursiveSet(val reflect.Value) {
}
return
} else if val.Kind() == reflect.String {
val.SetString("test")
//generate random string with len 10
b := make([]byte, 12)
for i := 0; i < 12; {
if idx := int(rand.Int63() & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i++
}
}
val.SetString(string(b))
return
} else if val.Kind() == reflect.Struct {
lngth := val.NumField()
Expand Down
26 changes: 12 additions & 14 deletions datafiller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,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 All @@ -148,16 +146,16 @@ func TestSimpleValues(t *testing.T) {
value interface{}
expectedValue interface{}
}{
{&IntV, int(55)},
{&Int8V, int8(55)},
{&Int16V, int16(55)},
{&Int32V, int32(55)},
{&Int64V, int64(55)},
{&UintV, uint(55)},
{&Uint8V, uint8(55)},
{&Uint16V, uint16(55)},
{&Uint32V, uint32(55)},
{&Uint64V, uint64(55)},
{&IntV, int(56)},
{&Int8V, int8(56)},
{&Int16V, int16(56)},
{&Int32V, int32(56)},
{&Int64V, int64(56)},
{&UintV, uint(56)},
{&Uint8V, uint8(56)},
{&Uint16V, uint16(56)},
{&Uint32V, uint32(56)},
{&Uint64V, uint64(56)},
{&Float32V, float32(0.91889215)},
{&Float64V, float64(0.9188921451568604)},
// {&Complex64V, complex(0.91889215, 0.23150717)},
Expand Down
26 changes: 0 additions & 26 deletions example/easy.go

This file was deleted.

5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/HelpfulHuman/datafiller

go 1.12

require github.com/Pallinder/go-randomdata v1.1.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/Pallinder/go-randomdata v1.1.0 h1:gUubB1IEUliFmzjqjhf+bgkg1o6uoFIkRsP3VrhEcx8=
github.com/Pallinder/go-randomdata v1.1.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=