Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
brianvoe committed Nov 26, 2023
2 parents b42894b + 5f00c22 commit c450282
Show file tree
Hide file tree
Showing 77 changed files with 2,068 additions and 565 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Expand Up @@ -10,7 +10,7 @@ jobs:
test:
strategy:
matrix:
go: [1.16, 1.17, 1.18, 1.19]
go: [1.18.*, 1.19.*, 1.20.*, 1.21.*]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}

Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Expand Up @@ -10,4 +10,4 @@
"coveredGutterStyle": "slashgreen",
"uncoveredGutterStyle": "slashred"
}
}
}
125 changes: 113 additions & 12 deletions README.md
Expand Up @@ -4,6 +4,8 @@

Random data generator written in go

[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/G2G0R5EJT)

<a href="https://www.buymeacoffee.com/brianvoe" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>

## Features
Expand All @@ -13,6 +15,7 @@ Random data generator written in go
- [Global Rand](#global-rand-set)
- [Struct Generator](#struct)
- [Custom Functions](#custom-functions)
- [Templates](#templates)
- [Http Server](https://github.com/brianvoe/gofakeit/tree/master/cmd/gofakeitserver)
- [Command Line Tool](https://github.com/brianvoe/gofakeit/tree/master/cmd/gofakeit)
- Zero dependencies
Expand Down Expand Up @@ -182,14 +185,14 @@ For example, this is useful when it is not possible to modify the struct that yo
// or just return a static value
type CustomString string

func (c *CustomString) Fake(faker *gofakeit.Faker) interface{} {
func (c *CustomString) Fake(faker *gofakeit.Faker) any {
return CustomString("my custom string")
}

// Imagine a CustomTime type that is needed to support a custom JSON Marshaler
type CustomTime time.Time

func (c *CustomTime) Fake(faker *gofakeit.Faker) interface{} {
func (c *CustomTime) Fake(faker *gofakeit.Faker) any {
return CustomTime(time.Now())
}

Expand Down Expand Up @@ -226,7 +229,7 @@ gofakeit.AddFuncLookup("friendname", gofakeit.Info{
Description: "Random friend name",
Example: "bill",
Output: "string",
Generate: func(r *rand.Rand, m *gofakeit.MapParams, info *gofakeit.Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *gofakeit.MapParams, info *gofakeit.Info) (any, error) {
return gofakeit.RandomString([]string{"bill", "bob", "sally"}), nil
},
})
Expand All @@ -240,7 +243,7 @@ gofakeit.AddFuncLookup("jumbleword", gofakeit.Info{
Params: []gofakeit.Param{
{Field: "word", Type: "string", Description: "Word you want to jumble"},
},
Generate: func(r *rand.Rand, m *gofakeit.MapParams, info *gofakeit.Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *gofakeit.MapParams, info *gofakeit.Info) (any, error) {
word, err := info.GetString(m, "word")
if err != nil {
return nil, err
Expand All @@ -263,13 +266,96 @@ fmt.Printf("%s", f.FriendName) // bill
fmt.Printf("%s", f.JumbleWord) // loredlowlh
```



## Templates

Generate custom outputs using golang's template engine [https://pkg.go.dev/text/template](https://pkg.go.dev/text/template).

We have added all the available functions to the template engine as well as some additional ones that are useful for template building.

Additional Available Functions
```go
- ToUpper(s string) string // Make string upper case
- ToLower(s string) string // Make string lower case
- ToString(s any) // Convert to string
- ToDate(s string) time.Time // Convert string to date
- SpliceAny(args ...any) []any // Build a slice of interfaces, used with Weighted
- SpliceString(args ...string) []string // Build a slice of strings, used with Teams and RandomString
- SpliceUInt(args ...uint) []uint // Build a slice of uint, used with Dice and RandomUint
- SpliceInt(args ...int) []int // Build a slice of int, used with RandomInt
```

<details>
<summary>Unavailable Gofakeit functions</summary>

```go
// Any functions that dont have a return value
- AnythingThatReturnsVoid(): void

// Not available to use in templates
- Template(co *TemplateOptions) ([]byte, error)
- RandomMapKey(mapI any) any
```
</details>


### Example Usages

```go
import "github.com/brianvoe/gofakeit/v6"

func main() {
// Accessing the Lines variable from within the template.
template := `
Subject: {{RandomString (SliceString "Greetings" "Hello" "Hi")}}
Dear {{LastName}},
{{RandomString (SliceString "Greetings!" "Hello there!" "Hi, how are you?")}}
{{Paragraph 1 5 10 "\n\n"}}
{{RandomString (SliceString "Warm regards" "Best wishes" "Sincerely")}}
{{$person:=Person}}
{{$person.FirstName}} {{$person.LastName}}
{{$person.Email}}
{{$person.Phone}}
`

value, err := gofakeit.Template(template, &TemplateOptions{Data: 5})

if err != nil {
fmt.Println(err)
}

fmt.Println(string(value))
}
```

Output:
```text
Subject: Hello
Dear Krajcik,
Greetings!
Quia voluptatem voluptatem voluptatem. Quia voluptatem voluptatem voluptatem. Quia voluptatem voluptatem voluptatem.
Warm regards
Kaitlyn Krajcik
kaitlynkrajcik@krajcik
570-245-7485
```

## Functions

All functions also exist as methods on the Faker struct

### File

Passing `nil` to `CSV`, `JSON` or `XML` it will auto generate data using a random set of generators.
Passing `nil` to `CSV`, `JSON` or `XML` will auto generate data using a random set of generators.

```go
CSV(co *CSVOptions) ([]byte, error)
Expand All @@ -279,6 +365,7 @@ FileExtension() string
FileMimeType() string
```


### Person

```go
Expand All @@ -302,9 +389,9 @@ Teams(peopleArray []string, teamsArray []string) map[string][]string
### Generate

```go
Struct(v interface{})
Slice(v interface{})
Map() map[string]interface{}
Struct(v any)
Slice(v any)
Map() map[string]any
Generate(value string) string
Regex(value string) string
```
Expand Down Expand Up @@ -489,9 +576,10 @@ Dessert() string
```go
Bool() bool
UUID() string
Weighted(options []any, weights []float32)
FlipACoin() string
RandomMapKey(mapI interface{}) interface{}
ShuffleAnySlice(v interface{})
RandomMapKey(mapI any) any
ShuffleAnySlice(v any)
```

### Colors
Expand Down Expand Up @@ -750,8 +838,6 @@ MovieGenre() string

### Error

Unlike most `gofakeit` methods which return a `string`, the error methods return a Go `error`. Access the error message as a string by chaining the `.Error()` method.

```go
Error() error
ErrorDatabase() error
Expand All @@ -762,3 +848,18 @@ ErrorHTTPServer() error
ErrorInput() error
ErrorRuntime() error
```

### School

```go
school() string
```

## Template

```go
Template(co *TemplateOptions) (string, error) // Generates custom documents
Markdown(co *MarkdownOptions) (string, error) // Generates markdown documents
EmailText(co *EmailOptions) (string, error) // Generates email documents
FixedWidth(co *FixedWidthOptions) (string, error) // Generates fixed width documents
```
32 changes: 16 additions & 16 deletions address.go
Expand Up @@ -211,7 +211,7 @@ func addAddressLookup() {
}`,
Output: "map[string]interface",
ContentType: "application/json",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return address(r), nil
},
})
Expand All @@ -222,7 +222,7 @@ func addAddressLookup() {
Description: "Random city",
Example: "Marcelside",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return city(r), nil
},
})
Expand All @@ -233,7 +233,7 @@ func addAddressLookup() {
Description: "Random country",
Example: "United States of America",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return country(r), nil
},
})
Expand All @@ -244,7 +244,7 @@ func addAddressLookup() {
Description: "Random 2 digit country abbreviation",
Example: "US",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return countryAbr(r), nil
},
})
Expand All @@ -255,7 +255,7 @@ func addAddressLookup() {
Description: "Random state",
Example: "Illinois",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return state(r), nil
},
})
Expand All @@ -266,7 +266,7 @@ func addAddressLookup() {
Description: "Random 2 digit state abbreviation",
Example: "IL",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return stateAbr(r), nil
},
})
Expand All @@ -277,7 +277,7 @@ func addAddressLookup() {
Description: "Random full street",
Example: "364 East Rapidsborough",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return street(r), nil
},
})
Expand All @@ -288,7 +288,7 @@ func addAddressLookup() {
Description: "Random street name",
Example: "View",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return streetName(r), nil
},
})
Expand All @@ -299,7 +299,7 @@ func addAddressLookup() {
Description: "Random street number",
Example: "13645",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return streetNumber(r), nil
},
})
Expand All @@ -310,7 +310,7 @@ func addAddressLookup() {
Description: "Random street prefix",
Example: "Lake",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return streetPrefix(r), nil
},
})
Expand All @@ -321,7 +321,7 @@ func addAddressLookup() {
Description: "Random street suffix",
Example: "land",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return streetSuffix(r), nil
},
})
Expand All @@ -332,7 +332,7 @@ func addAddressLookup() {
Description: "Random street zip",
Example: "13645",
Output: "string",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return zip(r), nil
},
})
Expand All @@ -343,7 +343,7 @@ func addAddressLookup() {
Description: "Random latitude",
Example: "-73.534056",
Output: "float",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return latitude(r), nil
},
})
Expand All @@ -358,7 +358,7 @@ func addAddressLookup() {
{Field: "min", Display: "Min", Type: "float", Default: "0", Description: "Minimum range"},
{Field: "max", Display: "Max", Type: "float", Default: "90", Description: "Maximum range"},
},
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
min, err := info.GetFloat64(m, "min")
if err != nil {
return nil, err
Expand All @@ -384,7 +384,7 @@ func addAddressLookup() {
Description: "Random longitude",
Example: "-147.068112",
Output: "float",
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
return longitude(r), nil
},
})
Expand All @@ -399,7 +399,7 @@ func addAddressLookup() {
{Field: "min", Display: "Min", Type: "float", Default: "0", Description: "Minimum range"},
{Field: "max", Display: "Max", Type: "float", Default: "180", Description: "Maximum range"},
},
Generate: func(r *rand.Rand, m *MapParams, info *Info) (interface{}, error) {
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
min, err := info.GetFloat64(m, "min")
if err != nil {
return nil, err
Expand Down

0 comments on commit c450282

Please sign in to comment.