faker

package module
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2018 License: MIT Imports: 10 Imported by: 60

README

Docs

faker

Struct Data Fake Generator

Faker will generate you a fake data based on your Struct.

Build Status codecov Go Report Card License GoDoc

Index

Support

You can file an Issue. See documentation in Godoc

Getting Started

Download
go get -u github.com/bxcodec/faker

Example

With Tag

Supported tag:

Internet :

  • Email
  • Mac address
  • Domain name
  • Url
  • UserName
  • IP Address (IPV4 IPV6 )
  • Password

Payment :

  • Credit Card Type (VISA, MASTERCARD , AMERICAN EXPRESS ,DISCOVER)
  • Credit Card Number

Address :

  • Latitude and Longitude

Phone :

  • Phone number
  • Toll free phone number
  • E164PhoneNumber

Person :

  • Title male
  • Title female
  • FirstName male
  • FirstName female
  • LastName
  • Name

DateTime :

  • UnixTime
  • Date
  • Time
  • MonthName
  • Year
  • DayOfWeek
  • DayOfMonth
  • Timestamp
  • Century
  • TimeZone
  • TimePeriod

Lorem :

  • Word
  • Sentence
  • Sentences

package main

import (
	"fmt"
	"github.com/bxcodec/faker"
)

type SomeStruct struct {
 Latitude         float32 `faker:"lat"`
 Long             float32 `faker:"long"`
 CreditCardType   string  `faker:"cc_type"`
 CreditCardNumber string  `faker:"cc_number"`
 Email            string  `faker:"email"`
 IPV4             string  `faker:"ipv4"`
 IPV6             string  `faker:"ipv6"`
 Password         string  `faker:"password"` 
 PhoneNumber      string  `faker:"phone_number"`
 MacAddress       string  `faker:"mac_address"`
 Url              string  `faker:"url"`
 UserName         string  `faker:"username"`
 ToolFreeNumber   string  `faker:"tool_free_number"`
 E164PhoneNumber  string  `faker:"e_164_phone_number"`
 TitleMale        string  `faker:"title_male"`
 TitleFemale      string  `faker:"title_female"`
 FirstNameMale    string  `faker:"first_name_male"`
 FirstNameFemale  string  `faker:"first_name_female"`
 LastName         string  `faker:"last_name"`
 Name             string  `faker:"name"`
 UnixTime         int64   `faker:"unix_time"`
 Date             string  `faker:"date"`
 Time             string  `faker:"time"`
 MonthName        string  `faker:"month_name"`
 Year             string  `faker:"year"`
 DayOfWeek        string  `faker:"day_of_week"`
 DayOfMonth       string  `faker:"day_of_month"`
 Timestamp        string  `faker:"timestamp"`
 Century          string  `faker:"century"`
 TimeZone         string  `faker:"timezone"`
 TimePeriod       string  `faker:"time_period"`
 Word             string  `faker:"word"`
 Sentence         string  `faker:"sentence"`
 Sentences        string  `faker:"sentences"`
}

func main() {

  a= SomeStruct{}
  err:= faker.FakeData(&a)
  if err!= nil {
    fmt.Println(err)
  }
  fmt.Printf("%+v", a)
	//Will Print  :
	 /* Latitude: -74.209991,
        Long: -4.394531,
        CreditCardNumber: 376243757700558,
        CreditCardType: american express,
        Email: QjTIcmn@lmErh.info,
        IPV4: 80.206.109.93,
        IPV6: 448f:a391:90c6:d165:1d1f:b536:45f7:f084,
	    Password: FhOSWpnzXwxxnCTlZJcZeBTUeRaVidThUdntQryyjNiAFwbyCe,
        PhoneNumber: 865-321-1047,
        MacAddress: 46:77:6f:bb:d0:fc,
        Url: https://sFUBEIj.info/kYvcpYL.html,
        UserName: VwrVhzZ,
        ToolFreeNumber: (777) 372-148965,
        E164PhoneNumber: +765101283947,
        TitleMale: Prof.,
        TitleFemale: Dr.,
        FirstNameMale: Charley,
        FirstNameFemale: Freeda,
        LastName: Runte,
        Name: Dr. Freeda Runte,
        UnixTime: 679631083,
        Date: 2010-06-12,
        Time: 11:33:24,
        MonthName: January,
        Year: 1982,
        DayOfWeek: Sunday,
        DayOfMonth: 26,
        Timestamp: 1972-06-24 14:03:32,
        Century: XV,
        TimeZone: Navajo,
        TimePeriod: AM,
	    Word: consectetur,
        Sentence: Perferendis aut sit accusantium consequatur voluptatem .,
        Sentences: Perferendis sit consequatur accusantium aut voluptatem .Aut voluptatem consequatur sit perferendis accusantium .Aut consequatur sit accusantium perferendis voluptatem .,
	 */
}

Without Tag

package main

import (
	"fmt"
	"github.com/bxcodec/faker"
)

type SomeStruct struct {
	Int      int
	Int8     int8
	Int16    int16
	Int32    int32
	Int64    int64
	String   string
	Bool     bool
	SString  []string
	SInt     []int
	SInt8    []int8
	SInt16   []int16
	SInt32   []int32
	SInt64   []int64
	SFloat32 []float32
	SFloat64 []float64
	SBool    []bool
	Struct   AStruct
}
type AStruct struct {
	Number        int64
	Height        int64
	AnotherStruct BStruct
}

type BStruct struct {
	Image string
}

func main() {

  a := SomeStruct{}
  err:= faker.FakeData(&a)
  if err!= nil {
    fmt.Println(err)
  }
  fmt.Printf("%+v", a)
}

Output :

{Int:7088293148785081331 Int8:7 Int16:14 Int32:1777976883 Int64:2467854463682814928 String:XMhCTmwvVqEUryIKnpWrQmBdb Bool:true SString:[iiCGZ GESVVaP] SInt:[2391903971675293806 5270400206229440165 7315288441301820955] SInt8:[124 104 84] SInt16:[-9403 -23327 -19174] SInt32:[1714966339 1617248797 1233525792] SInt64:[6505581000035730776 989945489581839946 7467254172609770414] SFloat32:[0.6761954 0.13427323 0.35608092] SFloat64:[0.49714054026277343 0.29188223737765046 0.7800285978504301] SBool:[true true true] Struct:{Number:8662858647992239649 Height:2466984558238338402 AnotherStruct:{Image:kNIwoxPiVcOqQxBUyyAuDAKom}}}

Example to use Faker

Bench To Generate Fake Data

Without Tag
BenchmarkFakerDataNOTTagged-4             500000              3049 ns/op             488 B/op         20 allocs/op
Using Tag
 BenchmarkFakerDataTagged-4                100000             17470 ns/op             380 B/op         26 allocs/op
MUST KNOW

The Struct Field must PUBLIC.
Support Only For :

  • int int8 int16 int32 int64
  • []int []int8 []int16 []int32 []int64
  • bool []bool
  • string []string
  • float32 float64 []float32 []float64
  • Nested Struct Field
  • time.Time []time.Time

Limitation

Unfortunately this library has some limitation

  • Not support for private field. Just make sure your field's struct is public. If not, it will throw panic error.
  • Not support for interface{} data type. How we can generate if we don't know what is the data type?
  • Not support for map[interface{}]interface{}, map[any_type]interface{}, map[interface{}]any_type. Still, it's about interface. We can't give you something if we don't know what really you want.

Contribution

To contrib on this project, you can make a PR or just an issue.

Maintainer

Documentation

Index

Constants

View Source
const (
	BaseDate   = "2006-01-02"
	Time       = "15:04:05"
	Month      = "January"
	Year       = "2006"
	Day        = "Sunday"
	DayOfMonth = "_2"
	TimePeriod = "PM"
)
View Source
const (
	Email              = "email"
	MacAddress         = "mac_address"
	DomainName         = "domain_name"
	UserName           = "username"
	Url                = "url"
	IPV4               = "ipv4"
	IPV6               = "ipv6"
	PASSWORD           = "password"
	LATITUDE           = "lat"
	LONGITUDE          = "long"
	CREDIT_CARD_NUMBER = "cc_number"
	CREDIT_CARD_TYPE   = "cc_type"
	PHONE_NUMBER       = "phone_number"
	TOLL_FREE_NUMBER   = "tool_free_number"
	E164_PHONE_NUMBER  = "e_164_phone_number"
	TITLE_MALE         = "title_male"
	TITLE_FEMALE       = "title_female"
	FIRST_NAME_MALE    = "first_name_male"
	FIRST_NAME_FEMALE  = "first_name_female"
	LAST_NAME          = "last_name"
	NAME               = "name"
	UNIX_TIME          = "unix_time"
	DATE               = "date"
	TIME               = "time"
	MONTH_NAME         = "month_name"
	YEAR               = "year"
	DAY_OF_WEEK        = "day_of_week"
	DAY_OF_MONTH       = "day_of_month"
	TIMESTAMP          = "timestamp"
	CENTURY            = "century"
	TIMEZONE           = "timezone"
	TIME_PERIOD        = "time_period"
	WORD               = "word"
	SENTENCE           = "sentence"
	SENTENCES          = "sentences"
)

Variables

View Source
var ErrMoreArguments = "Passed more arguments than is possible : (%d)"

Error when passed more arguments

View Source
var ErrNotSupportedPointer = "Use sample:=new(%s)\n faker.FakeData(sample) instead"
View Source
var ErrTagNotSupported = "Tag unsupported"

Error when tag not supported

View Source
var ErrUnsupportedKind = "Unsupported kind: %s"

Error when pass unsupported kind

View Source
var ErrUnsupportedKindPtr = "Unsupported kind: %s Change Without using * (pointer) in Field of %s"

Error when get fake from ptr

View Source
var ErrValueNotPtr = "Not a pointer value"

Error when value is not pointer

Functions

func FakeData

func FakeData(a interface{}) error

FakeData is the main function. Will generate a fake data based on your struct. You can use this for automation testing, or anything that need automated data. You don't need to Create your own data for your testing.

func RandomInt added in v1.3.0

func RandomInt(parameters ...int) (p []int, err error)

* / Get three parameters , only first mandatory and the rest are optional / --- If only set one parameter : This means the minimum number of digits and the total number / --- If only set two parameters : First this is min digit and second max digit and the total number the difference between them / --- If only three parameters: the third argument set Max count Digit

func RandomUnixTime added in v1.3.0

func RandomUnixTime() int64

helper function

func SetAddress added in v1.3.0

func SetAddress(net Addresser)

this set custom Address

func SetDataFaker added in v1.3.0

func SetDataFaker(d DataFaker)

func SetDateTimer added in v1.3.0

func SetDateTimer(d DateTimer)

func SetDowser added in v1.3.0

func SetDowser(d Dowser)

this set custom Address

func SetNetwork added in v1.3.0

func SetNetwork(net Networker)

this set custom Network

func SetPayment added in v1.3.0

func SetPayment(p Render)

this set custom Network

func SetPhoner added in v1.3.0

func SetPhoner(p Phoner)

this set custom Phoner

Types

type Address added in v1.3.0

type Address struct{}

func (Address) Latitude added in v1.3.0

func (i Address) Latitude(v reflect.Value) error

func (Address) Longitude added in v1.3.0

func (i Address) Longitude(v reflect.Value) error

type Addresser added in v1.3.0

type Addresser interface {
	Latitude(v reflect.Value) error
	Longitude(v reflect.Value) error
}

func GetAddress added in v1.3.0

func GetAddress() Addresser

Constructor

type DataFaker added in v1.3.0

type DataFaker interface {
	Word() string
	Sentence() string
	Sentences() string
}

func GetLorem added in v1.3.0

func GetLorem() DataFaker

Constructor

type DateTime added in v1.3.0

type DateTime struct {
}

func (DateTime) Century added in v1.3.0

func (d DateTime) Century() string

func (DateTime) Date added in v1.3.0

func (d DateTime) Date() string

format example BaseDate const

func (DateTime) DayOfMonth added in v1.3.0

func (d DateTime) DayOfMonth() string

func (DateTime) DayOfWeek added in v1.3.0

func (d DateTime) DayOfWeek() string

func (DateTime) MonthName added in v1.3.0

func (d DateTime) MonthName() string

func (DateTime) Time added in v1.3.0

func (d DateTime) Time() string

func (DateTime) TimePeriod added in v1.3.0

func (d DateTime) TimePeriod() string

func (DateTime) TimeZone added in v1.3.0

func (d DateTime) TimeZone() string

func (DateTime) Timestamp added in v1.3.0

func (d DateTime) Timestamp() string

func (DateTime) UnixTime added in v1.3.0

func (d DateTime) UnixTime(v reflect.Value) error

get unix time

func (DateTime) Year added in v1.3.0

func (d DateTime) Year() string

type DateTimer added in v1.3.0

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
}

func GetDateTimer added in v1.3.0

func GetDateTimer() DateTimer

type Dowser added in v1.3.0

type Dowser interface {
	TitleMale() string
	TitleFeMale() string
	FirstNameMale() string
	FirstNameFemale() string
	LastName() string
	Name() string
}

func GetPerson added in v1.3.0

func GetPerson() Dowser

type Internet added in v1.3.0

type Internet struct{}

func (Internet) DomainName added in v1.3.0

func (internet Internet) DomainName() string

func (Internet) Email added in v1.3.0

func (internet Internet) Email() string

func (Internet) Ipv4 added in v1.3.0

func (internet Internet) Ipv4() string

func (Internet) Ipv6 added in v1.3.0

func (internet Internet) Ipv6() string

func (Internet) MacAddress added in v1.3.0

func (internet Internet) MacAddress() string

func (Internet) Password added in v1.3.0

func (internet Internet) Password() string

get hash password

func (Internet) Url added in v1.3.0

func (internet Internet) Url() string

func (Internet) UserName added in v1.3.0

func (internet Internet) UserName() string

type Lorem added in v1.3.0

type Lorem struct {
}

func (Lorem) Sentence added in v1.3.0

func (l Lorem) Sentence() (sentence string)

func (Lorem) Sentences added in v1.3.0

func (l Lorem) Sentences() (sentences string)

func (Lorem) Word added in v1.3.0

func (l Lorem) Word() string

type Networker added in v1.3.0

type Networker interface {
	Email() string
	MacAddress() string
	DomainName() string
	Url() string
	UserName() string
	Ipv4() string
	Ipv6() string
	Password() string
}

func GetNetworker added in v1.3.0

func GetNetworker() Networker

Constructor

type Payment added in v1.3.0

type Payment struct{}

func (Payment) CreditCardNumber added in v1.3.0

func (p Payment) CreditCardNumber() string

CreditCardNum generated credit card number according to the card number rules

func (Payment) CreditCardType added in v1.3.0

func (p Payment) CreditCardType() string

type Person added in v1.3.0

type Person struct {
}

func (Person) FirstNameFemale added in v1.3.0

func (p Person) FirstNameFemale() string

func (Person) FirstNameMale added in v1.3.0

func (p Person) FirstNameMale() string

func (Person) LastName added in v1.3.0

func (p Person) LastName() string

func (Person) Name added in v1.3.0

func (p Person) Name() string

func (Person) TitleFeMale added in v1.3.0

func (p Person) TitleFeMale() string

func (Person) TitleMale added in v1.3.0

func (p Person) TitleMale() string

type Phone added in v1.3.0

type Phone struct{}

func (Phone) E164PhoneNumber added in v1.3.0

func (p Phone) E164PhoneNumber() string

'+27113456789'

func (Phone) PhoneNumber added in v1.3.0

func (p Phone) PhoneNumber() string

201-886-0269

func (Phone) TollFreePhoneNumber added in v1.3.0

func (p Phone) TollFreePhoneNumber() string

example : (888) 937-7238

type Phoner added in v1.3.0

type Phoner interface {
	PhoneNumber() string
	TollFreePhoneNumber() string
	E164PhoneNumber() string
}

func GetPhoner added in v1.3.0

func GetPhoner() Phoner

Constructor

type Render added in v1.3.0

type Render interface {
	CreditCardType() string
	CreditCardNumber() string
}

func GetPayment added in v1.3.0

func GetPayment() Render

Constructor

Directories

Path Synopsis
support

Jump to

Keyboard shortcuts

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