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

Commit a94c89d

Browse files
bxcodecwolf-joe
andauthored
feat: add v4 initial version, with options and restructured package (#169)
* feat: add v4 initial version, with options and restructured package * fix: move max slice to option * fix: move min slice to option * fix: move max str generation to option * fix: set nil if len zero to option * fix: set random len zero to option * chore: move integer boundary to option * fix: move random float to option * fix: add integer boundaries to option * chore: resolve linter * feat: update WithRecursionMaxDepth & unittest (#171) * feat: update WithRecursionMaxDepth & unittest * style: remove unnecessary check * chore: use variadic function for options * chore: move to pkg directory * fix: slice_len (#172) * chore: update readme Co-authored-by: OrangeWolf <[email protected]>
1 parent 6a1323d commit a94c89d

39 files changed

+911
-887
lines changed

README.md

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,42 @@ Faker will generate you a fake data based on your Struct.
1010
[![codecov](https://codecov.io/gh/bxcodec/faker/branch/master/graph/badge.svg)](https://codecov.io/gh/bxcodec/faker)
1111
[![Go Report Card](https://goreportcard.com/badge/github.com/bxcodec/faker)](https://goreportcard.com/report/github.com/bxcodec/faker)
1212
[![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/bxcodec/faker/blob/master/LICENSE)
13-
[![GoDoc](https://godoc.org/github.com/bxcodec/faker?status.svg)](https://godoc.org/github.com/bxcodec/faker)
14-
[![Go.Dev](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white)](https://pkg.go.dev/github.com/bxcodec/faker/v3?tab=doc)
13+
[![Go.Dev](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white)](https://pkg.go.dev/github.com/bxcodec/faker/v4?tab=doc)
1514

1615
## Index
1716

18-
* [Support](#support)
19-
* [Getting Started](#getting-started)
20-
* [Example](#example)
21-
* [Limitation](#limitation)
22-
* [Contribution](#contribution)
23-
17+
- [Support](#support)
18+
- [Getting Started](#getting-started)
19+
- [Example](#example)
20+
- [Limitation](#limitation)
21+
- [Contribution](#contribution)
2422

2523
## Support
2624

2725
You can file an [Issue](https://github.com/bxcodec/faker/issues/new).
28-
See documentation in [Godoc](https://godoc.org/github.com/bxcodec/faker) or in [Go.Dev](https://pkg.go.dev/github.com/bxcodec/faker/v3?tab=doc)
29-
26+
See documentation in [Go.Dev](https://pkg.go.dev/github.com/bxcodec/faker/v4?tab=doc)
3027

3128
## Getting Started
3229

3330
#### Download
3431

3532
```shell
36-
go get -u github.com/bxcodec/faker/v3
33+
go get -u github.com/bxcodec/faker/v4
3734
```
35+
3836
# Example
3937

4038
---
4139

42-
- Using Struct's tag:
43-
- [basic tags: example_with_tags_test.go](/example_with_tags_test.go)
44-
- [length and bounds: example_with_tags_lenbounds_test.go](/example_with_tags_lenbounds_test.go)
45-
- [language: example_with_tags_lang_test.go](/example_with_tags_lang_test.go)
46-
- [unique: example_with_tags_unique_test.go](example_with_tags_unique_test.go)
47-
- [slice length: example_with_tags_slicelength_test.go](example_with_tags_slicelength_test.go)
48-
- Custom Struct's tag (define your own faker data): [example_custom_faker_test.go](/example_custom_faker_test.go)
49-
- Without struct's tag: [example_without_tag_test.go](/example_without_tag_test.go)
50-
- Single Fake Data Function: [example_single_fake_data_test.go](/example_single_fake_data_test.go)
40+
- Using Struct's tag:
41+
- [basic tags: example_with_tags_test.go](/example_with_tags_test.go)
42+
- [length and bounds: example_with_tags_lenbounds_test.go](/example_with_tags_lenbounds_test.go)
43+
- [language: example_with_tags_lang_test.go](/example_with_tags_lang_test.go)
44+
- [unique: example_with_tags_unique_test.go](example_with_tags_unique_test.go)
45+
- [slice length: example_with_tags_slicelength_test.go](example_with_tags_slicelength_test.go)
46+
- Custom Struct's tag (define your own faker data): [example_custom_faker_test.go](/example_custom_faker_test.go)
47+
- Without struct's tag: [example_without_tag_test.go](/example_without_tag_test.go)
48+
- Single Fake Data Function: [example_single_fake_data_test.go](/example_single_fake_data_test.go)
5149

5250
## DEMO
5351

@@ -60,12 +58,15 @@ go get -u github.com/bxcodec/faker/v3
6058
---
6159

6260
Bench To Generate Fake Data
61+
6362
#### Without Tag
63+
6464
```bash
6565
BenchmarkFakerDataNOTTagged-4 500000 3049 ns/op 488 B/op 20 allocs/op
6666
```
6767

6868
#### Using Tag
69+
6970
```bash
7071
BenchmarkFakerDataTagged-4 100000 17470 ns/op 380 B/op 26 allocs/op
7172
```
@@ -77,25 +78,26 @@ BenchmarkFakerDataNOTTagged-4 500000 3049 ns/op
7778
The Struct Field must be PUBLIC.<br>
7879
Support Only For :
7980

80-
* `int`, `int8`, `int16`, `int32` & `int64`
81-
* `[]int`, `[]int8`, `[]int16`, `[]int32` & `[]int64`
82-
* `bool` & `[]bool`
83-
* `string` & `[]string`
84-
* `float32`, `float64`, `[]float32` &`[]float64`
85-
* `time.Time` & `[]time.Time`
86-
* Nested Struct Field
81+
- `int`, `int8`, `int16`, `int32` & `int64`
82+
- `[]int`, `[]int8`, `[]int16`, `[]int32` & `[]int64`
83+
- `bool` & `[]bool`
84+
- `string` & `[]string`
85+
- `float32`, `float64`, `[]float32` &`[]float64`
86+
- `time.Time` & `[]time.Time`
87+
- Nested Struct Field
8788

8889
## Limitation
8990

9091
---
9192

9293
Unfortunately this library has some limitation
93-
* It does not support private fields. Make sure your structs fields you intend to generate fake data for are public, it would otherwise trigger a panic. You can however omit fields using a tag skip `faker:"-"` on your private fields.
94-
* It does not support the `interface{}` data type. How could we generate anything without knowing its data type?
95-
* It does not support the `map[interface{}]interface{}`, `map[any_type]interface{}` & `map[interface{}]any_type` data types. Once again, we cannot generate values for an unknown data type.
96-
* Custom types are not fully supported. However some custom types are already supported: we are still investigating how to do this the correct way. For now, if you use `faker`, it's safer not to use any custom types in order to avoid panics.
97-
* Some extra custom types can be supported IF AND ONLY IF extended with [AddProvider()](https://github.com/bxcodec/faker/blob/9169c33ae9926e5b8f8732909790ee20b10b736a/faker.go#L320) please see [example](example_custom_faker_test.go#L46)
98-
* The `oneof` tag currently only supports `string`, the `int` types, and both `float32` & `float64`. Further support is coming soon (i.e. hex numbers, etc). See [example](example_with_tags_test.go#L53) for usage.
94+
95+
- It does not support private fields. Make sure your structs fields you intend to generate fake data for are public, it would otherwise trigger a panic. You can however omit fields using a tag skip `faker:"-"` on your private fields.
96+
- It does not support the `interface{}` data type. How could we generate anything without knowing its data type?
97+
- It does not support the `map[interface{}]interface{}`, `map[any_type]interface{}` & `map[interface{}]any_type` data types. Once again, we cannot generate values for an unknown data type.
98+
- Custom types are not fully supported. However some custom types are already supported: we are still investigating how to do this the correct way. For now, if you use `faker`, it's safer not to use any custom types in order to avoid panics.
99+
- Some extra custom types can be supported IF AND ONLY IF extended with [AddProvider()](https://github.com/bxcodec/faker/blob/9169c33ae9926e5b8f8732909790ee20b10b736a/faker.go#L320) please see [example](example_custom_faker_test.go#L46)
100+
- The `oneof` tag currently only supports `string`, the `int` types, and both `float32` & `float64`. Further support is coming soon (i.e. hex numbers, etc). See [example](example_with_tags_test.go#L53) for usage.
99101

100102
## Contribution
101103

address.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,16 @@ package faker
22

33
import (
44
"reflect"
5-
)
65

7-
var address Addresser
6+
"github.com/bxcodec/faker/v4/pkg/options"
7+
)
88

99
// GetAddress returns a new Addresser interface of Address
1010
func GetAddress() Addresser {
11-
mu.Lock()
12-
defer mu.Unlock()
13-
14-
if address == nil {
15-
address = &Address{}
16-
}
11+
address := &Address{}
1712
return address
1813
}
1914

20-
// SetAddress sets custom Address
21-
func SetAddress(net Addresser) {
22-
address = net
23-
}
24-
2515
// Addresser is logical layer for Address
2616
type Addresser interface {
2717
Latitude(v reflect.Value) (interface{}, error)
@@ -60,17 +50,17 @@ func (i Address) Longitude(v reflect.Value) (interface{}, error) {
6050
}
6151

6252
// Longitude get fake longitude randomly
63-
func Longitude() float64 {
53+
func Longitude(opts ...options.OptionFunc) float64 {
6454
return singleFakeData(LONGITUDE, func() interface{} {
6555
address := Address{}
6656
return float64(address.longitude())
67-
}).(float64)
57+
}, opts...).(float64)
6858
}
6959

7060
// Latitude get fake latitude randomly
71-
func Latitude() float64 {
61+
func Latitude(opts ...options.OptionFunc) float64 {
7262
return singleFakeData(LATITUDE, func() interface{} {
7363
address := Address{}
7464
return float64(address.latitude())
75-
}).(float64)
65+
}, opts...).(float64)
7666
}

address_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import (
44
"testing"
55
)
66

7-
func TestFakeData(t *testing.T) {
8-
SetAddress(Address{})
9-
}
10-
117
func TestGetLongitude(t *testing.T) {
128
long := Longitude()
139
if long > 180 || long < -180 {

datetime.go

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"reflect"
66
"time"
7+
8+
"github.com/bxcodec/faker/v4/pkg/options"
79
)
810

911
var century = []string{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX", "XXI"}
@@ -619,24 +621,12 @@ type DateTimer interface {
619621
TimePeriod(v reflect.Value) (interface{}, error)
620622
}
621623

622-
var date DateTimer
623-
624624
// GetDateTimer returns a new DateTimer interface of DateTime
625625
func GetDateTimer() DateTimer {
626-
mu.Lock()
627-
defer mu.Unlock()
628-
629-
if date == nil {
630-
date = &DateTime{}
631-
}
626+
date := &DateTime{}
632627
return date
633628
}
634629

635-
// SetDateTimer sets custom date time
636-
func SetDateTimer(d DateTimer) {
637-
date = d
638-
}
639-
640630
// DateTime struct
641631
type DateTime struct {
642632
}
@@ -659,11 +649,11 @@ func (d DateTime) UnixTime(v reflect.Value) (interface{}, error) {
659649
}
660650

661651
// UnixTime get unix time randomly
662-
func UnixTime() int64 {
652+
func UnixTime(opts ...options.OptionFunc) int64 {
663653
return singleFakeData(UnixTimeTag, func() interface{} {
664654
datetime := DateTime{}
665655
return datetime.unixtime()
666-
}).(int64)
656+
}, opts...).(int64)
667657
}
668658

669659
func (d DateTime) date() string {
@@ -676,11 +666,11 @@ func (d DateTime) Date(v reflect.Value) (interface{}, error) {
676666
}
677667

678668
// Date get fake date in string randomly
679-
func Date() string {
669+
func Date(opts ...options.OptionFunc) string {
680670
return singleFakeData(DATE, func() interface{} {
681671
datetime := DateTime{}
682672
return datetime.date()
683-
}).(string)
673+
}, opts...).(string)
684674
}
685675

686676
func (d DateTime) time() string {
@@ -693,11 +683,11 @@ func (d DateTime) Time(v reflect.Value) (interface{}, error) {
693683
}
694684

695685
// TimeString get time randomly in string format
696-
func TimeString() string {
686+
func TimeString(opts ...options.OptionFunc) string {
697687
return singleFakeData(TIME, func() interface{} {
698688
datetime := DateTime{}
699689
return datetime.time()
700-
}).(string)
690+
}, opts...).(string)
701691
}
702692

703693
func (d DateTime) monthName() string {
@@ -710,11 +700,11 @@ func (d DateTime) MonthName(v reflect.Value) (interface{}, error) {
710700
}
711701

712702
// MonthName get month name randomly in string format
713-
func MonthName() string {
703+
func MonthName(opts ...options.OptionFunc) string {
714704
return singleFakeData(MonthNameTag, func() interface{} {
715705
datetime := DateTime{}
716706
return datetime.monthName()
717-
}).(string)
707+
}, opts...).(string)
718708
}
719709

720710
func (d DateTime) year() string {
@@ -727,11 +717,11 @@ func (d DateTime) Year(v reflect.Value) (interface{}, error) {
727717
}
728718

729719
// YearString get year randomly in string format
730-
func YearString() string {
720+
func YearString(opts ...options.OptionFunc) string {
731721
return singleFakeData(YEAR, func() interface{} {
732722
datetime := DateTime{}
733723
return datetime.year()
734-
}).(string)
724+
}, opts...).(string)
735725
}
736726

737727
func (d DateTime) dayOfWeek() string {
@@ -744,11 +734,11 @@ func (d DateTime) DayOfWeek(v reflect.Value) (interface{}, error) {
744734
}
745735

746736
// DayOfWeek get day of week randomly in string format
747-
func DayOfWeek() string {
737+
func DayOfWeek(opts ...options.OptionFunc) string {
748738
return singleFakeData(DayOfWeekTag, func() interface{} {
749739
datetime := DateTime{}
750740
return datetime.dayOfWeek()
751-
}).(string)
741+
}, opts...).(string)
752742
}
753743

754744
func (d DateTime) dayOfMonth() string {
@@ -761,11 +751,11 @@ func (d DateTime) DayOfMonth(v reflect.Value) (interface{}, error) {
761751
}
762752

763753
// DayOfMonth get month randomly in string format
764-
func DayOfMonth() string {
754+
func DayOfMonth(opts ...options.OptionFunc) string {
765755
return singleFakeData(DayOfMonthTag, func() interface{} {
766756
datetime := DateTime{}
767757
return datetime.dayOfMonth()
768-
}).(string)
758+
}, opts...).(string)
769759
}
770760

771761
func (d DateTime) timestamp() string {
@@ -778,11 +768,11 @@ func (d DateTime) Timestamp(v reflect.Value) (interface{}, error) {
778768
}
779769

780770
// Timestamp get timestamp randomly in string format: 2006-01-02 15:04:05
781-
func Timestamp() string {
771+
func Timestamp(opts ...options.OptionFunc) string {
782772
return singleFakeData(TIMESTAMP, func() interface{} {
783773
datetime := DateTime{}
784774
return datetime.timestamp()
785-
}).(string)
775+
}, opts...).(string)
786776
}
787777

788778
func (d DateTime) century() string {
@@ -795,11 +785,11 @@ func (d DateTime) Century(v reflect.Value) (interface{}, error) {
795785
}
796786

797787
// Century get century randomly in string
798-
func Century() string {
788+
func Century(opts ...options.OptionFunc) string {
799789
return singleFakeData(CENTURY, func() interface{} {
800790
datetime := DateTime{}
801791
return datetime.century()
802-
}).(string)
792+
}, opts...).(string)
803793
}
804794

805795
func (d DateTime) timezone() string {
@@ -812,11 +802,11 @@ func (d DateTime) TimeZone(v reflect.Value) (interface{}, error) {
812802
}
813803

814804
// Timezone get timezone randomly in string
815-
func Timezone() string {
805+
func Timezone(opts ...options.OptionFunc) string {
816806
return singleFakeData(TIMEZONE, func() interface{} {
817807
datetime := DateTime{}
818808
return datetime.timezone()
819-
}).(string)
809+
}, opts...).(string)
820810
}
821811

822812
func (d DateTime) period() string {
@@ -829,11 +819,11 @@ func (d DateTime) TimePeriod(v reflect.Value) (interface{}, error) {
829819
}
830820

831821
// Timeperiod get timeperiod randomly in string (AM/PM)
832-
func Timeperiod() string {
822+
func Timeperiod(opts ...options.OptionFunc) string {
833823
return singleFakeData(TimePeriodTag, func() interface{} {
834824
datetime := DateTime{}
835825
return datetime.period()
836-
}).(string)
826+
}, opts...).(string)
837827
}
838828

839829
// RandomUnixTime is a helper function returning random Unix time

datetime_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ import (
66
"testing"
77
"time"
88

9-
"github.com/bxcodec/faker/v3/support/slice"
9+
"github.com/bxcodec/faker/v4/pkg/slice"
1010
)
1111

12-
func TestSetDateTimer(t *testing.T) {
13-
SetDateTimer(DateTime{})
14-
}
15-
1612
func TestUnixTimeValueValid(t *testing.T) {
1713
d := GetDateTimer()
1814
var ref = struct {

example_custom_faker_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"reflect"
66

7-
"github.com/bxcodec/faker/v3"
7+
"github.com/bxcodec/faker/v4"
88
)
99

1010
// Gondoruwo ...

0 commit comments

Comments
 (0)