This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
initial commit
- Loading branch information
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package faker | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"math/rand" | ||
"reflect" | ||
"time" | ||
) | ||
|
||
type SomeStruct struct { | ||
ID int64 | ||
Name string | ||
Hobbies []string | ||
Categories []int64 | ||
OtherStruct AStruct | ||
} | ||
type AStruct struct { | ||
Number int64 | ||
Height int64 | ||
} | ||
|
||
var src = rand.NewSource(time.Now().UnixNano()) | ||
|
||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
const ( | ||
letterIdxBits = 6 // 6 bits to represent a letter index | ||
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits | ||
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits | ||
) | ||
|
||
func FakeData(a interface{}) error { | ||
|
||
return setData(reflect.ValueOf(a)) | ||
} | ||
|
||
func setSliceData(v reflect.Value) error { | ||
v = reflect.Indirect(v) | ||
fmt.Println(v.Type()) | ||
switch v.Type().String() { | ||
case `[]int64`: | ||
v.Set(reflect.ValueOf([]int64{1, 2, 3})) | ||
break | ||
case `[]string`: | ||
v.Set(reflect.ValueOf([]string{"hello", "world"})) | ||
break | ||
default: | ||
return errors.New("Slice of Struct Not Supported Yet") | ||
} | ||
return nil | ||
} | ||
func setData(v reflect.Value) error { | ||
r := rand.New(src) | ||
|
||
if v.Kind() != reflect.Ptr { | ||
return errors.New("Not a pointer value") | ||
} | ||
|
||
v = reflect.Indirect(v) | ||
switch v.Kind() { | ||
|
||
case reflect.Int: | ||
v.SetInt(r.Int63()) | ||
case reflect.Int8: | ||
v.Set(reflect.ValueOf(int8(r.Int()))) | ||
case reflect.Int16: | ||
v.Set(reflect.ValueOf(int16(r.Int()))) | ||
case reflect.Int32: | ||
v.Set(reflect.ValueOf(r.Int31())) | ||
case reflect.Int64: | ||
v.Set(reflect.ValueOf(r.Int63())) | ||
case reflect.Float32: | ||
v.Set(reflect.ValueOf(r.Float32())) | ||
case reflect.Float64: | ||
v.Set(reflect.ValueOf(r.Float64())) | ||
case reflect.String: | ||
v.SetString(randomString(25)) | ||
case reflect.Bool: | ||
val := r.Intn(1) > 0 | ||
v.SetBool(val) | ||
case reflect.Slice: | ||
return setSliceData(v) | ||
case reflect.Struct: | ||
for i := 0; i < v.NumField(); i++ { | ||
err := setData(v.Field(i).Addr()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
default: | ||
return errors.New("Unsupported kind: " + v.Kind().String() + " Change Without using * (pointer) in any Field of Struct") | ||
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
func randomString(n int) string { | ||
b := make([]byte, n) | ||
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters! | ||
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; { | ||
if remain == 0 { | ||
cache, remain = src.Int63(), letterIdxMax | ||
} | ||
if idx := int(cache & letterIdxMask); idx < len(letterBytes) { | ||
b[i] = letterBytes[idx] | ||
i-- | ||
} | ||
cache >>= letterIdxBits | ||
remain-- | ||
} | ||
|
||
return string(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package faker | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestFakerData(t *testing.T) { | ||
// a := SomeStruct{} | ||
var a SomeStruct | ||
err := FakeData(&a) | ||
|
||
fmt.Println(a) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package: github.com/bxcodec/faker | ||
import: [] |