Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
fix: custom provider generator (#43)
Browse files Browse the repository at this point in the history
* fix-custom-provider (test-file is not updated yet)

* fix some test according to changes

* remove unrelevant test-case

* add test coverage

* update readme

* update readme
  • Loading branch information
bxcodec authored Jan 13, 2019
1 parent 36687d3 commit 99d982f
Show file tree
Hide file tree
Showing 20 changed files with 725 additions and 253 deletions.
54 changes: 51 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ See documentation in [Godoc](https://godoc.org/github.com/bxcodec/faker)
```shell
go get -u github.com/bxcodec/faker
```
## Example
# Example

### With Tag
## With Tag
Supported tag:

**Internet :**
Expand Down Expand Up @@ -213,7 +213,52 @@ func main() {

```

### Without Tag
## Custom Generator Provider
You can also add your own generator function to your own defined tags. See example below
```go
type Gondoruwo struct {
Name string
Locatadata int
}

type Sample struct {
ID int64 `faker:"customIdFaker"`
Gondoruwo Gondoruwo `faker:"gondoruwo"`
Danger string `faker:"danger"`
}

func CustomGenerator() {
faker.AddProvider("customIdFaker", func(v reflect.Value) (interface{}, error) {
return int64(43), nil
})
faker.AddProvider("danger", func(v reflect.Value) (interface{}, error) {
return "danger-ranger", nil
})

faker.AddProvider("gondoruwo", func(v reflect.Value) (interface{}, error) {
obj := Gondoruwo{
Name: "Power",
Locatadata: 324,
}
return obj, nil
})
}

func main() {
CustomGenerator()
var sample Sample
faker.FakeData(&sample)
fmt.Printf("%+v", sample)
}
```

Results:
```
{ID:43 Gondoruwo:{Name:Power Locatadata:324} Danger:danger-ranger}
```

## Without Tag
You also can use faker to generate your structs data randomly without any tag. And it will fill the data based on its data-type.

```go

Expand Down Expand Up @@ -294,8 +339,11 @@ func main() {

```

## DEMO

![Example to use Faker](https://cdn-images-1.medium.com/max/800/1*AkMbxngg7zfvtWiuvFb4Mg.gif)

## Benchmark
Bench To Generate Fake Data
#### Without Tag
```bash
Expand Down
29 changes: 18 additions & 11 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,42 @@ func SetAddress(net Addresser) {

// Addresser is logical layer for Address
type Addresser interface {
Latitude(v reflect.Value) error
Longitude(v reflect.Value) error
Latitude(v reflect.Value) (interface{}, error)
Longitude(v reflect.Value) (interface{}, error)
}

// Address struct
type Address struct{}

func (i Address) latitute() float32 {
return (rand.Float32() * 180) - 90
}

// Latitude sets latitude of the address
func (i Address) Latitude(v reflect.Value) error {
func (i Address) Latitude(v reflect.Value) (interface{}, error) {
kind := v.Kind()
val := (rand.Float32() * 180) - 90
val := i.latitute()
if kind == reflect.Float32 {
v.Set(reflect.ValueOf(val))
return nil
return float32(val), nil
}
v.Set(reflect.ValueOf(float64(val)))

return nil
return float64(val), nil
}

func (i Address) longitude() float32 {
return (rand.Float32() * 360) - 180
}

// Longitude sets longitude of the address
func (i Address) Longitude(v reflect.Value) error {
func (i Address) Longitude(v reflect.Value) (interface{}, error) {
kind := v.Kind()
val := (rand.Float32() * 360) - 180

val := i.longitude()
if kind == reflect.Float32 {
v.Set(reflect.ValueOf(val))
return nil
return float32(val), nil
}
v.Set(reflect.ValueOf(float64(val)))
return nil
return float64(val), nil
}
115 changes: 79 additions & 36 deletions datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,17 +607,17 @@ const (

// A DateTimer contains random Time generators, returning time string in certain particular format
type DateTimer interface {
UnixTime(v reflect.Value) error
Date() string
Time() string
MonthName() string
Year() string
DayOfWeek() string
DayOfMonth() string
Timestamp() string
Century() string
TimeZone() string
TimePeriod() string
UnixTime(v reflect.Value) (interface{}, error)
Date(v reflect.Value) (interface{}, error)
Time(v reflect.Value) (interface{}, error)
MonthName(v reflect.Value) (interface{}, error)
Year(v reflect.Value) (interface{}, error)
DayOfWeek(v reflect.Value) (interface{}, error)
DayOfMonth(v reflect.Value) (interface{}, error)
Timestamp(v reflect.Value) (interface{}, error)
Century(v reflect.Value) (interface{}, error)
TimeZone(v reflect.Value) (interface{}, error)
TimePeriod(v reflect.Value) (interface{}, error)
}

var date DateTimer
Expand All @@ -642,68 +642,111 @@ func SetDateTimer(d DateTimer) {
type DateTime struct {
}

func (d DateTime) unixtime() int64 {
return RandomUnixTime()
}

// UnixTime get unix time
func (d DateTime) UnixTime(v reflect.Value) error {
func (d DateTime) UnixTime(v reflect.Value) (interface{}, error) {
kind := v.Kind()

var val int64
if kind == reflect.Int64 {
v.SetInt(RandomUnixTime())
val = d.unixtime()
} else {
v.SetInt(0)
val = 0
}
return nil
v.SetInt(val)
return val, nil
}

// Date formats DateTime using example BaseDate const
func (d DateTime) Date() string {
func (d DateTime) date() string {
return time.Unix(RandomUnixTime(), 0).Format(BaseDate)
}

// Time formats DateTime using example Time const
func (d DateTime) Time() string {
// Date formats DateTime using example BaseDate const
func (d DateTime) Date(v reflect.Value) (interface{}, error) {
return d.date(), nil
}

func (d DateTime) time() string {
return time.Unix(RandomUnixTime(), 0).Format(Time)
}

// MonthName formats DateTime using example Month const
func (d DateTime) MonthName() string {
// Time formats DateTime using example Time const
func (d DateTime) Time(v reflect.Value) (interface{}, error) {
return d.time(), nil
}

func (d DateTime) monthName() string {
return time.Unix(RandomUnixTime(), 0).Format(Month)
}

// Year formats DateTime using example Year const
func (d DateTime) Year() string {
// MonthName formats DateTime using example Month const
func (d DateTime) MonthName(v reflect.Value) (interface{}, error) {
return d.monthName(), nil
}

func (d DateTime) year() string {
return time.Unix(RandomUnixTime(), 0).Format(Year)
}

// DayOfWeek formats DateTime using example Day const
func (d DateTime) DayOfWeek() string {
// Year formats DateTime using example Year const
func (d DateTime) Year(v reflect.Value) (interface{}, error) {
return d.year(), nil
}
func (d DateTime) dayOfWeek() string {
return time.Unix(RandomUnixTime(), 0).Format(Day)
}

// DayOfMonth formats DateTime using example DayOfMonth const
func (d DateTime) DayOfMonth() string {
// DayOfWeek formats DateTime using example Day const
func (d DateTime) DayOfWeek(v reflect.Value) (interface{}, error) {
return d.dayOfWeek(), nil
}

func (d DateTime) dayOfMonth() string {
return time.Unix(RandomUnixTime(), 0).Format(DayOfMonth)
}

// Timestamp formats DateTime using example Timestamp const
func (d DateTime) Timestamp() string {
// DayOfMonth formats DateTime using example DayOfMonth const
func (d DateTime) DayOfMonth(v reflect.Value) (interface{}, error) {
return d.dayOfMonth(), nil
}

func (d DateTime) timestamp() string {
return time.Unix(RandomUnixTime(), 0).Format(fmt.Sprintf("%s %s", BaseDate, Time))
}

// Century returns a random century
func (d DateTime) Century() string {
// Timestamp formats DateTime using example Timestamp const
func (d DateTime) Timestamp(v reflect.Value) (interface{}, error) {
return d.timestamp(), nil
}
func (d DateTime) century() string {
return randomElementFromSliceString(century)
}

// TimeZone returns a random timezone
func (d DateTime) TimeZone() string {
// Century returns a random century
func (d DateTime) Century(v reflect.Value) (interface{}, error) {
return d.century(), nil
}

func (d DateTime) timezone() string {
return randomElementFromSliceString(timezones)
}

// TimePeriod formats DateTime using example TimePeriod const
func (d DateTime) TimePeriod() string {
// TimeZone returns a random timezone
func (d DateTime) TimeZone(v reflect.Value) (interface{}, error) {
return d.timezone(), nil
}

func (d DateTime) period() string {
return time.Unix(RandomUnixTime(), 0).Format(TimePeriod)
}

// TimePeriod formats DateTime using example TimePeriod const
func (d DateTime) TimePeriod(v reflect.Value) (interface{}, error) {
return d.period(), nil
}

// RandomUnixTime is a helper function returning random Unix time
func RandomUnixTime() int64 {
return rand.Int63n(time.Now().Unix())
Expand Down
Loading

0 comments on commit 99d982f

Please sign in to comment.