All Projects → pioz → faker

pioz / faker

Licence: MIT license
Random fake data and struct generator for Go.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to faker

random
Random data generator AKA faker
Stars: ✭ 14 (-79.1%)
Mutual labels:  random, faker
Fako
Struct Faker for Go
Stars: ✭ 329 (+391.04%)
Mutual labels:  faker, struct
Faker
Go (Golang) Fake Data Generator for Struct
Stars: ✭ 1,698 (+2434.33%)
Mutual labels:  faker, struct
fastfaker
Fast, concurrent fake data generator for any structure or type.
Stars: ✭ 21 (-68.66%)
Mutual labels:  random, faker
Random
Generate random strings or numeric values
Stars: ✭ 68 (+1.49%)
Mutual labels:  random, faker
randomdata
TYPO3 extensions to generate new random data or replace existing data with random data
Stars: ✭ 14 (-79.1%)
Mutual labels:  random, faker
Foundry
A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.
Stars: ✭ 216 (+222.39%)
Mutual labels:  factory, faker
jRand
A Java library to generate random data for all sorts of things. Java random data faker
Stars: ✭ 27 (-59.7%)
Mutual labels:  random, faker
msp430-rng
Random (SLAA338) and pseudorandom (LCG) number generation.
Stars: ✭ 19 (-71.64%)
Mutual labels:  random
interface-forge
Graceful mock-data and fixtures generation using TypeScript
Stars: ✭ 58 (-13.43%)
Mutual labels:  factory
alokmenghrajani.github.com
Alok Menghrajani's Blog
Stars: ✭ 64 (-4.48%)
Mutual labels:  random
mockingbird
🐦 Decorator Powered TypeScript Library for Creating Mocks
Stars: ✭ 70 (+4.48%)
Mutual labels:  faker
pydantic-factories
Simple and powerful mock data generation using pydantic or dataclasses
Stars: ✭ 380 (+467.16%)
Mutual labels:  factory
rango
Random. Django. Rango. An introduction to using Python and Django to build a website.
Stars: ✭ 53 (-20.9%)
Mutual labels:  random
SDETools
Matlab Toolbox for the Numerical Solution of Stochastic Differential Equations
Stars: ✭ 80 (+19.4%)
Mutual labels:  random
fake-numbers
Generate fake, valid numbers. Check if a number is valid. Support a lot of different numbers: Credit card, EAN, ISBN, RTN, VIN, etc.
Stars: ✭ 51 (-23.88%)
Mutual labels:  faker
this-is-your-life
An angular app character backstories based on the Xanathar's Guide to Everything 'This Is Your Life' tables.
Stars: ✭ 36 (-46.27%)
Mutual labels:  random
order-id
Unique order id generator
Stars: ✭ 46 (-31.34%)
Mutual labels:  random
Fluent-Random-Picker
Fluent Random Picker is a nice, performant, fluent way to pick random values. Probabilities can be specified, values can be weighted.
Stars: ✭ 26 (-61.19%)
Mutual labels:  random
sfmt-erlang
sfmt-erlang: SIMD-oriented Fast Mersenne Twister (SFMT) for Erlang
Stars: ✭ 70 (+4.48%)
Mutual labels:  random

Faker

Go Go Report Card codecov awesome-go GoReference

Random fake data and struct generator for Go.

  • More than 100 generator functions
  • Struct generator
  • Unique data generator
  • Builtin types support
  • Easily customizable
  • Zero dependencies
  • Recursive infinite loop detector
  • Benchmarks (coming soon)

Installation

go get github.com/pioz/faker

Example

faker.SetSeed(623)

fmt.Println(faker.Username())
fmt.Println(faker.String())
fmt.Println(faker.IntInRange(1, 10))
fmt.Println(faker.CurrencyName())
// Output: spicule
// gzaazJyRGt3jDnVh6ik7R9FO0AU1HcOzdOXbmNVBRQ5pq8n9tHf9B21PIFozLEzsoY4wILvZjTxSLQmD3UOAamDgVR411T3YHleDTgLuz90XSO3NFZm1AnaJiJamVRcNGD2zmi4qWkcjKF3E4JKgn1DiCeC3eSb5WELsw8XqRzlvJqG
// 10
// Myanmar Kyat

Please refer to the godoc for all available functions and more.

Struct Builder Example

faker.SetSeed(622)

// Define a new builder
colorBuilder := func(params ...string) (interface{}, error) {
  return faker.Pick("Red", "Yellow", "Blue", "Black", "White"), nil
}

// Register a new builder named "color" for string type
err := faker.RegisterBuilder("color", "string", colorBuilder)
if err != nil {
  panic(err)
}

type Animal struct {
  Name  string `faker:"username"`
  Color string `faker:"color"` // Use custom color builder
}

type Person struct {
  FirstName string            `faker:"firstName"`         // Any available function case insensitive
  LastName  *string           `faker:"lastName"`          // Pointer are also supported
  Age       int               `faker:"intinrange(0,120)"` // Can call with parameters
  UUID      string            `faker:"uuid;unique"`       // Guarantees a unique value
  Number    int               `faker:"-"`                 // Skip this field
  Code      string            // No tag to use default builder for this field type
  Pet       Animal            // Recursively fill this struct
  Nicknames []string          `faker:"username;len=3"`          // Build an array of size 3 using faker.Username function
  Extra     map[string]string `faker:"stringWithSize(3);len=2"` // map are supported
}

p := Person{}
err = faker.Build(&p)
if err != nil {
  panic(err)
}
fmt.Println(p.FirstName)
fmt.Println(*p.LastName)
fmt.Println(p.Age)
fmt.Println(p.UUID)
fmt.Println(p.Number)
fmt.Println(p.Code)
fmt.Println(p.Pet.Name)
fmt.Println(p.Pet.Color)
fmt.Println(len(p.Nicknames))
fmt.Println(p.Nicknames[0])
fmt.Println(p.Nicknames[1])
fmt.Println(p.Nicknames[2])
fmt.Println(p.Extra)
// Output: Wilber
// Gutkowski
// 25
// ff8d6917-b920-46e6-b1be-dc2d48becfcb
// 0
// z
// honegger
// Red
// 3
// teagan
// polypeptide
// chinfest
// map[70w:3F6 gQS:isq]

Factory

One of the nice things about Faker is that it can also be used as a factory library. In fact when we call the faker.Build function if a value is not zero then it is not modified, leaving the original value. This allows you to create factory functions very easily:

faker.SetSeed(623)

type User struct {
    Username string `faker:"username"`
    Email    string `faker:"email"`
    Country  string `faker:"CountryAlpha2"`
}

italianUserFactory := func() *User {
    u := &User{Country: "IT"}
    faker.Build(u)
    return u
}

italianUser := italianUserFactory()
fmt.Println(italianUser)
// Output: &{spicule [email protected] IT}

Customization

A builder is a variadic function that will take an arbitrary number of strings as arguments and return an interface or an error. This function (builder) can be used to generate fake data and customize the behaviour of Faker. Here an example:

faker.SetSeed(1802)

// Define a new builder
builder := func(params ...string) (interface{}, error) {
    if len(params) > 0 && params[0] == "melee" {
        return faker.Pick("Barbarian", "Bard", "Fighter", "Monk", "Paladin", "Rogue"), nil
    }
    return faker.Pick("Cleric", "Druid", "Ranger", "Sorcerer", "Warlock", "Wizard"), nil
}

// Register a new builder named "dndClass" for string type
err := faker.RegisterBuilder("dndClass", "string", builder)
if err != nil {
    panic(err)
}

player := &struct {
    Class string `faker:"dndClass(melee)"`
    // other fields ...
}{}

// Build a struct with fake data
faker.Build(&player)

fmt.Println(player.Class)
// Output: Paladin

Contributing with new generator functions

If you want to contribute to faker with new generator functions please read this wiki!

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/pioz/faker/issues.

License

The package is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].