fake

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2022 License: MIT Imports: 16 Imported by: 0

README

Fake

Fake is a Go package that generates fake data for you to help with development. It tris to give a simple API to access anywhere.

codecov Go Report Card License GoDoc Go.Dev

Coming from a PHP background, I like the way PHP Faker works and how easy it is to generate data such as first and last names, emails, domains, texts, etc. to bootstrap databases for development, stress-test persistence layer, etc. So this Faker is heavily based on the same API provided by PHP Faker.

Table of Contents

Installation

You can simply add the package github.com/rmsj/fake to you import section and run go mod tidy

It's still early days in development so API might change. To upgrade, or downgrade the dependency, run go get:

go get github.com/rmsj/[email protected]

Using the appropriate version number you want.

Basic Usage

Fake API, as of version 0.0.5 has data generation capability and providers for person, internet, text, lotem ipsum text and DNA sequences.

  • PersonProvider
    • for first and last names, gender, etc.
  • InternetProvider
    • for email, domain names, user names, urls, etc.
  • TextProvider
    • for "real sentences" randomized from Alice in Wonder Land.
  • DNAProvider
    • for generation of random fake DNA sequences of n length.
  • CompanyProvider
    • for generation of random fake company data.
  • ImageProvider
  • Lorem
    • generates lorem ipsum texts.
package main

import (
	"fmt"
	"github.com/rmsj/fake"
)

func main() {

	f, err := fake.New()
	if err != nil {
		panic(err)
	}

	// print random first name
	fmt.Println(f.FirstName())

	// prints random email
	fmt.Println(f.Email())
}

Data Factory

To create multiple data fo fill in databases for development for example, you can use the Factory function. The Factory function requires a specific function type (type Builder func() interface{}) and an int as second parameter - being the amount of types the operation should be repeated.

package main

import (
	"fmt"
	"github.com/rmsj/fake"
)

type user struct {
	firstName string
	lastName  string
	email     string
}

func main() {

	f, err := fake.New()
	if err != nil {
		panic(err)
	}

	builder := func() interface{} {
		return user{
			firstName: f.FirstName(),
			lastName:  f.LastName(),
			email:     f.Email(),
		}
	}

	users := f.Factory(builder, 10)
	for _, v := range users {
		u, ok := v.(user)
		if !ok {
			panic("this should not happen")
		}

		// you can use the user value as normal
		fmt.Println(u.firstName)
		fmt.Println(u.email)
	}
	
	// or for more predictable values, with deterministic mode
    f.Determistix(42)

    sameUsers := f.Factory(builder, 10)
    for _, v := range sameUsers {
      u, ok := v.(user)
      if !ok {
        panic("this should not happen")
      }
  
      // all the generated users should have the same name and email
      fmt.Println(u.firstName)
      fmt.Println(u.email)
    }
	
}

Adding Your Own Providers

The number of providers will grow over time and the idea is that you can change a specific provider by implementing the required interface with a different set of data - to have more control of what data is generated, change language, etc.

So you could implement the PersonProvider interface to have portuguese names, for example, and the basic usabe example above would look like:

package pt_provider


type PortuguesePersonProvider struct {}


func (p PortuguesePersonProvider) FirstNames() []string {
	return []string{"Manoel", "Pedro"}
}

// rest of implementation for all required methods on interface PersonProvider...
package main

import (
	"fmt"
	"github.com/rmsj/fake"

	"github.com/user/project/pt_provider"
)

func main() {

	f, err := fake.New()
	if err != nil {
		panic(err)
	}
	
	f.SetPersonProvider(pt_provider)

	// print random first name from your list of names
	fmt.Println(f.FirstName())
}

Work In Progress

The package is a work in progress as I'm slowly adding more providers and related data generation from them.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InArray added in v0.0.3

func InArray(val interface{}, array interface{}) int

InArray checks if a given value exists in a specific array and returns the index or -1 if not found

Types

type Builder

type Builder func() any

Builder is a function type that returns anything

type CompanyProvider added in v0.0.3

type CompanyProvider interface {
	NameFormats() []string
	Suffixes() []string
	CatchWords() [][]string
	BuzzWords() [][]string
	JobTitles() []string
	EIN(rand1, rand2 int) string
}

CompanyProvider must be implemented to provide all data for company related fake data generation

type DNAProvider

type DNAProvider interface {
	Set() []string
}

DNAProvider must be implemented to provide different sets of data for DNA sequence generation

type Fake

type Fake struct {
	// contains filtered or unexported fields
}

Fake is the main type for faking data

func New

func New() (Fake, error)

New constructs an instance of Faker and returns it

func (Fake) BuzzPhrase added in v0.0.3

func (f Fake) BuzzPhrase() string

BuzzPhrase generates random phrase with buzz words

func (Fake) CatchPhrase added in v0.0.3

func (f Fake) CatchPhrase() string

CatchPhrase builds a random catch phrase from source

func (Fake) ChangeDNAProvider

func (f Fake) ChangeDNAProvider(d DNAProvider)

ChangeDNAProvider changes the data provider for DNA sequence related fake data generation

func (Fake) ChangeInternetProvider

func (f Fake) ChangeInternetProvider(i InternetProvider)

ChangeInternetProvider changes the data provider for internet related fake data generation

func (Fake) ChangeLoremProvider

func (f Fake) ChangeLoremProvider(l LoremProvider)

ChangeLoremProvider changes the data provider for lorem (dummy text) related fake data generation

func (Fake) ChangePersonProvider

func (f Fake) ChangePersonProvider(p PersonProvider)

ChangePersonProvider changes the data provider for person related fake data generation

func (Fake) ChangeTextProvider

func (f Fake) ChangeTextProvider(t TextProvider)

ChangeTextProvider changes the data provider for text related fake data generation

func (Fake) Company added in v0.0.3

func (f Fake) Company() string

Company generates a fake random company name and returns it

func (Fake) CompanyEmail

func (f Fake) CompanyEmail() string

CompanyEmail returns a random "company" email address

func (Fake) DNASequence

func (f Fake) DNASequence(len int) (string, error)

DNASequence generates a simplified random DNA sequence of the given len

func (*Fake) Deterministic added in v0.0.4

func (f *Fake) Deterministic(seed int64)

Deterministic will make the generated values constant until a call to Nondeterministic

func (Fake) DomainName

func (f Fake) DomainName() string

DomainName provides a random domain name

func (Fake) EIN added in v0.0.3

func (f Fake) EIN() string

EIN creates a random Employer Identification Number

func (Fake) Email

func (f Fake) Email() string

Email returns a random email address

func (Fake) Factory

func (f Fake) Factory(builder Builder, n int) []any

Factory builds N number of

func (Fake) FirstName

func (f Fake) FirstName() string

FirstName gets a first name randomly

func (Fake) FirstNameFemale

func (f Fake) FirstNameFemale() string

FirstNameFemale returns a random female first name

func (Fake) FirstNameMale

func (f Fake) FirstNameMale() string

FirstNameMale gets a first name male randomly

func (Fake) FreeEmail

func (f Fake) FreeEmail() string

FreeEmail returns a random free email address

func (Fake) Gender

func (f Fake) Gender() string

Gender returns a random gender

func (Fake) IPv4

func (f Fake) IPv4() string

IPv4 generates a IP v4

func (Fake) IPv6

func (f Fake) IPv6() string

IPv6 generates random IPv6 address

func (Fake) Image added in v0.0.3

func (f Fake) Image(img ImageSettings) (image.Image, error)

Image returns a random image.Image

func (Fake) ImageUrl added in v0.0.3

func (f Fake) ImageUrl(img ImageSettings) (string, error)

ImageUrl returns a random image url

func (Fake) JobTitle added in v0.0.3

func (f Fake) JobTitle() string

JobTitle returns a random job title from data source

func (Fake) LastName

func (f Fake) LastName() string

LastName get fake lastname

func (Fake) MacAddress

func (f Fake) MacAddress() string

MacAddress get mac address randomly in string

func (Fake) Name

func (f Fake) Name() string

Name returns a random full name with title

func (*Fake) Nondeterministic added in v0.0.4

func (f *Fake) Nondeterministic()

Nondeterministic reset the seed to it's default randomness for each generated value

func (Fake) Paragraph

func (f Fake) Paragraph(sentenceCount int) string

Paragraph returns a random paragraph

func (Fake) RealText

func (f Fake) RealText(maxWordCount int, prefixLen int) (string, error)

RealText generates a text string by the Markov chain algorithm.

Depending on the maxWordCount, returns a random valid looking text. The algorithm generates a weighted table with the specified number of words as the index and the possible following words as the value.// @example 'Alice, swallowing down her flamingo, and began by taking the little golden key' maxWordCount is the maximum number of characters the text should contain (minimum: 10) prefixLen determines how many words are considered for the generation of the next word. The minimum is 1, and it produces a higher level of randomness, although the generated text usually doesn't make sense. Higher index sizes (up to 5) produce more correct text, at the price of less randomness. @return string

func (Fake) SafeEmail

func (f Fake) SafeEmail() string

SafeEmail returns a random email address from a safe domain

func (Fake) Sentence

func (f Fake) Sentence(wordCount int) string

Sentence returns a number of random words as a sentence

func (Fake) Suffix

func (f Fake) Suffix() string

Suffix returns a Fake name suffix

func (Fake) Title

func (f Fake) Title() string

Title returns a Fake title

func (Fake) TitleFemale

func (f Fake) TitleFemale() string

TitleFemale get a title female randomly

func (Fake) TitleMale

func (f Fake) TitleMale() string

TitleMale get a title male randomly

func (Fake) Url

func (f Fake) Url() string

Url provides a random URL

func (Fake) Username

func (f Fake) Username() string

Username returns a random username

func (Fake) Word

func (f Fake) Word() string

Word returns a random word

type ImageProvider added in v0.0.3

type ImageProvider interface {
	// ImgSource default is lorempixel.com
	ImgSource() string
	// Categories is the list of valid categories for the default image source - LoremPixel
	Categories() []string
}

ImageProvider must be implemented by types that wants to provide data source for images.

type ImageSettings added in v0.0.3

type ImageSettings struct {
	Width    int
	Height   int
	Category string
}

ImageSettings provide settings for random image

type InternetProvider

type InternetProvider interface {
	FreeEmailDomains() []string
	SafeEmailDomains() []string
	Tld() []string
	UserNameFormats() []string
	EmailFormats() []string
	UrlFormats() []string
}

InternetProvider must be implemented by types that wants to provide data source for emails, URLs, etc.

type LoremProvider

type LoremProvider interface {
	Words() []string
}

LoremProvider must be implemented by types that wants to provide data source for emails, URLs, etc.

type PersonProvider

type PersonProvider interface {
	TitlesMale() []string
	TitlesFemale() []string
	FirstNames() []string
	FirstNamesMale() []string
	FirstNamesFemale() []string
	LastNames() []string
	Genders() []string
	Suffixes() []string
}

PersonProvider must be implemented by types that wants to provide data source for names, etc.

type TextProvider

type TextProvider interface {
	Source() string
}

TextProvider must be implemented by types that wants to provide data source for texts

Directories

Path Synopsis
internal
Package tests contains supporting code for running tests.
Package tests contains supporting code for running tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL